23 design patterns of GoF

GoF figurative language in a relaxed mode 23 kinds of java, there is a good inspiration.

Create a schema

1, ABSTRACT FACTORY- chase MM ultimately invited him to dinner, and of course McDonald's and KFC packages are MM favorite things, though taste is different, but whether you go to McDonald's or Kentucky Fried Chicken with MM, the waiter just to say "two B package "on the line. McDonald's and Kentucky Fried Chicken is the B course of Abstract Factory, B package contains burgers, chicken wings and drinks. McDonald's or KFC will be based on the specifications of Package B, let Hamburg Factory, chicken wings Factory, beverage Factory production materials respectively corresponding to B packages.

Abstract factory pattern: separate customer classes and factory classes. Consumers Whenever you need a certain set of collection of products, you can simply request to the abstract factory. Abstract Factory will again plant specific products that meet the specifications of the product set.

2, BUILDER-MM loved to listen to is "I love you", that is, to see different parts of the MM, to be able to use their dialect told her these words Oh, I have a multilingual translator, each language has a button above to see MM I just press the corresponding key, you can use the appropriate language to say "I love you", that is, the foreign MM can be easily fixed, this is what I "I love you" builder. (This must sell than the US military in Iraq with a machine translation of)

Construction mode: The internal representation of the product generation process and the product separated, so that the process of generating a construction product objects having different internal representation. Construction mode allowing for internal representation may vary independently, customers do not know the details of the internal composition of the product. Construction mode can impose construction of a process carried out in steps.

3, FACTORY METHOD- MM please go to McDonald's hamburgers, different MM have different tastes, we should remember that each is an annoying thing, I generally use the Factory Method pattern, with MM to the waiter there, he said, "to a burger ", specific to what kind of burger, let MM directly with the waiter on the line.

Factory Method Pattern: Core factory class is no longer responsible for creating all the products, but will work to create a specific subclass to do to become an abstract factory role, is only responsible to give concrete factory class must implement, without touching a product category such details should be instantiated.

4, PROTOTYPE- with MM chat with QQ, be sure to say some words of affection, and I collected a lot of nauseating love words, when needed as long as the copy inside out into QQ on the line, this is what I love, then the prototype. (100 dollars a copy, do you want)

Original model mode: to specify objects to be created by giving a prototype object type, and then create more of the same type of object with the method of copying this prototype object. Original model mode allows dynamic increase or decrease in product category, product type this need not have any hierarchical structure is determined in advance, the original model model applies to any hierarchical structure. The disadvantage is that each class must be equipped with a cloning method.

5, SINGLETON- I have six beautiful wife, they are my husband, my husband is our home Singleton, they just said "husband", are referring to the same person that I (just had a dream you, how can there be such a good thing)

Singleton: Singleton pattern ensures that only one instance of a certain class, and to instantiate the instance and provide single mode embodiment the entire system. Singleton be used when there is a demand should only be truly "single instance" of.

Structural model

6, ADAPTER- at a friend's party met a beautiful Sarah, come from Hong Kong, but I do not speak Cantonese, she would not speak Mandarin, I had to resort to a friend kent, as he's between me and Sarah Adapter let me and Sarah can talk to each other (do not know if he will play me)

Adapter (transformer) mode: the interface of a class into a client interface to expect another, so that the two classes because the interface to work with the original reasons for not matching can not work together. Adaptation parameter class may return a suitable example to the client.

7, BRIDGE- MM met in the morning, to say good morning and in the evening met MM, to say good evening; met MM wearing new clothes, say your clothes so beautiful Oh, do come across new hairstyle MM, say your hair is oh so beautiful. Do not ask me, "MM met in the morning and made a new hairstyle how to say" this problem, they used a combination BRIDGE what is not on the list

Bridge mode: abstract and implementation of decoupling, so that the two can vary independently, that is to say the strong correlation between them becomes weak association, also refers to a software abstraction between the system and the realization of combination / aggregation relationships instead of inheritance, so that the two can vary independently.

