For example, the Null mode and Bridge Mode Programming in Python, pythonnull

Source: Internet
Author: User

For example, the Null mode and Bridge Mode Programming in Python, pythonnull

Null Mode
I think everyone has an experience. In order to obtain a certain attribute, but sometimes the attribute is None, you need to handle exceptions. If you want to save code for such conditional filtering, the Null mode can be used to reduce whether the object is set to None.

Python example
Let me give you an example that is not very common, just to let everyone understand this mode: I have many classes, but not every class has the class method test, so I need to handle exceptions when calling class methods.

Class A (object): passclass B (object): B = 1 @ classmethod def test (cls): print cls. bdef get_test (x): try: return x. test failed t AttributeError: return None # I only wrote two classes here, but there are actually many classes for I in [A, B]: test = get_test (I) # I need to determine whether this class method is obtained in order to determine whether if test: test () can be executed ()

But I can use the Null method.

Class Null (object): def _ init _ (self, * args, ** kwargs): "ignore parameter" return None def _ call _ (self, * args, ** kwargs): "ignoring instance calls" return self def _ getattr _ (self, mname ): "retrieve" return self def _ setattr _ (self, name, value) for ignoring properties "return self def _ delattr _ (self, name): ''' ignore the delete attribute operation ''' return self def _ repr _ (self): return "<Null>" def _ str _ (self ): return "Null"

Or the above functions

Class Null (object): def _ init _ (self, * args, ** kwargs): "ignore parameter" return None def _ call _ (self, * args, ** kwargs): "ignoring instance calls" return self def _ getattr _ (self, mname ): "retrieve" return self def _ setattr _ (self, name, value) for ignoring properties "return self def _ delattr _ (self, name): ''' ignore the delete attribute operation ''' return self def _ repr _ (self): return "<Null>" def _ str _ (self ): return "Null"

Bridging Mode
This mode separates the implementation of the product class from the abstract class and can be flexibly changed. If you remember the state mode, it modifies the internal attributes, and the bridge mode specifies the internal attributes, each product class specifies that this attribute is called by the bridge mode class, which is suitable for product classes that may change frequently. This reduces the coupling between product classes.

Python example
The following shows how to print the operating system name.

Class Bridge (object): def _ init _ (self): self. _ implementation = None def someFunctionality (self): raise NotImplemented () class UseCase1 (Bridge): # The product class def _ init _ (self, implementation) implemented based on the initialization parameter): self. _ implementation = implementation # print the result def someFunctionality (self): print "UseCase1:", self. _ implementation. anotherFunctionality () class UseCase2 (Bridge): def _ init _ (self, implemen Tation): self. _ implementation = implementation def someFunctionality (self): print "UseCase2:", self. _ implementation. anotherFunctionality () class ImplementationInterface: def anotherFunctionality (self): raise NotImplemented # This is actually the product class Linux (ImplementationInterface): # It defines this method, responding to OS name def anotherFunctionality (self): print "Linux! "Class Windows (ImplementationInterface): def anotherFunctionality (self): print" Windows. "def main (): linux = Linux () windows = Windows () useCase = UseCase1 (linux) useCase. someFunctionality () useCase = UseCase1 (windows) useCase. someFunctionality () useCase = UseCase2 (linux) useCase. someFunctionality () useCase = UseCase2 (windows) useCase. someFunctionality () if _ name _ = "_ main _": main ()

Articles you may be interested in:
  • Introduction to the proxy mode and template method Mode Programming in Python Design Mode
  • 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

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.