Skip to content

Commit 90b5596

Browse files
committed
faif#184 - Added description of the Facade pattern.
1 parent 2ab4236 commit 90b5596

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

structural/facade.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,42 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3+
4+
"""
5+
*What is this pattern about?
6+
The Facade pattern is a way to provide a simpler unified interface to
7+
a more complex system. It provides an easier way to access functions
8+
of the underlying system by providing a single entry point.
9+
This kind of abstraction is seen in many real life situations. For
10+
example, we can turn on a computer by just pressing a button, but in
11+
fact there are many procedures and operations done when that happens
12+
(e.g., loading programs from disk to memory). In this case, the button
13+
serves as an unified interface to all the underlying procedures to
14+
turn on a computer.
15+
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+
*Where is the pattern used practically?
28+
This pattern can be seen in the Python standard library when we use
29+
the isdir function. Although a user simply uses this function to know
30+
whether a path refers to a directory, the system makes a few
31+
operations and calls other modules (e.g., os.stat) to give the result.
32+
33+
*References:
34+
https://sourcemaking.com/design_patterns/facade
35+
https://fkromer.github.io/python-pattern-references/design/#facade
36+
http://python-3-patterns-idioms-test.readthedocs.io/en/latest/ChangeInterface.html#facade
37+
"""
38+
39+
340
from __future__ import print_function
441
import time
542

0 commit comments

Comments
 (0)