Python设计模式--工厂模式&单例模式

 简单工厂

--使用Unittest框架
注释 & 每种设计模式的优缺点 后续补充

"""
工厂模式包涵一个超类。这个超类提供一个抽象化的接口来创建一个特定类型的对象,而不是决定哪个对象可以被创建。
根据需求产生对象
"""


class Fushikang:
    """手机工厂类"""

    name = None

    def __init__(self, name):
        self.name = name

    def create(self):
        pass


class Iphone(Fushikang):


    def __init__(self, name):
        super().__init__(name)

    def create(self):
        print(self.name)


class Ipad(Fushikang):

    def __init__(self, name):
        super().__init__(name)

    def create(self):
        print(self.name)


class Mac(Fushikang):

    def __init__(self, name):
        super().__init__(name)

    def create(self):
        print(self.name)


class Order:

    @staticmethod
    def create_order(self, order):
        return eval(order)(order)


class TestFactory:

    def test_factory(self):
        

        # factory
        Order().create_order(self, "Ipad").create()
        Order().create_order(self, "Mac").create()



""" 
    缺陷:
    最近airpods 销量不错,我要生产airpods
    
"""

工厂方法

import abc


class Fushikang:

    def __init__(self, name):
        self.name = name

    def create(self):
        pass


class Iphone(Fushikang):

    def __init__(self, name):
        super().__init__(name)

    def create(self):
        print(self.name)


class Ipad(Fushikang):

    def __init__(self, name):
        super().__init__(name)
        self.name = name

    def create(self):
        print(self.name)


class AbstractFactory(metaclass=abc.ABCMeta):
    """抽象工厂"""

    @abc.abstractmethod
    def create(self, name):
        pass


class IphoneFactory(AbstractFactory):
    """Iphone工厂"""

    def create(self, name):
        return Iphone(name).create()


class MacFactory(AbstractFactory):
    """Mac工厂"""

    def create(self, name):
        return Ipad(name).create()


class TestFactory:

    def test_factory(self):
        IphoneFactory().create("Iphone")
        IphoneFactory().create("Mac")
        IphoneFactory().create("Iphone15")

抽象工厂

"""
两个产品线,不同步骤
"""
import abc


class Fushikang:

    def __init__(self, name):
        self.name = name

    def create(self):
        pass


class IphoneStepOne(Fushikang):

    def __init__(self, name):
        super().__init__(name)

    def create(self):
        print("生产摄像头")


# class IpadStepOne(Fushikang):
#
#     def __init__(self, name):
#         super().__init__(name)
#         self.name = name
#
#     def create(self):
#         print(self.name)


class IphoneStepTwo(Fushikang):

    def __init__(self, name):
        super().__init__(name)

    def create(self):
        print("生产屏幕")


class IpadStepTwo(Fushikang):

    def __init__(self, name):
        super().__init__(name)
        self.name = name

    def create(self):
        print("生产处理器")


class AbstractFactory(metaclass=abc.ABCMeta):
    """抽象工厂"""

    @abc.abstractmethod
    def step_one(self, name):
        pass

    @abc.abstractmethod
    def step_two(self, name):
        pass


class IphoneFactory(AbstractFactory):
    """Iphone工厂"""

    def step_one(self, name):
        return IphoneStepOne(name).create()

    def step_two(self, name):
        return IphoneStepTwo(name).create()


class IpadFactory(AbstractFactory):
    """Ipad工厂"""

    def step_one(self, name):
        return IphoneStepOne(name).create()

    def step_two(self, name):
        return IpadStepTwo(name).create()


class TestFactory:

    def test_abs_factory(self):
        IphoneFactory().step_one("step_one")
        IphoneFactory().step_two("step_two")

        print("------------------------")
        IpadFactory().step_one("step_one")
        IpadFactory().step_two("step_two")

单例模式

class Singleton(object):
    def __init__(self, name):
        self.name = name

    def create(self):
        print(self.name)


iPhone = Singleton("iphone")


def singleton_two(cls):
    _instance = {}

    def _singleton_two(*args, **kwargs):
        if cls not in _instance:
            _instance[cls] = cls(*args, **kwargs)
        return _instance[cls]

    return _singleton_two

单例模式的两种实现--通过引入文件的方式实现


from singsingleton import iPhone as iphone_11
from singsingleton import iPhone as iphone_12


class TestSingleton:

    def test_singleton(self):
        iphone_11.create()
        iphone_12.create()
        print(id(iphone_11))
        print(id(iphone_12))


单例模式的两种实现--通过装饰器实现

# Method Two:通过装饰器实现
from singsingleton import singleton_two


@singleton_two
class Iphone(object):

    def __init__(self, name):
        self.name = name

    def create(self):
        print(self.name)


class Mac(object):

    def __init__(self, name):
        self.name = name

    def create(self):
        print(self.name)


class TestSingletonTwo:

    def test_singleton_two(self):
        iphone_11 = Iphone("iphone11")
        iphone_12 = Iphone("iphone12")
        iphone_11.create()
        iphone_12.create()
        print(id(iphone_11))
        print(id(iphone_12))

        print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")

        mac_air = Mac("mac_air")
        mac_pro = Mac("mac_pro")
        mac_air.create()
        mac_pro.create()
        print(id(mac_air))
        print(id(mac_pro))

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值