一、单例模式
单例:单个实例,确保某个类只有一个实例存在
举例:1、登录app,当前的登录对象,只能有一个
2、音乐播放器,正在播放的歌曲类而言,只能有一个实例
1 import my_util # 被导入的模块会从头到尾执行一遍
2
3 o1 = my_util.a
4 o2 = my_util.a
5 o3 = my_util.a
6
7 print(o1)
8 print(o2)
9 print(o3)
10
11
12 class Person:
13 __obj = None # 单例
14
15 def __new__(cls, *args, **kwargs): # 创建实例对象 ---> 实例
16
17 if not cls.__obj:
18 cls.__obj = super().__new__(cls)
19 return cls.__obj
20
21 def __init__(self): # 自己,本身; 谁调用谁就是self; 实际上,在调用init时, 实例对象已经被创建出来了;
22 pass
23
24
25 p1 = Person() # 1. 调用new方法,创建实例对象, 开辟内存空间, 存储对象 2. 自动调用init方法
26 p2 = Person()
27
28 print(p1) # <__main__.Person object at 0x000000000269AA88>
29 print(p2) # <__main__.Person object at 0x000000000269AA88>
30
31 # print(Person.__obj)
二、工厂模式
根据不同的条件, 创建不同的实例对象;
汽车父类
兰博基尼子类
法拉利子类
路虎子类
工厂类
方法:传入不同的钱,给你返回不同的汽车对象
商场类
属性:工厂对象
方法:卖小汽车,通过工厂对象卖小汽车
1 class Car:
2 def run(self):
3 print("车行驶")
4
5 def stop(self):
6 print("车制动")
7
8
9 class Lamborghini(Car):
10 def run(self):
11 print("兰博基尼行驶")
12 def stop(self):
13 print("兰博基尼制动")
14
15
16 class Ferrari(Car):
17 def run(self):
18 print("法拉利行驶")
19 def stop(self):
20 print("法拉利制动")
21
22
23 class Land_rover(Car):
24 def run(self):
25 print("路虎行驶")
26 def stop(self):
27 print("路虎制动")
28
29
30 class Factory:
31 def get_money(self, money):
32 if money < 10:
33 return Land_rover()
34 elif money < 100:
35 return Ferrari()
36 else:
37 return Lamborghini()
38
39
40 class SuperMarket:
41 def __init__(self, f_obj): # 参数: 工厂对象
42 self.f_obj = f_obj
43
44 def sale_car(self, money):
45 return self.f_obj.get_money(money)
46
47
48 f = Factory()
49 # obj = f.get_money(9)
50 # obj.run()
51 # obj.stop()
52
53 s = SuperMarket(f)
54 car = s.sale_car(115)
55 car.run()
56 car.stop()
三、策略模式
达到某个目标,有多种不同的方式,这些方式都有可能会使用到
具体使用哪种方式,根据需求来定
1 # 通知客户搞活动, 发邮件, 发短信;
2
3 # 发送信息的父类
4 class Msg: # 信息的内容, 联系方式, 发送的方法
5 def __init__(self, info, way):
6 self.info = info
7 self.way = way # 1336669999 [email protected]
8
9 def send(self):
10 pass
11
12
13 # 发邮件
14 class Email(Msg):
15 def send(self):
16 return "用邮件的方式, 给{}发送活动信息: {}".format(self.way, self.info)
17
18
19 # 发短信
20 class ShortMsg(Msg):
21 def send(self):
22 return "用短信的方式, 给{}发送活动信息: {}".format(self.way, self.info)
23
24
25 class Customer:
26 def __init__(self, name, tel, email):
27 self.name = name
28 self.tel = tel
29 self.email = email
30 self.receive_way = None # 接收信息的方式
31
32 def receive_info(self):
33 return self.receive_way.send()
34
35 def set_receive_way(self, obj): # 设置接收信息的方式 obj可能是邮件对象, 也有可能是短信对象
36 self.receive_way = obj
37
38
39 customer = Customer("xixi", "13333333333", "[email protected]")
40
41 email = Email("周年庆,买一送一", customer.email)
42 short_msg = ShortMsg("周年庆,买一送一", customer.tel)
43
44 customer.set_receive_way(short_msg) # 设置接收信息的方式就是短信
45 print(customer.receive_info())
四、观察者模式
小喇叭:一个人说,全校人听
通知中心:校长用喇叭喊
观察者:可以有n多个观察者,一直在监听通知
1 # 通知中心(校长)
2 class Monitor:
3 observer_list = [] # 观察者列表
4 info = "" # 通知的内容
5 name = "" # 名字
6
7 def set_observer(self, obj): # 绑定观察者, 参数:观察者对象
8 Monitor.observer_list.append(obj)
9
10 def show_info(self): # 发布通知
11 for observer in Monitor.observer_list: # 遍历获取每一个观察者
12 observer.receive_info() # 调用观察者的接收信息的方法
13
14
15 # 观察者(学生)
16 class Student:
17
18 def __init__(self, name, monitor):
19 self.name = name
20 self.monitor = monitor # 通知中心
21
22 def receive_info(self):
23 print("接收方:{}, {}发布了通知, 通知的内容为:{}".format(self.name, self.monitor.name, self.monitor.info))
24
25
26 m = Monitor()
27 Monitor.info = "今天开派对,时间是19:30"
28 Monitor.name = "xixi"
29
30 s1 = Student("haha", m)
31 s2 = Student("hanhan", m)
32
33 m.set_observer(s1)
34 m.set_observer(s2)
35
36 m.show_info()