This article mainly introduces the proxy mode and abstract factory mode of Python programming. Two simple small examples are given to illustrate the embodiment of the ideas of these two design modes in Python programming, for more information, see
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 # What the Dog should do here is 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 ()