콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
1 회신
4824 화면

Is it possible to implement singleton design pattern in odoo framework. 

아바타
취소
베스트 답변

Yes, it is possible to implement the singleton design pattern in the Odoo framework. To do this, you can create a base model with a class method that returns the same instance of the model each time it is called. The model would be initialized with a default value, and any subsequent calls to the class method would return that initialized instance. Here is an example:

class MyModel(models.Model):
    _name = 'my.model'

    def __init__(self):
        self.value = 0

    @classmethod
    def get_instance(cls):
        if not hasattr(cls, 'instance'):
            cls.instance = MyModel()
        return cls.instance

In this example, MyModel.get_instance() would always return the same instance of MyModel, with the value attribute initialized to 0. You can then use this instance in your code as needed. Note that this implementation of the singleton pattern is not thread-safe, so you may need to add additional logic to ensure that multiple threads cannot access the instance at the same time.

아바타
취소