Detailed description of design patterns

Source: Internet
Author: User
Definition

Design
Pattern. The design pattern is used to make code reusable, make it easier for others to understand, and ensure
Code reliability. There is no doubt that the design model is a win-win solution for others and the system. The design model enables code compilation to be truly engineered. The design model is the foundation of software engineering, just like the building blocks.

Image metaphor of design patterns

1. Abstract Factory-try to catch up with mm. The chicken wings of McDonald's and the chicken wings of KFC are what mm loves. Although the taste is different, whether you take mm to McDonald's or KFC, just say "four chicken wings" to the waiter. McDonald's and KFC are factory for chicken wings production.
  

Factory Model
: The customer class and the factory class are separated. A consumer needs a product at any time and only requests from the factory. Consumers can accept new products without modification. The disadvantage is that the factory class must be modified when the product is modified. For example, how to create and provide data to clients.
  

2, BUILDER-MM is the most love to hear is "I love you" this sentence, see different places of mm, to be able to use them
The dialect tells her this sentence. I have a multi-language translation machine with a key for each language above. To see mm, I only need to press the corresponding key, it can say "I love you" in the corresponding language.
The external mm can also be easily done, which is my "I love you" builder. (This must be better than the US military's translation server in Iraq)
  

Construction mode: separates the internal representation of an object from the object generation process, so that a building process generates a product object with a different internal representation. The construction mode allows the product's internal appearance to change independently, so that the customer does not have to know the details of the product's internal composition. The construction model enforces a step-by-step construction process.
  

3. Factory method-ask mm to go to McDonald's for a hamburger. Different mm has different tastes. It is annoying to remember every one. I usually use the factory method mode, take the mm to the waiter and say "I want a hamburger". What kind of hamburger should I ask the mm to tell the waiter directly.
  

Factory method mode: the Core factory category is no longer responsible for the creation of all products. Instead, it submits the specific creation work to the subclass to become an abstract factory role, it is only responsible for providing the interface that must be implemented by a specific factory class, without touching the details of which product class should be instantiated.
  

4. Prototype-chat with MM via QQ, so I must talk about some affectionate words. I have collected a lot of emotional words, and I just need to copy the words and put them in QQ, this is my prototype. (100 yuan a copy, do you want)
  

Original Model mode: specify the type of the object to be created by giving a prototype object, and then copy
Create more objects of the same type. The original model mode allows you to dynamically add or remove product classes. The product class does not need to have any predefined level structure. The original model mode applies to any level structure. Missing
Point is that each class must have a clone method.
  

5. Singleton-I have 6 beautiful wives. My husband is me. I am sigleton, our husband. All they need to say is "husband, that's me (I had a dream just now. How can this be a good thing)
  

Singleton mode: Singleton mode ensures that a class has only one instance, and instantiate the instance and provides the singleton mode to the entire system. The Singleton mode can only be used when there is a real "Single Instance" demand.
  

6. adapter-I met Sarah, a beautiful girl from Hong Kong at a friend's party. But I can't speak Cantonese, and she can't speak Mandarin. I have to turn to my friend Kent for help, as an adapter between me and Sarah, Sarah and I can talk to each other (I don't know if he will play with me)
  

Adapter (transformer) mode: converts an interface of a class into another interface that the client expects, so that the two classes that cannot work together due to interface mismatch can work together. The adaptation class can return a suitable instance to the client based on the parameters.
  

7. Bridge: mm in the morning. Good morning. mm in the evening. Good evening. mm in new clothes. Nice clothes. New hair styles, say your hair looks pretty. Don't ask me the question "How to say mm has a new hairstyle in the morning". Just use bridge to combine it.
  

Bridge Mode: Abstraction and implementation are decoupled so that the two can change independently, that is, the strong association between them becomes weak Association, that is, the combination/aggregation relationship is used between the abstraction and implementation of a software system, rather than the inheritance relationship, so that the two can change independently.
  

