-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfactory_method_pattern.py
More file actions
58 lines (40 loc) · 1.16 KB
/
Copy pathfactory_method_pattern.py
File metadata and controls
58 lines (40 loc) · 1.16 KB
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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'Andy'
"""
大话设计模式
设计模式——工厂方法模式
工厂方法模式(Factory Method Pattern):定义一个用于创建对象的接口,让子类决定实例化哪一个类,工厂方法使一个类的实例化延时到其子类.
工厂方法模式克服了简单工厂模式违背开放-封闭原则的缺点,又保持了封装对象创建过程的优点
场景:雷锋工厂,不关心执行者,只关心执行结果
"""
class LeiFeng(object):
def Sweep(self):
print "扫地"
def Wash(self):
print "洗衣"
def BuyRice(self):
print "买米"
class IFactory(LeiFeng):
def CreateLeiFeng(self):
pass
#大学生
class Undergraduate(LeiFeng):
pass
#新增社区服务者
class Volunteer(LeiFeng):
pass
# 学习雷锋的大学生工厂
class UndergraduateFactory(IFactory):
def CreateLeiFeng(self):
return Undergraduate()
#新增一个社区服务者的工厂
class VolunteerFactory(IFactory):
def CreateLeiFeng(self):
return Volunteer()
if __name__ == "__main__":
student = UndergraduateFactory()
volunteer = VolunteerFactory()
student.BuyRice()
student.Sweep()
volunteer.Wash()