File tree 10 files changed +271
-10
lines changed 10 files changed +271
-10
lines changed Original file line number Diff line number Diff line change 1
1
#!/usr/bin/env python
2
2
# -*- encoding: utf-8 -*-
3
- """
4
- Topic: sample
5
- Desc :
6
- """
7
-
8
- def main ():
9
- pass
10
-
11
- if __name__ == '__main__' :
12
- main ()
Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env python
2
+ # -*- encoding: utf-8 -*-
3
+ """
4
+ Topic: 单例模式
5
+ Desc : 实际上python里面不需要单例模式,直接用模块就行了
6
+ """
7
+
8
+
9
+ def singleton (cls , * args , ** kw ):
10
+ """定义一个单例装饰器"""
11
+ instances = {}
12
+
13
+ def _singleton ():
14
+ if cls not in instances :
15
+ instances [cls ] = cls (* args , ** kw )
16
+ return instances [cls ]
17
+
18
+ return _singleton
19
+
20
+
21
+ @singleton
22
+ class MyClass (object ):
23
+ a = 1
24
+
25
+ def __init__ (self , x = 0 ):
26
+ self .x = x
27
+
28
+ if __name__ == '__main__' :
29
+ one = MyClass ()
30
+ two = MyClass ()
31
+ print (one .a )
32
+ one .a = 2
33
+ print (two .a )
Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env python
2
+ # -*- encoding: utf-8 -*-
3
+ """
4
+ Topic: 工厂方法
5
+ Desc : 抽象工厂中定义一个方法,其中会有参数输入,
6
+ 而实现类通过传入的参数判断该生产出哪种对象
7
+ """
8
+
9
+
10
+ class ConcreteProduct1 :
11
+ def output (self ):
12
+ print ('ConcreteProduct1' )
13
+
14
+
15
+ class ConcreteProduct2 :
16
+ def output (self ):
17
+ print ('ConcreteProduct2' )
18
+
19
+
20
+ class Creator :
21
+ def create_product (self , type ):
22
+ return {'1' : ConcreteProduct1 (), '2' : ConcreteProduct2 ()}[type ]
23
+
24
+ if __name__ == '__main__' :
25
+ creator = Creator ()
26
+ creator .create_product ('1' ).output ()
27
+ creator .create_product ('2' ).output ()
28
+
Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env python
2
+ # -*- encoding: utf-8 -*-
3
+ """
4
+ Topic: 抽象工厂模式
5
+ Desc : 为创建一组相关或相互依赖的对象提供一个借口,而且无需指定它们的具体类
6
+ """
7
+
8
+
9
+ class ProductA1 :
10
+ def do_something (self ):
11
+ print ('产品A1的实现方法' )
12
+
13
+
14
+ class ProductA2 :
15
+ def do_something (self ):
16
+ print ('产品A2的实现方法' )
17
+
18
+
19
+ class ProductB1 :
20
+ def do_something (self ):
21
+ print ('产品B1的实现方法' )
22
+
23
+
24
+ class ProductB2 :
25
+ def do_something (self ):
26
+ print ('产品B2的实现方法' )
27
+
28
+
29
+ class Creator1 :
30
+ """生产系列1的产品"""
31
+ def create_product_a (self ):
32
+ return ProductA1 ()
33
+
34
+ def create_product_b (self ):
35
+ return ProductB1 ()
36
+
37
+
38
+ class Creator2 :
39
+ """生产系列2的产品"""
40
+ def create_product_a (self ):
41
+ return ProductA2 ()
42
+
43
+ def create_product_b (self ):
44
+ return ProductB2 ()
Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env python
2
+ # -*- encoding: utf-8 -*-
3
+ """
4
+ Topic: 模板方法模式
5
+ Desc :
6
+ """
7
+
8
+
9
+ class AbstractTemplate :
10
+ # 基本方法1
11
+ def do_something (self ):
12
+ pass
13
+
14
+ # 基本方法2
15
+ def do_anything (self ):
16
+ pass
17
+
18
+ # 模板方法
19
+ def template_method (self ):
20
+ # 调用基本方法,完成相关的业务逻辑
21
+ self .do_something ()
22
+ self .do_anything ()
23
+
24
+
25
+ class ConcreteClass1 (AbstractTemplate ):
26
+ # 基本方法1
27
+ def do_something (self ):
28
+ print ('class1 doSomething...' )
29
+
30
+ # 基本方法2
31
+ def do_anything (self ):
32
+ print ('class1 doAnything...' )
33
+
34
+
35
+ class ConcreteClass2 (AbstractTemplate ):
36
+ # 基本方法1
37
+ def do_something (self ):
38
+ print ('class2 doSomething...' )
39
+
40
+ # 基本方法2
41
+ def do_anything (self ):
42
+ print ('class2 doAnything...' )
43
+
44
+ if __name__ == '__main__' :
45
+ c1 = ConcreteClass1 ()
46
+ c1 .template_method ()
47
+ c2 = ConcreteClass2 ()
48
+ c2 .template_method ()
Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env python
2
+ # -*- encoding: utf-8 -*-
3
+ """
4
+ Topic: Builder模式
5
+ Desc :
6
+ 在Builder模式中,有如下3个角色:
7
+ 1,Product产品类
8
+ 通常是实现了模板方法模式,也就是有模板方法和基本方法
9
+ 2,Builder类
10
+ 可随时返回一个组建好的产品对象
11
+ 3,Director导演类
12
+ 负责安排已有模块的顺序,然后告诉Builder怎样建造产品对象
13
+ """
14
+
15
+
16
+ class Product :
17
+ def do_something (self ):
18
+ print ('do_something' )
19
+
20
+ def do_otherthing (self ):
21
+ print ('do_otherthing' )
22
+
23
+
24
+ class Builder :
25
+ def __init__ (self , product ):
26
+ self .product = product
27
+
28
+ def build_something (self ):
29
+ self .product .do_something ()
30
+
31
+ def build_otherthing (self ):
32
+ self .product .do_otherthing ()
33
+
34
+ def build_product (self ):
35
+ return self .product
36
+
37
+
38
+ class Director :
39
+ def __init__ (self ):
40
+ self .builder = Builder (Product ())
41
+
42
+ def get_product_a (self ):
43
+ self .builder .build_something ()
44
+ return self .builder .build_product ()
45
+
46
+ def get_product_b (self ):
47
+ self .builder .build_something ()
48
+ self .builder .build_otherthing ()
49
+ return self .builder .build_product ()
50
+
51
+ if __name__ == '__main__' :
52
+ director = Director ()
53
+ director .get_product_a ()
54
+ print ('-----------------' )
55
+ director .get_product_b ()
Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env python
2
+ # -*- encoding: utf-8 -*-
3
+ """
4
+ Topic: 代理模式
5
+ Desc : 代理模式,也叫委托模式 是一个使用率非常高的模式,
6
+ 非常典型的场景就是游戏代练,代练者就是一个代理者或者委托者。
7
+ """
8
+
9
+
10
+ class RealSubject :
11
+ def request (self ):
12
+ print ('核心业务逻辑' )
13
+
14
+
15
+ class Proxy (RealSubject ):
16
+ def __init__ (self ):
17
+ self .real_subject = RealSubject ()
18
+
19
+ def request (self ):
20
+ self .before ()
21
+ self .real_subject .request ()
22
+ self .end ()
23
+
24
+ def before (self ):
25
+ print ('before' )
26
+
27
+ def end (self ):
28
+ print ('end' )
29
+
30
+ if __name__ == '__main__' :
31
+ p = Proxy ()
32
+ p .request ()
Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env python
2
+ # -*- encoding: utf-8 -*-
3
+ """
4
+ Topic: class和instance的练习
5
+ Desc :
6
+ """
7
+
8
+
9
+ class Dog :
10
+ # 可变对象最好不要定义为类变量,防止共享时修改混乱
11
+ kind = 'canine' # class variable shared by all instances
12
+
13
+ def __init__ (self , name ):
14
+ self .name = name # instance variable unique to each instance
15
+
16
+
17
+ def change_dog ():
18
+ Dog .kind = 'another'
19
+
20
+
21
+ if __name__ == '__main__' :
22
+ a = Dog ('adog' )
23
+ b = Dog ('bdog' )
24
+ print (Dog .kind , a .kind , a .name )
25
+ print (Dog .kind , b .kind , b .name )
26
+ change_dog ()
27
+ print (Dog .kind , a .kind , a .name )
28
+ print (Dog .kind , b .kind , b .name )
29
+
Original file line number Diff line number Diff line change @@ -20,10 +20,12 @@ def name(self):
20
20
"""子类给我必须实现这个特性"""
21
21
pass
22
22
23
+
23
24
class Grok ():
24
25
def spam (self , a , b ):
25
26
print ("Grok..." )
26
27
28
+
27
29
def main ():
28
30
Foo .register (Grok ) # 向抽象基类注册
29
31
pass
File renamed without changes.
You can’t perform that action at this time.
0 commit comments