In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly analyzes the relevant knowledge points of how to analyze the 23 design patterns of java, the content is detailed and easy to understand, the operation details are reasonable, and has a certain reference value. If you are interested, you might as well follow the editor and learn more about "how to analyze the 23 design patterns of java".
1. FACTORY- 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 that make chicken wings.
Factory mode: customer class and factory class are separated. 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.
Sequence code
The following is a reference clip:
The following is a reference clip:
Public class Factory {
Public String Boy = "boy"
Public String Girl = "girl"
Public People getPeople (String people) {
If (people.equals ("boy")) {
Return new Boy ()
} else if (people.equals ("girl")) {
Return new Girl ()
}
}
}
2. What BUILDER-MM likes to hear most is the sentence "I love you". When you see MM in different places, you need to be able to say this to her in their dialect. Oh, I have a multilingual translator, 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, and foreign MM can easily handle it. This is my "I love you" builder. (this must be better than the translator used by the US military in Iraq.)
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. FACTORY METHOD- invites MM to McDonald's to eat hamburgers. Different MM has different tastes, so it's annoying to remember each of them. I usually use Factory Method mode, take MM to the waiter and say, "I 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- and MM use QQ to chat, be sure to say some affectionate words. I have collected a lot of nasty love words. When needed, as long as copy comes out and puts them in QQ, 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. SINGLETON- 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, there is no 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.
The following is a reference clip:
The following is a reference clip:
Public class SingLeton {
Private static SingLeton instance = new SingLeton ()
Public static SingLeton getInstance () {
Return instance
}
}
6. ADAPTER- met a beautiful woman Sarah from Hong Kong at the friends' party, 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- meets MM in the morning, good morning, MM in the evening, good evening, MM wearing a new dress, your clothes are so beautiful, MM's new hairstyle, 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.
COMPOSITE-Mary celebrates his 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? the 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. DECORATOR-Mary, when 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. FA cADE- 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 Fa ç ade 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. FLYWEIGHT- sends text messages to MM every day, and his fingers are exhausted. He recently bought a new mobile phone, which can store some commonly used sentences in the phone. When you want to use it, you can just take it out and add the name of MM in front of it. You 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 of 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. When PROXY- and MM chat online, they always start with "Hello, hi", "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 are set up automatic answer, 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 can not 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.
The following is a reference clip:
Public interface FactoryProxy {
Public People createBoy ()
Public People creteGirl ()
}
13. CHAIN OF RESPONSIBLEITY- went to English class in the evening, and in order to slip away, he sat in the last row. Wow, there were several beautiful MM sitting in front. 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 virgin. Run!
Chain of responsibility pattern: in the chain of responsibility pattern, many objects are connected by each object's reference to its next family.
Get up and 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 handle 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- I have a MM family who is so strict that she can't meet, so I have to send a message between us with the help of her brother. If she has any instructions to me, 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 executed, when and how it is executed. The system supports the revocation of commands.
15. INTERPRETER- I have a "MM Sutra" on which there are various 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. ITERATOR- 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.
Me: "I'll agree to any condition. Go ahead."
Mary: "I have a crush on that one-carat diamond."
Me: "I'll buy it, I'll buy it, is there any more?"
Mary: "I have my eye on the villa by the lake."
Me: "I'll buy it, I'll buy it, is there any more?"
Mary: "I have my eye on that Ferrari."
With a buzz in my head, I sat in the chair and gritted my teeth: "I'll buy it, I'll buy it, is there any more?"
……
Iterative subpatterns: iterative subpatterns can sequentially access 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 subpattern 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- four MM play mahjong, it is not clear who should give whom how much money to each other. Fortunately, I was there at that time, counting the money according to their respective chips. Those who made money took from me, and 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. When MEMENTO- is chatting with several MM at the same time, be sure to remember what he just said to MM, or MM will be unhappy when he finds out. Fortunately, I have a memo. I copy everything I said with 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- do you want to know the latest MM information of our company? Just join the company's MM intelligence 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 mail 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. When STATE- is dating 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 cinema tonight, MM who is not interested in you will say "something's wrong". MM, which you don't hate but haven't liked yet, will say, "OK, but can you bring my colleague?" MM will say "what time is it?" How about going to the bar after watching the movie? "of course, if you perform well during the movie, you can also change the state of MM from dislike to dislike.
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- uses different strategies to date with different types of MM. Some invite movies, some go for snacks, and some go to the seaside for romance. They all aim to win the heart of MM. There are a lot of Strategy in my MM bag.
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 pattern separates 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. Has TEMPLATE METHOD-- 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 improvisation.
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 logical framework, and leave the details of the logic to the specific subclasses to implement.
23. VISITOR- Valentine's Day is coming, send each MM a bouquet of flowers and a card, but each MM sends flowers according to her personal characteristics, and each card should be selected according to her personal characteristics. I can't figure it out by myself. I should ask the florist and gift shop owner to do a Visitor, let the florist choose a bouquet of flowers according to the characteristics of MM, and let the gift shop owner choose a card according to each person's characteristics. It's a lot 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 article mainly analyzes the relevant knowledge points of how to analyze the 23 design patterns of java, the content is detailed and easy to understand, the operation details are reasonable, and has a certain reference value. If you are interested, you might as well follow the editor and learn more about "how to analyze the 23 design patterns of java".
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.