By spring yang
There are too many design patterns in the garden. Recently I have also learned the design patterns and sorted out some codes I have practiced and wrote them. NET Design Pattern learning notes to give yourself a deeper understanding of the design pattern.
I remember when Jin Yong's novels taught me how to make a jump, he said that the general outline of the solitary nine swords is no trick to win, but in fact there is also such a general outline in the design model, that is? Identify the aspects of your application that vary and separate them from what stays the same. (locate the changed part in the system and separate the changed part from other stable parts ).
Let me start with the 23-way Design Model of the 23-way design model.
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, to see different places of MM, to be able to use their dialect to say this sentence to her oh, 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, and the foreign MM can easily do it. This 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 create more objects of the same type by copying the prototype object. 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. The disadvantage 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, all right, go to the store and pick it for yourself ." "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-shirts and skirts and bags, 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 finishes her birthday in Sarly's turn, do not ask her to pick it up by herself. Otherwise, the meal fee will surely end up this month and take out the photo I took on the top of Huashan last year, write "the best gift is your Fita" on the back, and then buy an image frame in the street Gift Shop (MM for gift sale is also very beautiful ), mike, who is next door to art design, designed a beautiful box and installed 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. In this way, only the photos I have taken are professional, but MM doesn't understand this. It won't take me 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 aim at the target and press the shutter. Everything is automatically adjusted by the camera, in this way, MM can also use this camera to take a photo 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. I can put some commonly used sentences into my mobile phone. I can take them out directly when I use them, add the MM name to the front and you will be able to send the message without a single word. The shared sentence is Flyweight, And the MM name is the extracted external feature, which is used according to the context.
Enjoy mode: FLYWEIGHT is the most lightweight in boxing. The metadata mode efficiently supports a large number of fine-grained objects in a shared manner. 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 state changes with the change of the environment. 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. The client cannot directly create shared objects, but 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. An agent is an action taken by one person or institution on behalf of another person or institution. In some cases, the customer does not want 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 as the proxy object, the proxy object must have other roles created and passed in on behalf of the system.
13. chain of responsibleity-I went to the English class at night and sat down in the last row for good convenience. Wow, I sat in front OF several beautiful MM. I found a piece OF paper and wrote "Hi, can I 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. Requests are transmitted on this chain 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 without affecting the client. 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. I had to use her brother to send messages between us. What instructions does she give me, 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. He said, "I also sent COMMAND to my sister and three boyfriends, you are the least angry to ask me to eat noodles."
Command mode: The command mode encapsulates a request or operation into an object. The command mode separates the responsibility for issuing commands from the responsibility for executing commands and delegates them to different objects. In command mode, the requester and the sender are allowed to be independent, so that the requester does not have to know the interfaces of the receiver, how the request is received, and whether the operation is executed, when and how it is executed. The system supports command revocation.
15. INTERPRETER-