8. Composite-Mary has a birthday today. "You want to give me a gift for my birthday ." "Well, okay,
Go to the store and pick it up ." "This T-shirt is very beautiful. Buy it. This dress looks good. Buy it. This bag is also good. Buy it ." "Hey, I bought three items. I only promised to give one gift ." "What, T-shirt and skirt
Add a bag, just like a set, miss. Please pack it. "……", Mm will all use the composite mode. Will you?
  

Merging mode: the merging mode organizes objects into the tree structure and can be used to describe the relationship between the whole and the part. The merging mode is a tree structure mode for processing objects. The merging mode shows the relationship between the part and the whole in a tree structure. The merging mode allows the client to view individual component objects in the same way as the compositing objects composed of them.
  

9. After decorator-Mary's birthday ends, do not ask her to pick it up. Otherwise
The monthly food fee is definitely over. I took out the photo I took on the top of Huashan last year and wrote "the best gift is your FITA" on the back ", I went to the street gift shop and bought a photo box (the mm for selling gifts is also very beautiful ).
Oh). Then Mike, who is next door to art design, designs a beautiful box and installs it ......, We are all decorator, and we are all modifying this person. How do you understand this?
  

Decoration mode: The decoration mode extends the object functions in a transparent way to the client. It is an alternative to the inheritance relationship and provides more flexibility than inheritance. Dynamically adds features to an object, which can be dynamically revoked. A large number of functions are generated by the arrangement and combination of some basic functions.
  

10. Facade-I have a professional Nikon camera. I like to adjust the aperture and shutter manually.
Only professional photos come from here, but mm doesn't understand this. It won't take a long time to teach. Fortunately, the camera has a facade design mode, which allows you to adjust the camera to the automatic file. You only need to align the camera with the target and press the shutter. Everything is automatically adjusted by the camera.
Mm can also use this camera to take a picture for me.
  

Facade mode: external communication with a sub-system must be performed through a unified facade object. The facade mode provides a high-level interface to make subsystems easier to use. Each subsystem has only one portal class, and this portal class has only one instance, that is, it is a singleton mode. However, the entire system can have multiple portals.
  

11. flyweight-I send text messages to mm every day, and my fingers are exhausted. I recently bought a new mobile phone and can
The sentences used are stored in the mobile phone. If you want to use them, you can simply take them out and add the MM name in front of them to send them. You don't need to write a word at all. The shared sentence is flyweight, the name of mm.
Words are the external features extracted and used according to the context.
  

Enjoy mode: flyweight is the most lightweight in boxing. The meta-mode supports a large number
Fine-grained object. The key to the sharing mode is to distinguish between the internal and external states. The internal status is stored in the shared element and will not change with the environment. The external status changes as the environment changes.
. The external status cannot affect the internal status. They are independent of each other. The States that can be shared and those that cannot be shared are separated from the conventional classes to remove the States that cannot be shared from the classes. Client does not
You can directly create shared objects. Instead, you should use a factory object to create shared objects. The metadata mode greatly reduces the number of objects in the memory.
  

12. Proxy-chatting with mm on the Internet always starts with "hi, hello". "Where are you from ?" "How old are you ?" "How tall ?" These words are really annoying. Write a program as my proxy. When I receive these words, I have set up an automatic answer. When I receive other words, I will be notified of the answer, cool.
  

Proxy mode: the proxy mode provides a proxy object for an object and controls the reference to the source object. Proxy is
One person or institution acts on behalf of another person or institution. In some cases, the customer does not want to or cannot directly reference an object. The proxy object can serve as an intermediary between the customer and the target object.
. The client cannot identify the proxy topic object and the real topic object. The proxy mode does not know the real proxy object, but only holds an interface of the proxy object. At this time, the proxy object cannot be created
Object, the proxy object must have other roles created and passed in on behalf of the system.
  

