forked from faif/python-patterns
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommand.py
More file actions
167 lines (123 loc) · 3.34 KB
/
command.py
File metadata and controls
167 lines (123 loc) · 3.34 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
# class MoveFileCommand(object):
# def __init__(self, src, dest):
# self.src = src
# self.dest = dest
# def execute(self):
# self()
# def __call__(self):
# print('renaming {} to {}'.format(self.src, self.dest))
# os.rename(self.src, self.dest)
# def undo(self):
# print('renaming {} to {}'.format(self.dest, self.src))
# os.rename(self.dest, self.src)
# def main():
# command_stack = []
# # commands are just pushed into the command stack
# command_stack.append(MoveFileCommand('foo.txt', 'bar.txt'))
# command_stack.append(MoveFileCommand('bar.txt', 'baz.txt'))
# # they can be executed later on
# for cmd in command_stack:
# cmd.execute()
# # and can also be undone at will
# for cmd in reversed(command_stack):
# cmd.undo()
# if __name__ == "__main__":
# main()
### OUTPUT ###
# renaming foo.txt to bar.txt
# renaming bar.txt to baz.txt
# renaming baz.txt to bar.txt
# renaming bar.txt to foo.txt
#---------------------------------------------#
#---------------------------------------#
# my command pattern
#---------------------------------------#
class Vehicle(object):
"""docstring for ClassName"""
def __init__(self):
self.name = 'vehicle'
def __call__(self):
print "travelling by a Vehicle"
def execute(self):
self()
class Bus(Vehicle):
"""docstring for Bus"""
def __init__(self):
super(Vehicle, self).__init__()
self.name = 'bus'
def __call__(self):
print 'travelling by bus'
def execute(self):
self()
class Bike(Vehicle):
"""docstring for Bike"""
def __init__(self):
super(Vehicle, self).__init__()
self.name = 'bike'
def __call__(self):
print 'travelling by bike'
def execute(self):
self()
class Car(Vehicle):
"""docstring for Car"""
def __init__(self):
super(Vehicle, self).__init__()
self.name = 'car'
def __call__(self):
print 'travelling by car'
def execute(self):
self()
class Rocket(Vehicle):
"""docstring for Rocket"""
def __init__(self):
super(Vehicle, self).__init__()
self.name = 'rocket'
def __call__(self):
print 'travelling by rocket'
def execute(self):
self()
class Traveller(object):
"""docstring for Traveller"""
def __init__(self, name):
self.name = name
def set_vehicle(self, vehicle):
self.vehicle = vehicle
def travel(self):
self.vehicle.execute()
def main():
# create a traveller
tom = Traveller('tom')
print 'stop 1:'
# traveller's first vehicle
vehicle = Bus()
tom.set_vehicle(vehicle)
tom.travel()
print 'stop 2:'
# traveller's second vehicle
vehicle = Bike()
tom.set_vehicle(vehicle)
tom.travel()
print 'stop 3:'
# traveller's third vehicle
vehicle = Car()
tom.set_vehicle(vehicle)
tom.travel()
print 'stop 4:'
# traveller's fourth vehicle
vehicle = Rocket()
tom.set_vehicle(vehicle)
tom.travel()
if __name__ == '__main__':
main()
### OUTPUT ###
# stop 1:
# travelling by bus
# stop 2:
# travelling by bike
# stop 3:
# travelling by car
# stop 4:
# travelling by rocket