8, COMPOSITE-Mary birthday today. "My birthday, you want to send me a gift." "Ah, well, go to the store, pick your own." "This T-shirt pretty, buy this dress look good, buy, this package is also good, buy. "" Hey, I bought a three Yeah, I only promised to send a gift Oh. "" What is it, T-shirt and skirt plus bag, just dubbed a way, Miss, could you wrap it up. "" ...... ", MM will use Composite mode, you will not?

Mode Synthesis: Synthesis of the target tissue mode to the tree structure, can be used to describe the relationship with the whole portion. Synthetic pattern is a pattern tree structure of an object process. Synthesis of the pattern part of the overall relationship represented by a tree structure. Synthetic pattern such that a client to a separate component and composite objects by their object is obtained by synthesis treated equally.

9, DECORATOR-Mary had finished turn Sarly birthday, or not to call her own pick, or meals certainly comes to an end this month, I took out my last year at the top of the Mountain photos taken in writing on the back of the "best the gift is the love you Fita ", and then to the street gift shop to buy a picture frame (MM sell gift is also very beautiful oh), engage in art designed to find the next Mike designed a beautiful box loaded up ......, we are all Decorator, eventually modified my people in it, how, read it?

Decorator pattern: decorative pattern in a transparent manner for the client extension object function, is an alternative to inheritance, providing more flexibility than inheritance. Moving to an object increased functions, which can then dynamically undone. Increased by the very large number of permutations and combinations of features of the basic functions generated.

10, FACADE- I have a professional Nikon camera, I would like to manually adjust the iris, shutter, so shine out of the picture only professional, but MM can not understand these, we will not teach for a long time. Fortunately, the cameras have Facade design pattern, adjust the camera to the automatic transmission, as long as the aim press the shutter on the line, everything is automatically adjusted by the camera, so MM can also use this camera to take a picture of me.

Facade mode: communicating with an external subsystem must be unified through a facade object. Facade pattern provides a high-level interface, such that the subsystem easier to use. Each subsystem is only one type of window, and only one instance of this type of window, that is to say it is a singleton. However, the entire system can have a plurality of type of window.

11, FLYWEIGHT- with MM send text messages every day, fingers are worn out, recently bought a new phone, you can bring some common sentences in the phone, the use of time, direct out, preceded by the name of MM on It can be sent, and then without a word a word knocked. The sentence is shared Flyweight, MM name is extracted from the external features, using the context situation.

Flyweight: FLYWEIGHT middle finger bantamweight in boxing. Flyweight pattern to share effective way to support a large number of fine-grained objects. Flyweight shared key can do it is to distinguish between state and an intrinsic outer Yun state. Intrinsic state is stored in the internal yuan to enjoy, will not change with changes in the environment varies. Yun state with external changes in the environment is changed. Yun not affect the state of the outer intrinsic state, they are mutually independent. The state can be shared and not shared state to distinguish from the conventional classes, we will not be shared state from class removed out. The client can not directly create objects to be shared, and should use a factory object is responsible for creating the shared object. Flyweight pattern greatly reduce the number of objects in memory.

12, PROXY- with MM in the online chat, always begin with a "hi, hello," "Where did you come to it?" "How old are you?" "How tall is it?" These words, really annoying, write a program as my Proxy, right, who received these words are set up auto-answer, and then I answered notification when receiving other words, how cool it.

Proxy mode: a proxy mode to an object provides a proxy object, the proxy object controlled by reference to the source object. Agent is a person or an institution on behalf of another person or an institution to take action. In some cases, customers do not want or can not directly refer to an object, the proxy object can play the role of intermediary in direct customers and target audience. The client can not tell the real object and the proxy theme theme object. Acting mode may not be aware of the real proxy object, but merely holds a proxied object's interface, this time the proxy object can not be created proxy object, the proxy object must have other roles to create and pass on behalf of the system.

Behavior

