Application Features:
When many complex and small functions require calling requirements, these calls often have a certain degree of correlation, that is, one call is a series.
Structural characteristics:
Plan and unify the originally complex and numerous calls into an entry class, and then you can only call through this entry from now on.
Code structure example:
class ModuleOne(object): def Create(self): print 'create module one instance' def Delete(self): print 'delete module one instance' class ModuleTwo(object): def Create(self): print 'create module two instance' def Delete(self): print 'delete module two instance' class Facade(object): def __init__(self): self.module_one = ModuleOne() self.module_two = ModuleTwo() def create_module_one(self): self.module_one.Create() def create_module_two(self): self.module_two.Create() def create_both(self): self.module_one.Create() self.module_two.Create() def delete_module_one(self): self.module_one.Delete() def delete_module_two(self): self.module_two.Delete() def delete_both(self): self.module_one.Delete() self.module_two.Delete()
It is a bit similar to the proxy mode. The difference is that the appearance mode not only proxies the functions of each module of a subsystem, but also stands from the perspective of the subsystem and combines the functions of each module of the subsystem to provide a higher-level interface to the outside world, thereby semantically meeting the needs of the subsystem level.
As the system functions continue to expand, when the system needs to be divided into multiple subsystems or submodules to reduce coupling, reduce system code complexity, and improve maintainability, the proxy mode usually has its own advantages.
Let’s take a look at another example:
class small_or_piece1: def __init__(self): pass def do_small1(self): print 'do small 1' class small_or_piece_2: def __init__(self): pass def do_small2(self): print 'do small 2' class small_or_piece_3: def __init__(self): pass def do_small3(self): print 'do small 3' class outside: def __init__(self): self.__small1 = small_or_piece1() self.__small2 = small_or_piece_2() self.__small3 = small_or_piece_3() def method1(self): self.__small1.do_small1() ##If there are more than two functions called here, the function will be displayed. You can clear the original complex function call relationship and unify self.__small2.do_small2() def method2(self): self.__small2.do_small2() self.__small3.do_small3() if __name__ == '__main__': osd = outside() osd.method1() osd.method2()
result:
do small 1 do small 2 do small 2 do small 3