|
2 | 2 | # -*- coding: utf-8 -*- |
3 | 3 |
|
4 | 4 | """ |
| 5 | +Example from https://en.wikipedia.org/wiki/Facade_pattern#Python |
| 6 | +
|
| 7 | +
|
5 | 8 | *What is this pattern about? |
6 | 9 | The Facade pattern is a way to provide a simpler unified interface to |
7 | 10 | a more complex system. It provides an easier way to access functions |
|
13 | 16 | serves as an unified interface to all the underlying procedures to |
14 | 17 | turn on a computer. |
15 | 18 |
|
16 | | -*What does this example do? |
17 | | -The code defines three classes (TC1, TC2, TC3) that represent complex |
18 | | -parts to be tested. Instead of testing each class separately, the |
19 | | -TestRunner class acts as the facade to run all tests with only one |
20 | | -call to the method runAll. By doing that, the client part only needs |
21 | | -to instantiate the class TestRunner and call the runAll method. |
22 | | -As seen in the example, the interface provided by the Facade pattern |
23 | | -is independent from the underlying implementation. Since the client |
24 | | -just calls the runAll method, we can modify the classes TC1, TC2 or |
25 | | -TC3 without impact on the way the client uses the system. |
26 | | -
|
27 | 19 | *Where is the pattern used practically? |
28 | 20 | This pattern can be seen in the Python standard library when we use |
29 | 21 | the isdir function. Although a user simply uses this function to know |
|
40 | 32 | """ |
41 | 33 |
|
42 | 34 | from __future__ import print_function |
43 | | -import time |
44 | | - |
45 | | -SLEEP = 0.1 |
46 | | - |
47 | | - |
48 | | -# Complex Parts |
49 | | -class TC1: |
50 | | - def run(self): |
51 | | - print(u"###### In Test 1 ######") |
52 | | - time.sleep(SLEEP) |
53 | | - print(u"Setting up") |
54 | | - time.sleep(SLEEP) |
55 | | - print(u"Running test") |
56 | | - time.sleep(SLEEP) |
57 | | - print(u"Tearing down") |
58 | | - time.sleep(SLEEP) |
59 | | - print(u"Test Finished\n") |
60 | | - |
61 | | - |
62 | | -class TC2: |
63 | | - def run(self): |
64 | | - print(u"###### In Test 2 ######") |
65 | | - time.sleep(SLEEP) |
66 | | - print(u"Setting up") |
67 | | - time.sleep(SLEEP) |
68 | | - print(u"Running test") |
69 | | - time.sleep(SLEEP) |
70 | | - print(u"Tearing down") |
71 | | - time.sleep(SLEEP) |
72 | | - print(u"Test Finished\n") |
73 | | - |
74 | | - |
75 | | -class TC3: |
76 | | - def run(self): |
77 | | - print(u"###### In Test 3 ######") |
78 | | - time.sleep(SLEEP) |
79 | | - print(u"Setting up") |
80 | | - time.sleep(SLEEP) |
81 | | - print(u"Running test") |
82 | | - time.sleep(SLEEP) |
83 | | - print(u"Tearing down") |
84 | | - time.sleep(SLEEP) |
85 | | - print(u"Test Finished\n") |
86 | | - |
87 | | - |
88 | | -# Facade |
89 | | -class TestRunner: |
| 35 | + |
| 36 | + |
| 37 | +# Complex computer parts |
| 38 | +class CPU(object): |
| 39 | + """ |
| 40 | + Simple CPU representation. |
| 41 | + """ |
| 42 | + def freeze(self): |
| 43 | + print("Freezing processor.") |
| 44 | + |
| 45 | + def jump(self, position): |
| 46 | + print("Jumping to:", position) |
| 47 | + |
| 48 | + def execute(self): |
| 49 | + print("Executing.") |
| 50 | + |
| 51 | + |
| 52 | +class Memory(object): |
| 53 | + """ |
| 54 | + Simple memory representation. |
| 55 | + """ |
| 56 | + def load(self, position, data): |
| 57 | + print("Loading from {0} data: '{1}'.".format(position, data)) |
| 58 | + |
| 59 | + |
| 60 | +class SolidStateDrive(object): |
| 61 | + """ |
| 62 | + Simple solid state drive representation. |
| 63 | + """ |
| 64 | + def read(self, lba, size): |
| 65 | + return "Some data from sector {0} with size {1}".format(lba, size) |
| 66 | + |
| 67 | + |
| 68 | +class ComputerFacade(object): |
| 69 | + """ |
| 70 | + Represents a facade for various computer parts. |
| 71 | + """ |
90 | 72 | def __init__(self): |
91 | | - self.tc1 = TC1() |
92 | | - self.tc2 = TC2() |
93 | | - self.tc3 = TC3() |
94 | | - self.tests = [self.tc1, self.tc2, self.tc3] |
95 | | - |
96 | | - def runAll(self): |
97 | | - [i.run() for i in self.tests] |
98 | | - |
99 | | - |
100 | | -# Client |
101 | | -if __name__ == '__main__': |
102 | | - testrunner = TestRunner() |
103 | | - testrunner.runAll() |
104 | | - |
105 | | -### OUTPUT ### |
106 | | -# ###### In Test 1 ###### |
107 | | -# Setting up |
108 | | -# Running test |
109 | | -# Tearing down |
110 | | -# Test Finished |
111 | | -# |
112 | | -# ###### In Test 2 ###### |
113 | | -# Setting up |
114 | | -# Running test |
115 | | -# Tearing down |
116 | | -# Test Finished |
117 | | -# |
118 | | -# ###### In Test 3 ###### |
119 | | -# Setting up |
120 | | -# Running test |
121 | | -# Tearing down |
122 | | -# Test Finished |
123 | | -# |
| 73 | + self.cpu = CPU() |
| 74 | + self.memory = Memory() |
| 75 | + self.ssd = SolidStateDrive() |
| 76 | + |
| 77 | + def start(self): |
| 78 | + self.cpu.freeze() |
| 79 | + self.memory.load("0x00", self.ssd.read("100", "1024")) |
| 80 | + self.cpu.jump("0x00") |
| 81 | + self.cpu.execute() |
| 82 | + |
| 83 | + |
| 84 | +def main(): |
| 85 | + """ |
| 86 | + >>> computer_facade = ComputerFacade() |
| 87 | + >>> computer_facade.start() |
| 88 | + Freezing processor. |
| 89 | + Loading from 0x00 data: 'Some data from sector 100 with size 1024'. |
| 90 | + Jumping to: 0x00 |
| 91 | + Executing. |
| 92 | + """ |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + import doctest |
| 97 | + doctest.testmod(optionflags=doctest.ELLIPSIS) |
0 commit comments