13. Chain
Responsibleity-I went to the English class at night. So I had to sit in the last row. Wow, I sat in front of several beautiful mm. I found a piece of paper and wrote "Hi, I can be my girlfriend ."
? If you don't want to, please pass it to the front. The paper is uploaded one by one. Unfortunately, the mm in the first row passed the paper to the teacher. I heard it was an old virgin. Run fast!
Responsibility Chain Mode: In the responsibility chain mode, many objects are connected by the reference of each object to its next home to form a chain. Request in
This chain is passed until an object on the chain decides to process this request. The customer does not know which object on the chain finally processes this request. The system can dynamically reorganize the chain and
Assign responsibility. The handler has two options: Take responsibility or push the responsibility to the next house. A request cannot be accepted by any receiving end object.
  

14. Command-I have a mm family with very strict management and can't meet each other, so I had to use her brother to stay between us.
Send the information. If she gives me any instructions, write a piece of paper for her brother to bring to me. No, her brother sent another command. To thank him, I asked him to eat a bowl of soy sauce:
"I will send command to my sister and three boyfriends at the same time, so it will take you the least effort to invite me to dinner ."
  

Command mode
:
In command mode, a request or operation is encapsulated into an object. The command mode separates the responsibility for issuing commands from the responsibility for executing commands and delegates them to different objects. Command mode: allow the requester and the sender
Independent, so that the requesting party does not have to know the interface of the receiving party, nor how the request is received, whether the operation is executed, when it is executed, and how it is executed. Commands supported by the system
Undo.
  

15. interpreter-I have a "bubble mm Sutra" with various tips on bubble mm, such as the steps to eat western food and the methods to watch movies. When I am dating mm, you only need to create an interpreter and execute it according to the above script.
  

Interpreter mode: Given a language, the interpreter mode can define a representation of its syntax and provide an interpreter at the same time.
The client can use this interpreter to explain sentences in this language. The interpreter mode describes how to explain these statements with a simple syntax. Language mentioned in interpreter Mode
It refers to any combination that any interpreter object can interpret. In the interpreter mode, you need to define a hierarchical structure of a command class that represents grammar, that is, a series of combination rules. Each command object has an interpreter.
To explain the command object. Any permutation and combination of objects in the hierarchical structure of command objects is a language.
  

16. iterator-I fell in love with Mary and proposed to her desperately.
Mary: "If I want to marry you, I have to agree to my conditions"
Me: "I agree with all the conditions. Let's talk about it"
Mary: "I saw the 1 karat diamond"
Me: "I bought, I bought, are there any other products ?"
Mary: "I saw the villa by the lake"
Me: "I bought, I bought, are there any other products ?"
Mary: "I saw that Ferrari sports car"
I sat down in a chair and bit my teeth: "I bought it, I bought it, do you still have it ?"
  

Iteration sub-mode: the iteration sub-mode can sequentially access an element in the aggregation without exposing the internal representation of the aggregation. Multiple objects are clustered in
The aggregation object is a container object that can accommodate a group of objects. The iteration sub-mode encapsulates the iteration logic into an independent sub-object, which is separated from the aggregation itself. Simplified iteration Mode
The aggregation interface. Each clustering object can have one or more iteration sub-objects. The iteration status of each iteration sub-object can be independent of each other. The iteration algorithm can be independent from the clustering role changes.
  

17. The mediator-four mm players played mahjong. It was unclear who should pay for each other. Fortunately, I was there and counted the money based on my chips, if you make money, you can get it from me. If you lose the money, you can pay it to me. Everything is OK. You get a phone number of four mm.
  

Called by mode: The called by mode encapsulates a series of ways in which objects interact, so that these objects do not have to interact explicitly. Thus
So that they can be loosely coupled. When the roles of some objects change, the roles of other objects are not immediately affected. Ensure that these functions can be changed independently of each other. The called-by mode combines many-to-many phases
Interaction is converted to one-to-many interaction. The mediation mode abstracts the behavior and collaboration of objects, and processes the interaction between objects and other objects on a small scale.
  

18. Memento-Remember what you said to mm when you chat with several mm at the same time, or mm will be unhappy if it finds out. Fortunately, I have a memorandum, I copied a copy of what I said to the mm and saved it in the memo so that I can view the previous records at any time.
  

