An example introduction to 'Appearance Pattern' for learning Python design patterns

高洛峰
Release: 2017-03-19 15:16:50
Original
1248 people have browsed it

PythonDesign PatternAppearance Pattern 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, and 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 Agent pattern, the difference is that the appearance pattern not only represents a subsystem At the same time, from the perspective of the subsystem, by combining the functions of each module of the subsystem, a higher-level interface is provided 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 submodules 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


The above is the detailed content of An example introduction to 'Appearance Pattern' for learning Python design patterns. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!