13, CHAIN ​​OF RESPONSIBILITY- night to go to English class, for the good bugger sat in the last row, wow, sitting in front of several beautiful MM hey, find a piece of paper, write "Hi, can be my girlfriend right? If you do not want to please pass forward "on a piece of paper after another passed up, and bad, spread to the first row of the note passed to MM teacher, I heard an old maid Yeah, run!

Chain of Responsibility pattern: the chain of responsibility pattern, many objects of each object reference to them at home and then

Together form a chain. In this chain transfer request until a certain object on the chain determination process request. Customers do not know which object on the chain finally deal with this request, the system can not affect the dynamics of re-organization and distribution chain responsibility in the case of the client. Handlers have two choices: take responsibility or blame on to the next home. A request may not be accepted by the final receiving end of any objects.

14, COMMAND- I have a MM tube was particularly strict at home, unable to meet, to transfer information between the two of us have to rely on her brother, she had something on my instructions, wrote a note to let her brother bring me . This does not, her brother and sent over a COMMAND, to thank him, I asked him to eat a bowl of mixed sauce noodles, know what he said: "I also gave my sister three boyfriend COMMAND, you will minimize the number of gas before Please eat my face. ", :-(

Command Mode: the command mode request or an encapsulating operation into an object. Command mode to issue a command responsibility and the responsibility of executing commands separated, assigned to different objects. One command mode allows one party and transmission of the request to open an independent, so the request need not know the interface of one of receiving the request, not to know how the request is received, and the operation is performed, when and how the execution is to be performed of. The system supports undo command.

15, INTERPRETER- I have a "bubble MM Scriptures", a variety of bubble MM Raiders above, such steps to eat Western food, go to the movies and so on method, when a date with MM, just do a Interpreter, photos the above script execute on it.

Interpreter pattern: after a given a language interpreter pattern grammar can be defined one of its representation, while providing an interpreter. The client can use the interpreter to explain the sentences in the language. Interpreter model will describe how to have a simple grammar, the usage patterns designed to explain these statements. Language interpreter which is mentioned in the mode is any combination of objects can be interpreted by the interpreter. You need to define hierarchical structure representative of a command class grammar mode in the interpreter, which is a combination of a series of rules. Each object has a command interpretation, on behalf of the interpretation of the command object. Any permutation and combination of the hierarchical structure of the command object is a target language.

16, ITERATOR- I fell in love with Mary, desperate to marry her.

Mary: "I want to marry you, have agreed to my terms."

Me: "What condition I promise, you say."

Mary: "I took a fancy to the one-carat diamond."

Me: "I bought, I bought, and you?"

Mary: "I saw a lakeside villa in the army."

Me: "I bought, I bought, and you?"

……

Iterator pattern: iterator mode allows access to the order of elements in a gathering without having to expose internal representation gathered. A plurality of objects together to form aggregates called general, aggregate object container object is able to accommodate a group of objects. Iterative sub-mode iterative logic package into a separate sub-object, thereby spaced aggregation itself. Iterator model simplifies the aggregation interface. Each aggregate object can have one or more than one iteration of sub-objects, each state of an iterator iteration may be independent of each other. Iterative algorithm can be independent of the changing role of aggregation.

17, MEDIATOR- four MM playing mahjong, between each other who should calculate exactly how much money to whom, and fortunately I was in the next, according to their number of chips count the money, made money from me took, lost money also paid me, everything OK it, I got four phone MM.

Mediator mode: Mode mediator way packaging a series of objects interacting with each other such that the objects do not have significant effect. So that they may be loosely coupled. When the interaction between certain objects change will not affect the interaction between a number of other objects immediately. To ensure that these effects may be independent of each other change. Mediator pattern will interact to interact many-to-many transformation. Mediator pattern to the behavior of the object and collaboration abstraction, the object handling interaction with other objects on a separate small-scale behavior.

18, MEMENTO- while chatting with a few MM, be sure to write clearly with the MM just said something then, or MM found will be unhappy Oh, fortunately I have a memorandum of understanding with which MM just said something I They are copied into a memorandum which saved so you can always look at previous records it.

Memo mode: a memo object is used to store additional objects snapshot of the internal state of an object. Memo mode is intended in the package without destroying the state of an object is caught, and external of, stored, can be the right time to restore the object to the state stored in the future.

19, OBSERVER- we want to know the latest intelligence MM it? MM joined the company's e-mail intelligence group on the line, tom responsible for gathering intelligence, new information he found a notice that we do not have a publish directly to the e-mail group, we as a subscriber (viewer) can receive timely information it

Observer Pattern: Observer Pattern defines a dependent relationship of one to many observers have multiple objects simultaneously monitor a topic object. The theme of the object changes, the observers will notify all objects in the status, enabling them to automatically update itself.

20, when STATE- contacts with MM, must pay attention to her status, oh, in the different states her behavior will be different, for example, you see a movie about her tonight, for you are not interested in the MM would say " things do that, "you do not hate but did not like the MM would say," Well, but you can take my colleague what? ", you already like the MM would say," what time? watched the film again how to go clubbing? ", of course, you see a good movie during the performance, I can never hate the MM state does not like to become like oh.

Mode Status: state model allows an object to change its behavior when its internal state changes. This looks like the object changes its class the same. State mode to study the behavior of an object wrapped in a different state objects where each object belongs to a state of a subclass of the abstract class status. The intention is to make the state a model object changes in its internal state when their behavior has changed. State model requires the creation of a subclass of a class of state for the status of each system may be achieved. When the state of the system changes, the system will change the selected subclass.

21, STRATEGY- with different types of MM appointments, use different strategies, some make movies better, and some snacks to eat well, go to the beach and some romantic most appropriate single goal is to get the heart MM I chase MM kit has a lot Strategy oh.

Policy Mode: Policy mode for a set of algorithms, each algorithm is encapsulated into a separate class have a common interface, so that that they can replace each other. Changes in the policy mode makes the algorithm can not affect the client's situation. Strategy Mode to separate the behavior and the environment. Environmental responsible for the maintenance and query behavior classes, various algorithms to provide specific strategies class. Since the algorithm independent and open to the environment, increase or decrease algorithms, modifications will not affect the environment and the client.

22, TEMPLATE METHOD-- read the "How to convince the girls to bed," this classic article? Girls into coincidence recognize the same bed from the steps to break the deadlock, started the pursuit, kissing, foreplay, hands, caressing, into eight steps (Template method), but each step for different situations, has a different practice, that depends on your resourcefulness friends (realization);

Template Method: Template Method to prepare an abstract class, the specific part of the logic implemented in the form of specific methods and constructors, and abstract method declarations to force the remaining subclasses implement logic. Different subclasses of abstract methods may be implemented in different ways, so that different implementations of the remaining logic. The first to develop a top-level logical framework, and leave the details to the logic of concrete subclasses to achieve.

23, VISITOR- Valentine's Day to give each of the MM to send a bouquet of flowers and a card, but each MM should send flowers for her personal characteristics, but also to pick each card based on individual characteristics, I which ever know a person, or to find a florist and gift shop owner to do something Visitor, let a bunch of flowers florist selected according to the characteristics of MM, so the gift shop owner would choose a card based on the characteristics of each person, so easy too much;

Visitor pattern: the visitor object pattern is applied onto the packaging operation a certain number of data structure elements. Once these operations need to be modified, then accept the operation of the data structure may remain unchanged. Visitor pattern applied to the data structure of the system is relatively uncertain, which is coupled to the data structures and operations on the acting between the structure to be freed, so that the operation can be relatively freely set evolution. Adding new visitor pattern such that the operation becomes very easy, is to add a new visitor class. The visitor pattern of behavior related to the concentration of a visitor object, rather than distributed to one node class. When using the Visitor pattern, you want as many objects on visitors browser type logic, rather than put it in the subclass. Visitors can cross the hierarchy mode access to several classes of different levels that are members of the class structure.

Guess you like

Origin blog.csdn.net/qq_37725560/article/details/95312432