File tree 1 file changed +63
-0
lines changed
1 file changed +63
-0
lines changed Original file line number Diff line number Diff line change
1
+ #
2
+ # Python Design Patterns: Command
3
+ # Author: Jakub Vojvoda [github.com/JakubVojvoda]
4
+ # 2016
5
+ #
6
+ # Source code is licensed under MIT License
7
+ # (for more details see LICENSE)
8
+ #
9
+
10
+ import sys
11
+
12
+ #
13
+ # Receiver
14
+ # knows how to perform the operations associated
15
+ # with carrying out a request
16
+ #
17
+ class Receiver :
18
+ def action (self ):
19
+ print ("Receiver: execute action." )
20
+
21
+ #
22
+ # Command
23
+ # declares an interface for all commands
24
+ #
25
+ class Command :
26
+ def execute (self ):
27
+ pass
28
+
29
+ #
30
+ # Concrete Command
31
+ # implements execute by invoking the corresponding
32
+ # operation(s) on Receiver
33
+ #
34
+ class ConcreteCommand (Command ):
35
+ def __init__ (self , receiver ):
36
+ self ._receiver = receiver
37
+
38
+ def execute (self ):
39
+ self ._receiver .action ()
40
+
41
+ #
42
+ # Invoker
43
+ # asks the command to carry out the request
44
+ #
45
+ class Invoker :
46
+ def __init__ (self ):
47
+ self ._command = None
48
+
49
+ def set (self , command ):
50
+ self ._command = command
51
+
52
+ def confirm (self ):
53
+ if (self ._command is not None ):
54
+ self ._command .execute ()
55
+
56
+
57
+ if __name__ == "__main__" :
58
+ receiver = Receiver ()
59
+ command = ConcreteCommand (receiver )
60
+
61
+ invoker = Invoker ()
62
+ invoker .set (command )
63
+ invoker .confirm ()
You can’t perform that action at this time.
0 commit comments