The example explains the proxy mode and abstract factory mode of Python Design Mode Programming, and the python design mode.

Source: Internet
Author: User

The example explains the proxy mode and abstract factory mode of Python Design Mode Programming, 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.

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 ()

Abstract Factory Model
Unlike simple factory/factory methods, abstract factory may be better understood. For example:
For example, two animals, cats and dogs, all of which have speak and eat functions, but obviously they have different results with a 'factory '(a type in an abstract factory is a factory, which is different from other models) specifically, it helps us find the corresponding animal and perform the correct operation. There is a class/function that can help us find the factory above through parameters. This is the abstract factory.

From abc import ABCMetaclass StandardFactory (object): ''' this is the abstract factory ''' @ staticmethod def get_factory (factory ): ''' locate the factory for the actual operation based on the parameter ''' if factory = 'cat': return CatFactory () elif factory = 'Dog': return DogFactory () raise TypeError ('unknown Factory. ') Here we help the dog product class find the factory class DogFactory (object): def get_pet (self): return Dog (); class CatFactory (object ): # note that this method is the same as the above name, but the returned class is different. This is the role of the factory def get_pet (self): return Cat (); # You can think that dog and cat are both animal types. You can have a base class Pet (object): # ABCMeta will allow this class to add many basic abstract base classes after registration, see [ABCMeta] (http://docs.python.org/2/library/abc.html#abc.ABCMeta) _ metaclass _ = ABCMeta def eat (self): pass # Dog what should be done here class Dog (Pet): def eat (self): return 'dog food... 'class Cat (Pet): # Here the eat is still the same name, they are all the same operation, but return different def eat (self): return 'cat food... 'If _ name _ = "_ main _": factory = StandardFactory. get_factory ('cat') pet = factory. get_pet () print pet. eat () # note that you only need to modify the parameter passed in by the abstract factory. You do not need to change factory = StandardFactory for anything else. get_factory ('dog ') pet = factory. get_pet () print pet. eat ()

Articles you may be interested in:
  • 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

Related Article

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.