Introduction to the proxy mode and template method Mode Programming in the Python design mode, and the python Design Mode

Source: Internet
Author: User

Introduction to the proxy mode and template method Mode Programming in the Python design mode, and the python Design Mode

Proxy Mode
The Proxy mode is A common design mode. It is mainly used to provide A 'agent' access to an object (such as B) (such as A) through an object (such as. For example, if an object is not directly referenced, the proxy acts as an intermediary between the object and the visitor.

Python example
Imagine that an object provides three color values: rgb. I want to get three colors of an object, but I don't want you to get the blue attribute. What should I do?

Class Proxy (object): def _ init _ (self, subject): self. _ subject = subject # The proxy is essentially the attribute delegate def _ getattr _ (self, name): return getattr (self. _ subject, name) class RGB: def _ init _ (self, red, green, blue): self. _ red = red self. _ green = green self. _ blue = blue def Red (self): return self. _ red def Green (self): return self. _ green def Blue (self): return self. _ blueclass NoBlueProxy (Proxy): # I intercepted blue access in this sub-Proxy class, so that the Blue attribute def Blue (self) of the class to be Proxy will not be returned ): return 0if _ name _ = '_ main _': rgb = RGB (100,192,240) print rgb. red () proxy = Proxy (rgb) print proxy. green () noblue = NoBlueProxy (rgb) print noblue. green () print noblue. blue ()


Template Method Mode
I don't know if you have noticed that we have implemented a service function and different details are implemented on different objects. If we say the rule mode, the rule mode encapsulates the logic in a class (the Duck mentioned in the article) and then uses the delegate method to solve the problem. The method mode of the template is to abstract the unchanged framework and define the interface for details to be passed in. the public behaviors of various product classes will be raised to the public parent class, and all the variables are included in the product subclass.

Python example

# Ingredients = "spam eggs apple" line = '-' * 10 # This is the basic function def iter_elements (getter, action): "circular processing skeleton" # getter is the data to be iterated, and action is the function to be executed for element in getter (): action (element) print (line) def rev_elements (getter, action): "reverse" for element in getter () [:-1]: action (element) print (line) # after the data is processed by the function, def get_list (): return ingredients is finally passed to the template. split () # Same as def get_lists (): return [list (x) for x in ingredients. split ()] # data operation def print_item (item): print (item) # reverse processing data def reverse_item (item): print (item [:-1]) # template function def make_template (skeleton, getter, action): # It abstracts the input of skeleton, data, and subclass operation function def template (): skeleton (getter, action) return template # list parsing. Data is the first two types of skeleton (how to define iteration), two data splitting functions, and the combination of positive and reverse data printing templates = [make_template (s, g, a) for g in (get_list, get_lists) for a in (print_item, reverse_item) for s in (iter_elements, rev_elements)] # execute for template in templates: template ()

Articles you may be interested in:
  • Example of memorandum mode and object Pool Mode in Python Design Mode Programming
  • An example is provided to illustrate the visitor and observer modes in Python Design Mode Programming.
  • An example is provided to illustrate the proxy mode and abstract factory mode of Python Design Mode Programming.
  • Getting started with programming in Python Design Patterns
  • Python design mode-singleton mode instance
  • Example of observer mode in Python Design Mode
  • Example of proxy mode in Python Design Mode
  • Python combination mode and responsibility chain mode programming example

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.