An example of the proxy mode and abstract factory pattern for Python design pattern programming

Source: Internet
Author: User
Proxy mode
Proxy mode is a common design pattern that is used primarily to provide a "proxy" access to an object (such as a) through an object (such as B). For example, an object is inconvenient for direct reference, and the agent is mediating between the object and the visitor.

You first imagine: an object to provide RGB three color values, I want to get an object of RGB three colors, but I do not want you to get the blue attribute, how to do?

Class Proxy (object):  def __init__ (self, subject):    self.__subject = Subject  # Proxy is essentially the attribute's 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:    return self.__green  def Blue (self):    return Self.__blueclass noblueproxy (Proxy):  # I intercepted Blue's access in this sub-proxy class so that it does not return the blue attribute of the proxy class  def blue (self):    return 0if __name__ = = ' __main__ ':  RGB = RGB (192, +)  print RGB. Red ()  proxy = proxy (RGB)  print proxy. Green ()  noblue = Noblueproxy (RGB)  print Noblue. Green ()  print noblue. Blue ()

Abstract Factory mode
Unlike simple factory/factory methods, abstract factories may best understand, for example:
like 2 animals, cats and dogs, they all have speak and eat function, But it's obvious that the results are different. There is a ' factory ' (a type in the abstract factory is a factory, which is different from other models) specifically to help us find the right animal for the correct operation there is a class/function that can be used to help us find this factory. This is the abstract factory

From ABC import abcmetaclass standardfactory (object): ' This is the Abstract factory ' @staticmethod def get_factory (factory): ' ' according to the reference Number found on the actual operation of the factory ' if factory = = ' Cat ': Return catfactory () elif factory = = ' dog ': Return dogfactory () Rais E TypeError (' Unknown Factory. ') Here to help Dog this product class to find the properties of the factory class Dogfactory (object): Def get_pet (self): return Dog (), Class Catfactory (object): # Note This method and the above The name is the same, but the returned class is different, this is the function of the factory def Get_pet (self): return Cat (); # You can think of dog and Cat as a kind of animal, there can be a base class pet (object): # Abcmeta will let this class After registering to add a lot of basic abstract base classes, you can see [Abcmeta] (HTTP://DOCS.PYTHON.ORG/2/LIBRARY/ABC.HTML#ABC. Abcmeta) __metaclass__ = Abcmeta def eat (self): What pass# dog should do is 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, just return the different def eat (self): return ' Cat food ... ' if __name__ = = ' __main__ ': Fact Ory = Standardfactory.get_factory (' cat ') pet = Factory.get_pet () print pet.eat () # Note here, you just need to change the parameter that the abstract factory passed in, and nothing else. FAC Tory = Standardfactory.get_factory (' dog ') Pet = Factory.get_pet () print pet.eat () 
  • 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.