我是靠谱客的博主 直率悟空,最近开发中收集的这篇文章主要介绍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))

最后

以上就是直率悟空为你收集整理的Python设计模式--工厂模式&单例模式的全部内容,希望文章能够帮你解决Python设计模式--工厂模式&单例模式所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部