In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what are the design patterns". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Preface
Design pattern (Design Pattern) is a summary of code development experience by predecessors, and it is a series of routines to solve specific problems. It is not a syntax stipulation, but a solution to improve code reusability, maintainability, readability, robustness, and security.
Design patterns are tested in programmers' interviews, usually by introducing their principles and pointing out their advantages and disadvantages, or by comparing the similarities and differences of several similar patterns. Questions such as drawing a UML diagram of a design pattern may appear in the written exam. Just because it doesn't account for a large proportion of the interview doesn't mean it's not important.
1. Factory method
Chasing MM is a must. McDonald's chicken wings and KFC chicken wings are MM's favorite foods, although the taste is different, but whether you take MM to McDonald's or KFC, just say "four chicken wings" to the waiter. McDonald's and KFC are the Factory factory model for producing chicken wings: the customer category is separated from the factory category.
Whenever consumers need a certain product, they only need to ask the factory. Consumers can accept new products without modification. The disadvantage is that when the product is modified, the factory class has to be modified accordingly. Such as: how to create and how to provide to the client.
2. Builder model
What MM likes to hear most is the sentence "I love you". When you see MM in different places, you should be able to say this to her in their dialect. Oh, I have a multilingual translation machine, each of which has a button. When I see MM, I just need to press the corresponding button, and it will be able to say "I love you" in the corresponding language. Foreign MM can also easily deal with it. This is my "I love you" builder.
Construction mode: separate the internal representation of the product from the product generation process, so that a construction process generates product objects with different internal representations. The construction mode enables the internal appearance of the product to change independently, and the customer does not have to know the details of the internal composition of the product. The construction model can enforce a step-by-step construction process.
3. Abstract factory
Invite MM to McDonald's to eat hamburgers, different MM have different tastes, it is an annoying thing to remember each. I usually use Factory Method mode, take MM to the waiter and say, "want a hamburger". What kind of hamburger do you want? just let MM tell the waiter directly.
Factory method pattern: the core factory class is no longer responsible for the creation of all products, but leaves the work of concrete creation to the subclass, becoming an abstract factory role, only responsible for giving the interfaces that the specific factory class must implement. without touching the details of which product class should be instantiated.
4. Prototype model
Chat with MM with QQ, be sure to say some affectionate words, I have collected a lot of nasty love words, as long as copy come out and put it in QQ when needed, this is my love word prototype. (100 yuan a piece, do you want it or not)
Original model pattern: specify the type of 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 pattern allows the dynamic increase or decrease of product classes, which do not need to have any pre-determined hierarchical structure, and the original model pattern is suitable for any hierarchical structure. The disadvantage is that each class must be equipped with a clone method.
5. Singlet mode
I have six beautiful wives, their husbands are all me, I am our husband Sigleton, as long as they say "husband", they all refer to the same person, that is me (I just had a dream, how can there be such a good thing)
Singleton pattern: the singleton pattern ensures that there is only one instance of a class and instantiates itself and provides this instance singleton pattern to the entire system. The singleton pattern should only be used when there is a true "single instance" requirement.
6. Adapter mode
At the friends' party, I met a beautiful woman, Sarah, from Hong Kong, but I could not speak Cantonese and she could not speak Mandarin, so she had to turn to my friend kent. As the Adapter between me and Sarah, Sarah and I could talk to each other (I don't know if he would play tricks on me)
Adapter (transformer) mode: converts the interface of one class into another interface expected by the client, so that two classes that cannot work together because of interface mismatch can work together. The adaptation class can return an appropriate instance to the client based on the parameters.
7. Bridge mode
Meet MM in the morning, say good morning, meet MM in the evening, say good evening; meet MM wearing a new dress, say your clothes are so beautiful, meet MM's new hairstyle, say your hair is so beautiful. Don't ask me "what do you say when I met MM with a new haircut this morning?" just combine it with BRIDGE.
Bridge pattern: decoupling abstraction and implementation, so that the two can change independently, that is, the strong relationship between them can be changed into weak correlation, that is, the combination / aggregation relationship rather than inheritance relationship is used between the abstraction and implementation of a software system, so that the two can change independently.
8. Synthesis mode
It's Mary's birthday today. " You will give me a present for my birthday. "" Yeah, well, go to the store, you pick. "" This T-shirt is very beautiful, buy, this skirt looks good, buy, this bag is also good, buy. " Hey, I've bought three. I only promised one gift. "" What? t-shirt, skirt and bag make a perfect match, miss. Please wrap it up. " . " MM can use Composite mode now. Have you got it?
Composition pattern: the composition pattern organizes objects into a tree structure and can be used to describe the relationship between the whole and the part. A composite pattern is a pattern that deals with the tree structure of an object. The composition pattern represents the relationship between the part and the whole as a tree structure. The composition mode allows the client to treat individual component objects in the same way as the composite objects formed by them.
9. Decoration mode
Mary, after it's Sarly's turn to celebrate her birthday, don't ask her to pick it out, or the food bill will be over this month. Take out the photo I took on the top of Huashan last year and write "the best gift is your Fita" on the back. Then I went to the gift shop on the street to buy a picture frame (the MM selling gifts is also very beautiful), and then found the next door art designer Mike to design a beautiful box to pack it. We are all Decorator, and we are all decorating me as a person in the end. How about that? do you understand?
Decoration mode: decoration mode extends the functionality of objects in a way that is transparent to the client, is an alternative to inheritance relationships, and provides more flexibility than inheritance. Dynamically add functions to an object that can be dynamically undone. Add a very large number of functions produced by the arrangement and combination of some basic functions.
10. Facade mode
I have a professional Nikon camera, I like to manually adjust the aperture and shutter, so that the photos taken are professional, but MM does not understand this, not even after teaching for a long time. Fortunately, the camera has Facade design mode, adjust the camera to automatic, as long as aim at the target and press the shutter, everything is automatically adjusted by the camera, so MM can also use this camera to take a picture of me. Facade mode: external communication with a subsystem must be carried out through a unified facade object.
Facade mode provides a high-level interface that makes the subsystem easier to use. Each subsystem has only one facade class, and this facade class has only one instance, that is, it is a singleton pattern. But the whole system can have multiple facade classes.
11. Enjoy the meta-mode
I am exhausted from texting with MM every day. I recently bought a new mobile phone, so I can store some commonly used sentences in the phone. When I want to use it, I can just take it out and add the name of MM in front of it. I don't have to type a word anymore. The shared sentence is the name of Flyweight,MM, which is the extracted external feature, which is used according to the context. Hedonic mode: FLYWEIGHT refers to the lightest weight in boxing.
The shared meta-pattern efficiently supports a large number of fine-grained objects in a shared way. The key to sharing meta-mode is to distinguish the intrinsic state from the extrinsic state. The intrinsic state is stored inside the sharing element and will not change with the change of the environment. The extrinsic state changes with the change of the environment. The extrinsic state can not affect the intrinsic state, they are independent of each other.
Distinguish the state that can be shared from the state that cannot be shared from the regular class, and remove the state that cannot be shared from the class. Instead of creating shared objects directly, clients should use a factory object to create shared objects. The shared meta mode greatly reduces the number of objects in memory.
12. Agent mode
When chatting with MM online, they always start with "hi, hello", "where are you from?"how old are you?"how tall are you?" These words, really annoying, write a program as my Proxy, all receive these words have set up their own answers, receive other words and then let me know the answer, how, cool?
Proxy pattern: the proxy pattern provides a proxy object to an object, and the proxy object controls the reference to the source object. An agent is a person or an organization acting on behalf of another person or an organization. In some cases, the customer does not want or cannot directly reference an object, and the proxy object can act as an intermediary between the customer and the target object.
The client cannot distinguish between the proxy topic object and the real topic object. The proxy pattern can not know the real proxied object, but only holds an interface of the proxied object. At this time, the proxy object cannot create the proxied object, and the proxied object must be created and passed in by other roles of the system.
13. Responsibility chain model
When I went to English class in the evening, I sat in the last row in order to slip away. Wow, there were several beautiful MM sitting in front of me. Find a note and write, "Hi, can you be my girlfriend? if you don't want to, please pass it forward." the note was passed one by one. Too bad, the MM in the first row passed the note to the teacher. I heard it was an old maid. Run!
Chain of responsibility pattern: in the chain of responsibility pattern, many objects are connected by each object's reference to its subordinate to form a chain. The request is passed on the chain until an object on the chain decides to process the request. The customer does not know which object on the chain will eventually process the request, and the system can dynamically reorganize the chain and assign responsibilities without affecting the client. The handler has two choices: take the responsibility or shift the responsibility to the next family. A request can eventually not be accepted by any receiving object.
14. Command mode
I have a MM family that is too strict to meet, so I have to use her brother to send a message between us. If she has any instructions, she will write a note for her brother to bring to me. In this case, her brother sent another COMMAND. In order to thank him, I invited him to eat a bowl of noodles with miscellaneous sauce. How could I know that he said, "I gave COMMAND to my sister's three boyfriends at the same time. You were the most stingy and invited me to eat noodles."
Command mode: command mode encapsulates a request or operation into an object. The command mode separates the responsibility of issuing the command from the responsibility of executing the command and delegates it to different objects. The command mode allows the requesting party to be independent from the sending party, so that the requesting party does not need to know the interface of the party receiving the request, let alone how the request is received and whether the operation is performed, when and how it is executed. The system supports the revocation of commands.
15. Interpreter mode
I have a "soak in MM Sutra", which has a variety of strategies for soaking in MM, such as how to eat western food, how to go to the movies, etc. When dating MM, just make an Interpreter and follow the script above.
Interpreter pattern: given a language, the interpreter pattern can define a representation of its grammar and provide an interpreter. The client can use this interpreter to interpret sentences in the language. The interpreter pattern will describe how to use pattern design to interpret these statements after you have a simple grammar.
The language mentioned in the interpreter pattern refers to any combination that any interpreter object can interpret. In the interpreter pattern, you need to define a hierarchical structure of command classes that represent grammars, that is, a series of combination rules. Each command object has an interpretation method that represents the interpretation of the command object. Any arrangement or combination of objects in the hierarchical structure of a command object is a language.
16. Iterative model
I fell in love with Mary and proposed to her desperately. Mary: "if you want me to marry you, you have to agree to my terms." I: "I agree to any terms, so tell me." Mary: "I have a crush on that one-carat diamond." I: "I'll buy it. I'll buy it." Mary: "I have a crush on the villa by the lake." I: "I'll buy it. I'll buy it. Is there anything else?" Mary: "I have a crush on that Ferrari." I sat in my chair and clenched my teeth. "I'll buy it, I'll buy it, is there any more?"
Iterative pattern: the iterative pattern can sequentially access the elements in an aggregation without exposing the internal representation of the aggregation. The aggregate formed by multiple objects is called aggregation, which is a container object that can contain a group of objects. The iterative subpattern encapsulates the iterative logic into a separate sub-object, thus separating it from the aggregation itself.
The iterative pattern simplifies the interface of aggregation. Each aggregate object can have one or more iterated sub-objects, and the iterative state of each iterator can be independent of each other. The iterative algorithm can be independent of the aggregate role change.
17. Mediator model
Four MM play mahjong, it is not clear who should give each other how much money. Fortunately, I was there at that time, counting money according to their respective chips, those who made money took from me, those who lost money also paid me, everything was OK, I got four MM numbers. Mediator pattern: the mediator pattern wraps a series of ways in which objects interact so that they do not have to interact significantly with each other. So that they can be loosely coupled.
When the interaction between some objects changes, it does not immediately affect the interaction between other objects. To ensure that these effects can be changed independently of each other. The mediator model transforms many-to-many interactions into one-to-many interactions. The mediator model abstracts the behavior and cooperation of objects and separates the interactions of objects from the interactions of other objects in small-scale behavior.
18. Memo mode
When chatting with several MM at the same time, be sure to remember what you just said to MM, or MM will be unhappy when you find out. Fortunately, I have a memo. I copy everything I said to which MM and keep it in the memo, so that I can check the previous records at any time.
Memo mode: a memo object is an object that stores a snapshot of the internal state of another object. The purpose of the memo pattern is to capture, externalize and store the state of an object without breaking the encapsulation, so that the object can be restored to the stored state at an appropriate time in the future.
19. Observer mode
Want to know the latest MM information of our company? Just join the company's MM intelligence e-mail group. Tom is responsible for collecting intelligence. The new information he finds does not need to notify us one by one, but directly publishes it to the email group. As subscribers (observers), we can receive the information in time.
Observer pattern: the Observer pattern defines a set of multiple dependencies that allow multiple observer objects to listen to a subject object at the same time. When this subject object changes in state, it notifies all observer objects so that they can update themselves automatically.
20. Status mode
When dating with MM, be sure to pay attention to her status. Her behavior will be different in different states. For example, if you ask her to go to the movies tonight, MM who is not interested in you will say "something's wrong". MM, who you don't hate but don't like, will say, "OK, but can you bring my colleague?" MM, who already has a crush on you, will say, "what time is it? how about going to a bar after the movie?" Of course, if you do well in watching the movie, you can also change the state of MM from dislike to liking.
State mode: state mode allows an object to change its behavior when its internal state changes. This object looks as if it had changed its class. The state pattern wraps the behavior of the studied objects in different state objects, each of which belongs to a subclass of an abstract state class.
The intention of the state mode is to change the behavior of an object when its internal state changes. The state mode needs to create a subclass of the state class for each state that the system may achieve. When the state of the system changes, the system changes the selected subclass.
21. Strategy model
With different types of MM dating, to use different strategies, some invite a movie is better, some go to eat snacks effect is good, some go to the beach romantic is the most appropriate, monocular is to get the heart of MM, I chase MM bag has a lot of Strategy. Policy pattern: for a set of algorithms, the policy pattern encapsulates each algorithm into a separate class with a common interface, so that they can replace each other.
The policy mode allows the algorithm to change without affecting the client. The strategy module separates the behavior from the environment. Environment classes are responsible for maintaining and querying behavior classes, and various algorithms are provided in specific policy classes. Because the algorithm and the environment are independent, the addition or subtraction and modification of the algorithm will not affect the environment and the client.
22. Template method model
Have you read the classic article "how to persuade a girl to have sex"? Girls realize that the constant steps of having sex can be divided into eight steps: chance encounter, breaking the deadlock, pursuing, kissing, foreplay, hands-on, caressing, and getting in (Template method), but each step has a different approach according to different situations, which depends on your adaptability (concrete realization).
Template method pattern: the template method pattern prepares an abstract class, implements part of the logic in the form of concrete methods and concrete constructors, and then declares some abstract methods to force subclasses to implement the rest of the logic. Different subclasses can implement these abstract methods in different ways, thus having different implementations of the remaining logic. First develop a top-level logic framework, and leave the details of the logic to specific subclasses to implement.
23. Visitor mode
Valentine's Day is coming, to send each MM a bouquet of flowers and a card, but each MM to send flowers according to her personal characteristics, each card should also be selected according to her personal characteristics, I can't figure it out by myself, or find the florist and gift shop owner to do a Visitor, let the florist choose a bouquet of flowers according to the characteristics of MM, let the gift shop owner also choose a card according to everyone's characteristics, it will be much easier.
Visitor pattern: the purpose of the Visitor pattern is to encapsulate operations that are imposed on certain data structure elements. Once these operations need to be modified, the data structure that accepts the operation can remain unchanged. The visitor pattern is suitable for systems with relatively uncertain data structures. it detaches the coupling between the data structure and the operations acting on the structure, so that the set of operations can evolve relatively freely. The visitor pattern makes it easy to add a new operation, which is to add a new visitor class.
The visitor pattern centralizes the relevant behavior into a visitor object rather than spreading it into node classes. When using the visitor pattern, put as much object browsing logic as possible in the visitor class, rather than in its subclasses. The visitor pattern can access member classes that belong to different hierarchical structures across the hierarchical structure of several classes.
This is the end of the content of "what Design patterns". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.