我是靠谱客的博主 直率悟空,这篇文章主要介绍Python设计模式--工厂模式&单例模式,现在分享给大家,希望可以做个参考。

 简单工厂

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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
""" 工厂模式包涵一个超类。这个超类提供一个抽象化的接口来创建一个特定类型的对象,而不是决定哪个对象可以被创建。 根据需求产生对象 """ 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 """

工厂方法

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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")

抽象工厂

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
""" 两个产品线,不同步骤 """ 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")

单例模式

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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

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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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))

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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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设计模式--工厂模式&单例模式内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部