In Python, the design pattern appearance pattern advocates code management by multiple modules to reduce coupling. The following describes the examples. Application Features: when many complex and small functions need to be called, the appearance mode of the design pattern in Python advocates code management in multiple modules to reduce coupling. the following example is used to describe.
Application features:
When many complex and small functions need to be called, and these calls are often related, that is, one call is a series.
Structural features:
The original complex and numerous calls are planned to be unified into an entry class. from then on, only this entry can be called.
Sample code structure:
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()
The appearance mode is similar to the proxy mode. the appearance mode not only proxy the functions of each module of a sub-system, but also combines the functions of each module of the sub-system, provides higher-level external interfaces to meet the requirements at the subsystem level in terms of semantics.
With the continuous expansion of system functions, proxy mode is usually useful when you need to divide the system into multiple subsystems or sub-modules to reduce coupling, reduce system code complexity, and improve maintainability.
Let's look at an 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 more than two functions are called here, the functions are displayed. you can clarify the original complex function call relationships 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
The above is a detailed description of the "appearance mode" instance for learning the Python design mode. For more information, see other related articles in the first PHP community!