단순 공장 모드 (python)

3819 단어 디자인 모드
"""
      :           ,            ,        
"""


class ParentClass(object):
    """
            
    """
    def __init__(self, a, b):
        self.a = a
        self.b = b
        pass

    def operation(self):
        """
            
        :return:
        """
        pass


class AddOp(ParentClass):
    """
      
    """
    def __init__(self, a, b):
        super(AddOp, self).__init__(a, b)
        pass

    def operation(self):
        return self.a + self.b


class JianOp(ParentClass):
    """
      
    """
    def __init__(self, a, b):
        super(JianOp, self).__init__(a, b)
        pass

    def operation(self):
        return self.a - self.b


class FactoryClass(object):
    """
         
    """
    def __init__(self):
        pass

    def create_class(self, op, a, b):
        if op == "+":
            return AddOp(a, b)
        elif op == "-":
            return JianOp(a, b)


if __name__ == '__main__':
    factory = FactoryClass()
    add = factory.create_class("+", 23, 34)
    print(add.operation())
    jian = factory.create_class("-", 34, 55)
    print(jian.operation())

좋은 웹페이지 즐겨찾기