Home > Backend Development > Python Tutorial > Python design pattern 'appearance pattern' example explanation

Python design pattern 'appearance pattern' example explanation

高洛峰
Release: 2016-10-17 16:38:17
Original
1238 people have browsed it

The appearance pattern of design patterns in Python advocates dividing code into multiple modules to reduce coupling. The following uses examples to illustrate.

Application features:

When many complex and small functions need to be called, and these calls are often related to a certain extent, that is, one call is a series.

Structural features:

Unify the original complex and numerous calls into one entry class. From now on, you can only call through this one entry.

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()
Copy after login


is somewhat similar to the proxy mode. The difference is that the appearance mode not only proxy the functions of each module of a subsystem, but also from the perspective of the subsystem, by combining the subsystems The functions of each module provide higher-level interfaces to the outside world, thereby semantically satisfying the needs of the subsystem level.

With the continuous expansion of system functions, when the system needs to be divided into multiple subsystems or sub-modules to reduce coupling, reduce system code complexity, and improve maintainability, the agent mode usually comes into play.

Let’s 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()  ##如果这里调用的不只2两函数,作用就显示出来了,可以把原本复杂的函数调用关系清楚化,统一化 
    self.__small2.do_small2() 
      
  def method2(self): 
    self.__small2.do_small2() 
    self.__small3.do_small3() 
  
if __name__ == '__main__': 
  osd = outside() 
  osd.method1() 
  osd.method2()
Copy after login

Result:

do small 1 
do small 2 
do small 2 
do small 3
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template