forked from mlyangyue/design_patterns
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathabstract_factory_pattern.py
108 lines (77 loc) · 2.02 KB
/
abstract_factory_pattern.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'Andy'
"""
大话设计模式
设计模式——抽象工厂模式
抽象工厂模式(Abstract Factory Pattern):提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们的类
"""
import sys
#抽象用户表类
class User(object):
def get_user(self):
pass
def insert_user(self):
pass
#抽象部门表类
class Department(object):
def get_department(self):
pass
def insert_department(self):
pass
#操作具体User数据库类-Mysql
class MysqlUser(User):
def get_user(self):
print 'MysqlUser get User'
def insert_user(self):
print 'MysqlUser insert User'
#操作具体Department数据库类-Mysql
class MysqlDepartment(Department):
def get_department(self):
print 'MysqlDepartment get department'
def insert_department(self):
print 'MysqlDepartment insert department'
#操作具体User数据库-Orcal
class OrcalUser(User):
def get_user(self):
print 'OrcalUser get User'
def insert_user(self):
print 'OrcalUser insert User'
#操作具体Department数据库类-Orcal
class OrcalDepartment(Department):
def get_department(self):
print 'OrcalDepartment get department'
def insert_department(self):
print 'OrcalDepartment insert department'
#抽象工厂类
class AbstractFactory(object):
def create_user(self):
pass
def create_department(self):
pass
class MysqlFactory(AbstractFactory):
def create_user(self):
return MysqlUser()
def create_department(self):
return MysqlDepartment()
class OrcalFactory(AbstractFactory):
def create_user(self):
return OrcalUser()
def create_department(self):
return OrcalDepartment()
if __name__ == "__main__":
db = sys.argv[1]
myfactory = ''
if db == 'Mysql':
myfactory = MysqlFactory()
elif db == 'Orcal':
myfactory = OrcalFactory()
else:
print "不支持的数据库类型"
exit(0)
user = myfactory.create_user()
department = myfactory.create_department()
user.insert_user()
user.get_user()
department.insert_department()
department.get_department()