Memorandum mode: A memorandum object is an object used to store snapshots of the internal state of another object. The purpose of the memorandum mode is to catch the state of an object without damaging the encapsulation, and store it externally, in this way, this object can be restored to the stored state when appropriate in the future.
  

19. Observer-do you want to know our company's latest mm intelligence? Join the company's mm intelligence contact list. Tom is responsible for collecting intelligence. The new intelligence he found does not need to notify us one by one and is directly released to the contact list. We serve as subscribers (observers) you can receive the information in time.
  

Observer Mode
: The Observer mode defines a one-to-many dependency, allowing multiple observer objects to listen to a topic object at the same time. When the status of this topic object changes, it notifies all observer objects so that they can automatically update themselves.
  

20. State-when interacting with MM, pay attention to her status. In different states, her behavior may be different.
If you ask her to go to a movie this evening, MM will say
"Something is going on," said mm, who doesn't hate you but doesn't like it. "Okay, but can I bring my colleagues with me ?", If you like your mm, you will say, "What time is it? How about watching a movie ?",
Of course, if you are doing well during a movie, you can also turn the status of mm from dislike to liking.
  

State mode: State mode allows an object to change its behavior when its internal state changes. This object seems to have changed it.
. The state pattern encapsulates the behavior of the studied object in different State objects. Each State object belongs to a subclass of an abstract state class. The intention of the state mode is to put an object in its internal state
When the state changes, its behavior also changes. The State mode creates a subclass of the state class for the possible states of each system. When the system status changes, the system changes the selected subclass.
  

21. Strategy-for dates with different types of MM, different strategies should be used. Some movies are recommended, some are recommended for snacks, and some are recommended for romance at the seaside, the single goal is to get the attention of mm. There are a lot of strategy in my pursuit of mm.
  

Policy mode: the Policy mode encapsulates each algorithm into an independent class with a common interface for a group of algorithms.
They can be replaced with each other. The policy mode allows the algorithm to change without affecting the client. Rule mode separates behavior from environment. The environment class is responsible for maintaining and querying behavior classes.
. Because the algorithm and environment are independent, the increase, decrease, and modification of the algorithm will not affect the environment and the client.
  

22. Template
Method -- have you read the classic article "how to persuade a girl to bed? The steps for girls to go to bed are divided into coincidence, deadlock breaking, pursuit, kissing, foreplay, hands-on, caressing, and going in.
Template method, but each step has different practices for different situations. It depends on your Random Response (specific implementation );
  

Template Method mode: The template method mode prepares an abstract class and implements part of the logic in the form of a specific method and a specific constructor,
Then declare some abstract methods to force the subclass to implement the remaining logic. Different sub-classes can implement these abstract methods in different ways to implement the rest logic differently. First, develop a top-level logical framework,
The logic details are left to specific sub-classes for implementation.
  

23. Visitor-when Valentine's day arrives, each mm will be given a bunch of flowers and a card, but every mm will send flowers
To address her personal characteristics, each card should also be selected based on her own characteristics. If I have a clear idea, I 'd like to ask the florist and the gift shop owner for visitor, let the florist follow the features of mm
Select a bunch of flowers so that the gift shop owner can select a card based on the characteristics of each person. This makes it much easier;
  

Visitor mode: the visitor mode aims to encapsulate operations that are applied to certain data structure elements. Once these operations require
If modified, the data structure that accepts this operation can remain unchanged. The visitor mode is suitable for systems with relatively undetermined data structures. It frees the coupling between the data structure and the operations acting on the structure, allowing operations
A set can evolve freely. The visitor mode makes it easy to add new operations, that is, to add a new visitor class. The visitor mode aggregates relevant behaviors into a visitor object, rather than disperses
To each node class. When using the visitor mode, put as many object browsing logic as possible in the visitor class, rather than in its subclass. The visitor pattern allows you to access the genus through several hierarchical structures
It is a member class of different levels.

 

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.