Python combination mode and responsibility chain mode programming example, python example

Source: Internet
Author: User

Python combination mode and responsibility chain mode programming example, python example

Combination Mode
We regard the Composite mode as a complex attribute structure. In fact, there are basically three roles: trunk (defining some operations for leaf) and branches (many branches on the trunk) and the leaf (the object that the trunk wants to operate on), the Composite mode helps us implement: that is, they are easy to act as other objects, so as to provide consistency.

Python example

Class Trunk (object): '''trunk ''' def _ str _ (self): pass def subtree (self): passclass Composite (Trunk ): def _ init _ (self, left = None, right = None, length = None): self. left = left self. right = right self. length = length def _ str _ (self): # This result returns if self when subtree () is called. length: return "(" + self. left. _ str _ () + "," + self. right. _ str _ () + ")" + ":" + str (self. length) else: return "(" + self. left. _ str _ () + "," + self. right. _ str _ () + ")" # This is actually a technique to return objects of the next level through this function, that is, it is both an object and a container def subtree (self) of an object: return Composite (self. left, self. right) class Leaf (Trunk): '''leaf class. It cannot extend '''def _ init _ (self, name, length = None): self. name = name self. length = length self. left = None self. right = None def _ str _ (self): return self. name + ":" + str (self. length) def subtree (self): return Leaf (self. name, self. length) if _ name _ = "_ main __": # If only the leaves exist, the assembled result t1 = Leaf ('A', 0.71399) of _ str _ is directly returned. print t1 # There is A combination of two leaves, the returned result is a combination of two Leaf objects t2 = Composite (Leaf ('B',-0.00804), Leaf ('C', 0.07470 )) print t2 # This is A combination of nested leaves with branches on the trunk and Leaf t3 = Composite (Leaf ('A', 0.71399), Composite (Leaf ('B ', -0.00804), Leaf ('C', 0.07470), 0.1533), 0.0666) print t3 # locate the corresponding Leaf object through the left and right nodes using t4 = t3.right. right. subtree () print t4 # The left Tree of t3 is actually the leaf object t5 = t3.left. subtree () print t5


Responsibility Chain Model
For example, when we are still studying, the scores for the exam are several grades, for example, 90-points and 80-90 points. Well, I 'd like to provide a feedback to print your score based on the score, for example, 90-100 Is A +, 80-90 is A, and 70-80 is B +... Of course, you can implement it in many ways. Here I will implement a Chain mode: A series of classes are used for response, but only classes suitable for processing will be processed, similar to case and switch Functions

Python example

Class BaseHandler: # It plays the role of chain def successor (self, successor): self. successor = successorclass ScoreHandler1 (BaseHandler): def handle (self, request): if request> 90 and request <= 100: return "A +" else: # otherwise, it is passed to the next chain, the same below, but I want to return the return self of the result. successor. handle (request) class ScoreHandler2 (BaseHandler): def handle (self, request): if request> 80 and request <= 90: return "A" else: return self. successor. handle (request) class ScoreHandler3 (BaseHandler): def handle (self, request): if request> 70 and request <= 80: return "B +" else: return "unsatisfactory result" class Client: def _ init _ (self): h1 = ScoreHandler1 () h2 = ScoreHandler2 () h3 = ScoreHandler3 () # Pay attention to this order, h3 contains something similar to the default result, which should be placed at the end. Other sequences do not matter, such as h1 and h2 h1.successor (h2) h2.successor (h3) requests = {'hangsan ': 78, 'lisi': 98, 'hangzhou': 82, 'shanghai': 60} for name, score in requests. iteritems (): print '{}is {}'. format (name, h1.handle (score) if _ name __= = "_ main _": client = Client ()

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.