Learn about the "look patterns" example of python design patterns

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:     pass      def do_small1 (self):     print ' do small 1 '    class Small_ Or_piece_2:   def init:     pass      def do_small2 (self):     print ' Do small 2 '    class Small_or_piece_3:   def init:     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两函数, the function is displayed, you can clear the original complex function call relationship, 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 Do small 2 does 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.