Summary of design patterns-behavior patterns

Source: Internet
Author: User

Category:

After learning about such a multi-mode model, many behavior patterns have also been designed during the learning process. What are the behavior patterns? One by one.

Iterator Mode Http://blog.csdn.net/huo065000/article/details/24352437
Intermediary Mode Http://blog.csdn.net/huo065000/article/details/22856883
Memorandum Mode Http://blog.csdn.net/huo065000/article/details/24233429
Observer Mode Http://blog.csdn.net/huo065000/article/details/22933081
Status Mode Http://blog.csdn.net/huo065000/article/details/23021919
Rule Mode Http://blog.csdn.net/huo065000/article/details/21286775
Template Method Mode Below
Visitor Mode Below
Responsibility connection mode Below
Command mode Http://blog.csdn.net/huo065000/article/details/24587949

There are so many behaviors, what is the behavior pattern?

Definition:

The behavior pattern is the abstraction of division of responsibilities and algorithms between different objects. The behavior pattern is not just aboutClass and ObjectAnd is aboutInteraction. Can be dividedClass Behavior PatternAndBehavior patterns of Objects.

Behavior mode of the class: The behavior mode of the class uses the inheritance relationship to allocate behavior objects among several classes. The behavior mode uses the object aggregation to allocate the behavior template method mode: we can see that this mode is the most common mode! Recently, I am nearing graduation. I like my colleagues who work outside China to come back to visit me, because they can invite me to a big dinner! For meals, there is also a fixed pattern. Click order-eat-pay. That's a simple process! The key lies in the ordering of orders. the ordering process is repeated. All repeated code can be raised to the parent class, rather than making every order repeat. This is the so-called imitation method model. Definition: defines the skeleton of an algorithm in an operation, and delays some steps to the subclass. The template method allows the subclass to redefine certain steps of the Algorithm without changing the structure of an algorithm. Structure:
Basic code: AbstractClass is an abstract class, which is actually an abstract template. It defines and implements a template method.
Abstract class AbstractClass {public abstract void PrimitOperation1 (); // some abstract behaviors are put into the subclass to implement public abstract void PrimitOperation2 (); public void TemplateMethod () // template method, the logic skeleton is given, and the logical composition is some corresponding abstract operations. They are all postponed to the subclass implementation {PrimitOperation1 (); PrimitOperation2 (); Console. writeLine ("");}
ConcreteClass implements one or more abstract methods defined by the parent class. Each AbstractClass can have any number of concreteclasses corresponding to it, and each ConcreteClass can provide different implementations of these abstract methods, so that the implementation of the top-level logic is different.
Class ConcreteClassA: AbstractClass {public override void PrimitOperation1 () {Console. writeLine ("Implementation of specific Class A method 1"); // different method implementation from ConcreteClassB} public override void PrimitOperation2 () {Console. writeLine ("Implementation of Class A method 2 ");}}
Client call:
static void Main(string[] args)        {            AbstractClass c;            c = new ConcreteClassA();            c.TemplateMethod();            Console.Read();        }
Responsibility connection mode: "Why don't I raise my salary if I have been working for so long ?" Said with a full tone of complaints. "I want to pay you a long salary too! I have no right to watch you do your best every day !" The manager told me with patience. "If you ask for leave for a day or two for the reason, I have the right to handle it. However, after all, the financial power is out of our hands! Lack of strength ......" The manager added. As a matter of fact, I also know that the manager is good, so I thought this was just so bad, but one day the manager said that the general manager agreed to pay you a higher salary, and your performance in the company was well known. Of course you will be happy! When you hand over what you cannot do to the next person, there will always be one person with the right to solve the problem. This is the so-called responsibility connection model. Structure:
So that multiple objects have the opportunity to process requests, so as to avoid coupling between request senders and recipients. Connect the object to a chain and pass the request along the chain until an object processes it.
Client code to submit a request to a specific handler object on the chain
Static void Main (string [] args) {Handler h1 = new ConcreteHandler1 (); // h2 and h3 ...... H1.SetSuccessor (h2); // set the responsibility chain to go home and next to the int [] requests = {2, 5, 12, 17}; foreach (int request int requests) // submit the request to the minimum handler cyclically. Different amounts are processed by different permission handlers {h1. handlerRequest (request);} Console. read ();}
Handler class, processing an interface for Request Handling
Abstract class Handler {protected Handler successor; public void SetSuccessor (Handler successor) // set successor {this. successor = successor;} public abstract void HandlerRequest (int request); // The abstract method for processing the request}
Specific handler object class
Class ConcreteHandler1: Handler {public override void HandlerRequest (int request) {if (request> = 0 & request <10) // 0 to 10 process this request {Console. writeLine ("{0} Processing request {1}", this. getType (). name, request);} else if (successor! = Null) {successor. HandlerRequest (request); // transfer to the next position }}
Benefit: The main purpose of this mode is to keep requests uninterrupted, And I can add or modify the structure of a request at any time. This enhances the flexibility of assigning roles to objects. Of course, if a request may not be processed at the end of the chain, or cannot be processed because of incorrect configuration, this is very bad, so you must consider it comprehensively when using it. Visitor mode: I like to go to the supermarket for a small stroll when I'm okay. I just want to find a reason to reward myself, although there is nothing to do with it! But this is a small achievement! At least I understand a complicated design model when talking about shopping this time! In the above example, if there are too many visitors or too many purchased items, the abstract method becomes unstable. The visitor mode frees the coupling between the data structure and the operations acting on the structure, allowing the operation set to evolve relatively freely. Objective: To separate processing from the data structure. The premise is that there is a stable data structure! The advantage of using this mode is that it is easy to add new operations, because adding new operations means adding a new visitor. The visitor mode aggregates relevant behaviors into a visitor object.


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.