python设计模式之抽象工厂

from abc import ABCMeta, abstractmethod


# 定义抽象产品
class CheLun(metaclass=ABCMeta):
    @abstractmethod
    def create_chelun(self):
        pass


class FaDongji(metaclass=ABCMeta):
    @abstractmethod
    def create_fadongji(self):
        pass


#定义具体产品
class WuLingCheLun(CheLun):
    def create_chelun(self):
        print("五菱车轮")


class BenCheLun(CheLun):
    def create_chelun(self):
        print("奔驰车轮")


class WuLingFaDongJi(FaDongji):
    def create_fadongji(self):
        print("五菱发动机")


class BenFaDongJi(FaDongji):
    def create_fadongji(self):
        print("奔驰发动机")


#定义抽象工厂
class CarFactory(metaclass=ABCMeta):

    @abstractmethod
    def create_chelun(self):
        pass

    @abstractmethod
    def create_fadongji(self):
        pass


# 定义具体工厂
class WulingFactory(CarFactory):
    def create_chelun(self):
        return WuLingCheLun()

    def create_fadongji(self):
        return WuLingFaDongJi()


class BenFactory(CarFactory):
    def create_chelun(self):
        return BenCheLun()

    def create_fadongji(self):
        return BenFaDongJi()


class Create(object):
    def __init__(self, chelun, fasongji):
        self.chelun = chelun
        self.fadongji = fasongji

    def show_info(self):
        self.chelun.create_chelun()
        self.fadongji.create_fadongji()


def client(factory):
    chelun = factory.create_chelun()
    fadongji = factory.create_fadongji()
    Create(chelun, fadongji).show_info()


if __name__ == '__main__':
    client(WulingFactory())
    client(BenFactory())

你可能感兴趣的:(python,设计模式,开发语言)