doc.dev1x.org

Abstract Factory

Appendix-1 ソースコード全体

from abc import ABCMeta, abstractmethod

class Factory(metaclass=ABCMeta):
    @abstractmethod
    def createProgramA(self):
        pass

    @abstractmethod
    def createProgramB(self):
        pass

    @abstractmethod
    def createProgramC(self):
        pass
from abc import ABCMeta, abstractmethod

class ProgramA(metaclass=ABCMeta):
    @abstractmethod
    def exec(self):
        pass

class ProgramB(metaclass=ABCMeta):
    @abstractmethod
    def exec(self):
        pass

class ProgramC(metaclass=ABCMeta):
    @abstractmethod
    def exec(self):
        pass
from factory import Factory
from program import ProgramA, ProgramB, ProgramC

class Type01Factory(Factory):
    def createProgramA(self):
        return Type01ProgramA()

    def createProgramB(self):
        return Type01ProgramB()

    def createProgramC(self):
        return Type01ProgramC()

class Type01ProgramA(ProgramA):
    def exec(self):
        return "Execute Type01ProgramA"

class Type01ProgramB(ProgramB):
    def exec(self):
        return "Execute Type01ProgramB"

class Type01ProgramC(ProgramC):
    def exec(self):
        return "Execute Type01ProgramC"
from factory import Factory
from program import ProgramA, ProgramB, ProgramC

class Type02Factory(Factory):
    def createProgramA(self):
        return Type02ProgramA()

    def createProgramB(self):
        return Type02ProgramB()

    def createProgramC(self):
        return Type02ProgramC()

class Type02ProgramA(ProgramA):
    def exec(self):
        return "Execute Type02ProgramA"

class Type02ProgramB(ProgramB):
    def exec(self):
        return "Execute Type02ProgramB"

class Type02ProgramC(ProgramC):
    def exec(self):
        return "Execute Type02ProgramC"
from factory import Factory
from program import ProgramA, ProgramB, ProgramC

class Type03Factory(Factory):
    def createProgramA(self):
        return Type03ProgramA()

    def createProgramB(self):
        return Type03ProgramB()

    def createProgramC(self):
        return Type03ProgramC()

class Type03ProgramA(ProgramA):
    def exec(self):
        return "Execute Type03ProgramA"

class Type03ProgramB(ProgramB):
    def exec(self):
        return "Execute Type03ProgramB"

class Type03ProgramC(ProgramC):
    def exec(self):
        return "Execute Type03ProgramC"
class Client:
    def __init__(self, factory):
        self.factory = factory

    def execProgramA(self):
        p = self.factory.createProgramA()
        print(p.exec())

    def execProgramB(self):
        p = self.factory.createProgramB()
        print(p.exec())

    def execProgramC(self):
        p = self.factory.createProgramC()
        print(p.exec())


if __name__=='__main__':
    type1 = Client(Type01Factory())
    type1.execProgramA()       # => Execute Type01ProgramA
    type1.execProgramB()       # => Execute Type01ProgramB
    type1.execProgramC()       # => Execute Type01ProgramC

    type2 = Client(Type02Factory())
    type2.execProgramA()       # => Execute Type02ProgramA
    type2.execProgramB()       # => Execute Type02ProgramB
    type2.execProgramC()       # => Execute Type02ProgramC

    type3 = Client(Type03Factory())
    type3.execProgramA()       # => Execute Type03ProgramA
    type3.execProgramB()       # => Execute Type03ProgramB
    type3.execProgramC()       # => Execute Type03ProgramC