Python Design Patterns " appearance patterns " Example Tutorials

Source: Internet
Author: User
The appearance pattern of design patterns in Python advocates less coupling with multiple modules for code management, and is illustrated with examples below.

Application Features:

When many complex and small functions need to invoke requirements, and these calls often have a certain relevance, that is, a call is a series of.

Structural characteristics:

The original complex and numerous calls, planning unified into an entry class, from this only through the entrance call.

Example 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 ', ' instance '  def delete (self):    print ' Delete module '-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 ()


A bit similar to the proxy mode, the difference is that the appearance mode not only proxies the function of each module of a subsystem, at the same time standing in the subsystem angle, by combining the functions of each module of the subsystem, provide more high-level interfaces to the outside, thus more semantically meet the needs of the subsystem level.

With the continuous expansion of system functions, when the system needs to be divided into subsystems or sub-modules, in order to reduce the coupling, reduce the complexity of system code, improve maintainability, the proxy mode is usually useful.

Let's look at one more 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两函数, function is displayed, you can make the original complex function call relationship clear, unified     Self.__small2.do_small2 ()         def method2 (self):     self.__small2.do_ Small2 ()     self.__small3.do_small3 ()   if __name__ = = ' __main__ ':   OSD = Outside ()   osd.method1 ()   osd.method2 ()

Results:

Do small 1 does small 2 do small 2 do small 3
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.