Skip to content

Commit b20f16a

Browse files
author
刘欣
committed
init
0 parents  commit b20f16a

13 files changed

+914
-0
lines changed

chain_of_responsibility.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Handler(object):
2+
3+
def __init__(self):
4+
self.successor = None
5+
6+
def handle(self, data):
7+
res = self._handle(data)
8+
if res:
9+
return res
10+
if self.successor:
11+
return self.successor.handle(data)
12+
13+
def _handle(self, data):
14+
raise NotImplementedError
15+
16+
def link(self, handler):
17+
self.successor = handler
18+
return handler
19+
20+
21+
class DictHandler(Handler):
22+
def _handle(self, data):
23+
if isinstance(data, dict):
24+
print("handled by %s" % self)
25+
return True
26+
27+
28+
class ListHandler(Handler):
29+
def _handle(self, data):
30+
if isinstance(data, list):
31+
print("handled by %s" % self)
32+
return True
33+
34+
35+
if __name__ == '__main__':
36+
h = DictHandler()
37+
h.link(ListHandler()).link(Handler())
38+
ret = h.handle([1, 2, 3])
39+
ret = h.handle({1: 2})

chaining_method.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Player(object):
2+
def __init__(self, name):
3+
self.pos = (0, 0)
4+
5+
def move(self, pos):
6+
self.pos = pos
7+
print("move to %s, %s" % self.pos)
8+
return self
9+
10+
def say(self, text):
11+
print(text)
12+
return self
13+
14+
def home(self):
15+
self.pos = (0, 0)
16+
print("I am home")
17+
return self
18+
19+
20+
if __name__ == '__main__':
21+
p = Player('liuxin')
22+
p.move((1, 1)).say("haha").move((2, 3)).home().say("go to sleep")

command.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from collections import deque
2+
3+
4+
class Document(object):
5+
value = ""
6+
cmd_stack = deque()
7+
8+
@classmethod
9+
def execute(cls, cmd):
10+
cmd.redo()
11+
cls.cmd_stack.append(cmd)
12+
13+
@classmethod
14+
def undo(cls):
15+
cmd = cls.cmd_stack.pop()
16+
cmd.undo()
17+
18+
19+
class AddTextCommand(object):
20+
def __init__(self, text):
21+
self.text = text
22+
23+
def redo(self):
24+
Document.value += self.text
25+
26+
def undo(self):
27+
Document.value = Document.value[:-len(self.text)]
28+
29+
30+
if __name__ == '__main__':
31+
cmds = [AddTextCommand("liu"), AddTextCommand("xin"), AddTextCommand("heihei")]
32+
for cmd in cmds:
33+
Document.execute(cmd)
34+
print(Document.value)
35+
36+
for i in range(len(cmds)):
37+
Document.undo()
38+
print(Document.value)

factory.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Shape(object):
2+
@classmethod
3+
def factory(cls, name, *args, **kwargs):
4+
types = {c.__name__: c for c in cls.__subclasses__()}
5+
shape_class = types[name]
6+
return shape_class(*args, **kwargs)
7+
8+
9+
class Circle(Shape):
10+
pass
11+
12+
13+
class Square(Shape):
14+
pass
15+
16+
17+
if __name__ == '__main__':
18+
shapes = ["Circle", "Square", "Square", "Circle"]
19+
for i in shapes:
20+
s = Shape.factory(i)
21+
print(s)

iterator.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def fibonacci(count=100):
2+
a, b = 1, 2
3+
yield a
4+
yield b
5+
while count:
6+
a, b = b, a + b
7+
count -= 1
8+
yield b
9+
10+
11+
for i in fibonacci():
12+
print(i)

mvc.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class Model(object):
2+
products = {
3+
'milk': {'price': 1.50, 'quantity': 10},
4+
'eggs': {'price': 0.20, 'quantity': 100},
5+
'cheese': {'price': 2.00, 'quantity': 10}
6+
}
7+
8+
def get(self, name):
9+
return self.products.get(name)
10+
11+
12+
class View(object):
13+
def show_item_list(self, item_list):
14+
print('-' * 20)
15+
for item in item_list:
16+
print("* Name: %s" % item)
17+
print('-' * 20)
18+
19+
def show_item_info(self, name, item_info):
20+
print("Name: %s Price: %s Quantity: %s" % (name, item_info['price'], item_info['quantity']))
21+
print('-' * 20)
22+
23+
def show_empty(self, name):
24+
print("Name: %s not found" % name)
25+
print('-' * 20)
26+
27+
28+
class Controller(object):
29+
def __init__(self, model, view):
30+
self.model = model
31+
self.view = view
32+
33+
def show_items(self):
34+
items = self.model.products.keys()
35+
self.view.show_item_list(items)
36+
37+
def show_item_info(self, item):
38+
item_info = self.model.get(item)
39+
if item_info:
40+
self.view.show_item_info(item, item_info)
41+
else:
42+
self.view.show_empty(item)
43+
44+
45+
if __name__ == '__main__':
46+
model = Model()
47+
view = View()
48+
controller = Controller(model, view)
49+
controller.show_items()
50+
controller.show_item_info('cheese')
51+
controller.show_item_info('apple')

observer.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Observable(object):
2+
3+
def __init__(self):
4+
self._observers = []
5+
6+
def attach(self, observer):
7+
if observer not in self._observers:
8+
self._observers.append(observer)
9+
10+
def detach(self, observer):
11+
self._observers.remove(observer)
12+
13+
def notify(self):
14+
for observer in self._observers:
15+
observer.update(self)
16+
17+
18+
class Observer(object):
19+
def update(self, observable):
20+
print('updating %s by %s' % (self, observable))
21+
22+
23+
if __name__ == '__main__':
24+
clock = Observable()
25+
user1 = Observer()
26+
user2 = Observer()
27+
clock.attach(user1)
28+
clock.attach(user2)
29+
clock.notify()

0 commit comments

Comments
 (0)