Joke Collection Website - Public benefit messages - Who can introduce some design patterns that have been used for the longest time in the development of JAVA platform ~ it is better to be easy to understand and have examples ~500 points reward.
Who can introduce some design patterns that have been used for the longest time in the development of JAVA platform ~ it is better to be easy to understand and have examples ~500 points reward.
Factory mode, factory method mode, singleton pattern mode, facade mode, observer mode and bridge mode are all commonly used. Different projects have different design directions, and the design patterns that can be referenced are also different. There is no fixed number, but the above models are used more.
I found other patterns, and they are all listed.
======================
Common design patterns of Java
Innovation model
1, factory-please have dinner after chasing mm. The chicken wings of McDonald's and KFC are MM's favorite. 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 factories that produce chicken wings.
Factory mode: customer class and factory class are separated. Whenever a consumer needs a product, just ask the manufacturer. Consumers can accept new products without modification. The disadvantage is that when modifying the product, the factory class should also be modified accordingly. For example: how to create it and how to provide it to the client.
2. builder -MM likes to hear the sentence "I love you" most. When you meet MM in a different place, you should be able to tell her this sentence in their dialect. Oh, I have a multilingual translator, and each language has a button. When you meet MM, just press the corresponding button, and it can say "I love you" in the corresponding language, and foreign MM can easily handle it. This is mine. This is definitely better than the translation machine used by the US military in Iraq.
Construction mode: the internal representation of a product is separated from the generation process of the product, so that a construction process can generate product objects with different internal representations. The construction mode enables the internal appearance of the product to change independently, and the customer does not need to know the details of the internal composition of the product. A build pattern can force a step-by-step build process.
3. Factory Law-Invite MM to McDonald's for hamburgers. Different MMs have different tastes, so remember that everyone is annoying. I usually adopt the factory method, take MM to the waiter and say "I want a hamburger". Ask MM to tell the waiter directly what kind of hamburger she wants.
Factory method mode: the core factory class is no longer responsible for the creation of all products, but gives the specific creation work 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-When chatting with MM on QQ, you must say something affectionate. I collected a lot of disgusting love words, and I copied them out and put them in QQ when necessary. This is my prototype. (100 yuan each, do you want it? )
Original model mode: indicate the type of object to be created by giving a prototype object, and then create more objects of the same type by copying this prototype object. The original model mode allows dynamic increase or decrease of product categories. The product category does not need any predetermined hierarchy, and the original model pattern is applicable to any hierarchy. The disadvantage is that each class must be equipped with a cloning method.
5. Single-I have six beautiful wives. Their husbands are all me. I'm our husband Sigton. As long as I say "husband", I mean the same person, that's me (I just had a dream, how can there be such a good thing).
Singleton mode: singleton mode ensures that there is only one instance of a class, and instantiates it by itself, providing this instance Singleton mode to the whole system. Singleton pattern should be used only when there is a real "single instance" requirement.
structural model
6. Adapter-I met a beautiful woman Sarah from Hong Kong at a friend's party, but I can't speak Cantonese and she can't speak Mandarin, so I had to turn to my friend kent for help. As an adapter between Sarah and me, Sarah and I can talk to each other (I wonder if he will fool me).
Transformer mode: Convert the interface of one class to another interface expected by the client, so that two classes that can't work together because of interface mismatch can work together. The adaptation class can return an appropriate instance to the client according to the parameters.
7. Bridge-If you meet MM in the morning, say good morning; If you meet MM in the evening, say good evening; When you meet a MM wearing new clothes, you should say that the clothes look good. When you meet MM's new hairstyle, you should say it looks good. Don't ask me, "What did you say when I met MM with a new hairstyle this morning?" Just combine it with bridge.
Bridging mode: Decoupling abstraction and implementation, so that they can change independently, that is, turning strong correlation between them into weak correlation, that is, replacing inheritance relationship with combination/aggregation relationship between abstraction and implementation of a software system, so that they can change independently.
Today is Mary's birthday. "You should give me a birthday present." "Well, well, go to the store and choose for yourself." "This T-shirt looks good, buy it, this skirt looks good, buy it, this bag is not bad, buy it." "Hey, I bought three pieces. I only promised to send one gift. " "What? T-shirts, skirts and bags are a perfect match. Miss, please wrap it up. " "...", MM can use compound mode, have you learned?
Composition mode: Composition mode organizes objects into a tree structure, which can be used to describe the relationship between the whole and the parts. Composite pattern is a pattern that deals with the tree structure of objects. The comprehensive model shows the relationship between part and whole in a tree structure. Composition mode enables clients to treat individual component objects as composite objects composed of them.
9. decorator-Mary's birthday is Sarly's turn. Don't let her choose by herself, otherwise the food expenses will definitely not pass this month. Take out the photos I took on the top of Huashan Mountain last year, write "the best gift is Fita with love" on the back, then go to the gift shop on the street to buy a photo frame (the MM selling gifts is also beautiful), and then ask Mike, who is engaged in art design next door, to design a beautiful box and put it in. ...
Decorator pattern: decorator pattern extended the function of the object in a transparent way to the client, which is an alternative way to inherit the relationship and provides more flexibility than inheritance. Functions are dynamically added to an object, and these functions can be dynamically revoked. Plus a very large number of functions generated by the arrangement and combination of some basic functions.
10, facade-I have a professional Nikon camera, and I like to adjust the aperture and shutter manually, so that the photos I take are professional, but MM doesn't understand these things, even after teaching for a long time. Fortunately, the camera has an elevation design mode. Set the camera to automatic mode, aim at the target and press the shutter. Everything is automatically adjusted by the camera. Let MM take a picture of me with this camera.
Facade mode: External communication with subsystems must be conducted through a unified facade object. The facade pattern provides an advanced interface, which 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.
1 1, flyweight-texting MM every day makes my fingers exhausted. I bought a new mobile phone recently, so I can store some commonly used sentences in it. When you want to use it, you can take it out directly, add MM's name in front of it and send it, without typing word by word. * * * Enjoy the sentence is Flyweight, and the name of MM is an extracted external feature, which is used according to the context.
Enjoy mode: Lightweight refers to the lightest weight in boxing. Meta-sharing mode efficiently supports a large number of fine-grained objects by * * * sharing. The key to * * * enjoyment in meta-enjoyment mode is to distinguish between internal state and external state. The inner state is stored in the enjoyment element and will not be different with the change of environment. The external state changes with the change of environment. The external state cannot affect the internal state, they are independent of each other. Distinguish the state that * * * can enjoy and the state that * * * cannot enjoy from the regular class, and exclude the state that * * * cannot enjoy from the class. The client can't directly create the object enjoyed by * * * *, but should use a factory object to create the object enjoyed by * * * *. Enjoying metamodel greatly reduces the number of objects in memory.
12, agent-chatting with MM on the Internet always starts with "Hello, Hello" and "Where are you from?" "How high is it?" These words are really annoying. Write a program as my agent. When you receive these words, you can set an automatic answer and let me know when you receive other words. How's it going? Cool.
Proxy mode: Proxy mode provides a proxy object for an object, and the proxy object controls the reference to the source object. Agency means that a person or an organization acts on behalf of another person or another organization. In some cases, the client doesn't want to or can't directly refer to an object, and the proxy object can directly act as an intermediary between the client and the target object. The client cannot distinguish between a proxy theme object and a real theme object. The proxy pattern may not know the real proxy object, but only hold an interface of the proxy object. At this point, the proxy object cannot create a proxy object, and the proxy object must be created and passed in by other roles in the system.
behavioral pattern
13, chain of responsibility-go to English class at night and sit in the last row to get away. Wow, there are several beautiful MM sitting in front. Find a note and write "Hi, can you be my girlfriend? If you don't want to, please forward it ",and the notes will be passed down one by one. Oh, no, the MM in the first row handed the note to the teacher. I heard it was an old maid. Run!
Chain of responsibility mode: In chain of responsibility mode, many objects are connected by each object's reference to its next home directory.
Stand up and form a chain. Requests pass along this chain until an object in the chain decides to handle the request. The client doesn't know which object in the chain will finally handle this request, and the system can dynamically reorganize the chain and assign responsibilities without affecting the client. The handler has two choices: take responsibility or pass it on to the next family. A request may eventually not be accepted by any recipient object.
14, coMMand-I have a mm, and my family is very strict. I can't meet each other. I have to send messages between us with the help of my brother. If she has any instructions for me, she will write a note for her brother to bring to me. No, her brother gave another order. To thank him, I treated him to a bowl of miscellaneous sauce noodles. I didn't know he said, "I gave orders to my sister's three boyfriends at the same time, so you should calculate your anger before inviting me to eat noodles." ,:-(
Command mode: Command mode encapsulates a request or operation into an object. Command mode separates the responsibility of issuing commands from the responsibility of executing commands and delegates them to different objects. Command mode allows the requester and the sender to be independent, so that the requester does not need to know the interface of the receiver, let alone how the request was received, whether the operation was performed, when and how. The system supports command cancellation.
15, narrator-I have the true story of soaking up MM, which contains various strategies for soaking up MM, such as the steps of eating western food and the method of going to the movies. When dating MM, just be an interpreter and follow the script above.
Interpreter pattern: Given a language, an interpreter pattern can define its grammatical representation and provide an interpreter. Customers can use this interpreter to interpret sentences in this language. Interpreter patterns will describe how to use pattern design to interpret these statements after having a simple grammar. The language mentioned in the interpreter pattern refers to any combination that can be interpreted by any interpreter object. In interpreter mode, it is necessary to define a hierarchy of command classes representing grammar, that is, a series of combination rules. Each command object has an interpretation method, which represents the interpretation of the command object. Any arrangement and combination of objects in the hierarchy of command objects is a language.
16, iterator-I fell in love with Mary and asked her to marry me regardless.
Mary: "If you want me to marry you, you must agree to my terms."
Me: "I promise anything, just say it."
Mary: "I have a crush on that one-carat diamond."
Me: "I'll buy it, I'll buy it, what else?"
Mary: "I took a fancy to the villa by the lake."
Me: "I'll buy it, I'll buy it, what else?"
Mary: "Your penis must be 50 centimeters long."
My head is buzzing. I sit in the chair and grit my teeth: "I cut, I cut, anything else?"
……
Iterative subpattern: Iterative subpattern can access the elements in the aggregation sequentially without exposing the internal appearance of the aggregation. A collection formed by multiple objects is called aggregation, and an aggregation object is a container object that can contain a group of objects. Iterative sub-pattern encapsulates iterative logic into an independent sub-object, which is separate from the aggregation itself. Iterative subpatterns simplify the interface of aggregation. Each aggregation object can have one or more iteration sub-objects, and the iteration state of each iteration can be independent of each other. Iterative algorithm can be independent of aggregation role change.
17, Mediator-Four MMs play mahjong, and they can't tell who should pay who how much. Fortunately, I was there at that time, counting the money according to their chips, taking the money from me, and paying for the loss. Everything was all right. I have four MM mobile phones.
Mediator mode: Mediator mode encapsulates a series of ways in which objects interact, so that these objects don't have to interact with each other obviously. So that they can be loosely coupled. When the interaction between some objects changes, it will not immediately affect the interaction between other objects. Ensure that these effects can be changed independently of each other. The mediation model transforms many-to-many interactions into one-to-many interactions. The mediator model abstracts the behavior and cooperation of objects, and handles the interaction between objects and other objects separately in small-scale behavior.
18, memento-When chatting with several MMs at the same time, you must remember clearly what you just said to MM, otherwise MM will be unhappy when it finds out. Fortunately, I have a memo. I copied down what I just said to which MM and stored it in the memo, so that I can check the previous records at any time.
Memento mode: memo object is an object used to store a snapshot of the internal state of another object. The purpose of Memento mode is to capture the state of an object without destroying the encapsulation, externalize it and store it so that the object can be restored to the stored state at an appropriate time in the future.
19, Observer ——— Want to know the latest MM information of our company? Just join the company's MM information mailbox group. Tom is in charge of collecting information. The new information he found is published in the mail group without notifying us one by one, so that we can receive the information in time as subscribers (observers).
Observer mode: Observer mode defines multiple dependencies and allows multiple observer objects to monitor a topic object at the same time. When the status of this theme object changes, all observer objects will be notified, so that they can update themselves automatically.
20. State-When dealing with MM, you must pay attention to her state. Her behavior will be different in different states. For example, if you invite her to a movie tonight, the MM who is not interested in you will say "something is wrong", while the MM who doesn't hate you but hasn't liked you will say "OK, but can you bring my colleague?" , already like your MM will say "when? How about going to the bar after watching the movie? " Of course, if you do well in the process of watching movies, you can also make MM's status change 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 its class. State pattern wraps the behavior of the studied object in different state objects, and each state object belongs to a subclass of abstract state class. The purpose of state mode is to change the behavior of an object when its internal state changes. The state pattern needs to create subclasses of the state class for every possible state of the system. When the system state changes, the system will change the selected subclass.
2 1, strategy ———— Different strategies are needed for dating different types of MM. Some invited movies, some ate snacks, and some went to the seaside for romance. The single purpose is to please MM. There are many strategies in my MM chase bag.
Policy pattern: Policy pattern is aimed at a group of algorithms, and each algorithm is encapsulated into an independent class with the same interface, so that they can be replaced by each other. The policy mode enables the algorithm to be changed without affecting the client. The strategic model separates behavior from environment. The environment class is responsible for maintaining and querying the behavior class, and the specific strategy class provides various algorithms. Because the algorithm and the environment are independent, the addition, subtraction 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 girls to go to bed" The constant steps for girls to go to bed are divided into eight template methods: meeting by chance, breaking the deadlock, pursuing, kissing, foreplay, hands-on, caressing and entering, but each step has different methods according to different situations, depending on your improvisation (concrete realization);
Template method pattern: Template method pattern prepares an abstract class, implements some logic in the form of concrete methods and concrete constructors, and then declares some abstract methods to force subclasses to implement the rest logic. Different subclasses can implement these abstract methods in different ways, thus realizing the remaining logic differently. First, make a top-level logical framework, and leave the details of logic to specific subclasses to realize.
23.VISITOR—-Valentine's Day is coming. I want to send each MM a bunch of flowers and a card, but the flowers sent by each MM should be based on her personal characteristics, and each card should be selected according to her personal characteristics. How can I find out for myself? I'd better ask the florist and the gift shop owner to make a visit, so that the florist can choose a bunch of flowers according to the characteristics of MM, and the gift shop owner can also choose a card according to everyone's characteristics, which will be much easier.
Visitor pattern: The purpose of visitor pattern is to encapsulate some operations imposed on some data structure elements. Once these operations need to be modified, the data structure that accepts this operation can remain unchanged. Visitor pattern is suitable for the system with relatively uncertain data structure, which decouples the data structure from the operations acting on the structure, so that the operation set can evolve relatively freely. Visitor mode can easily add a new operation, that is, add a new visitor class. The visitor pattern concentrates related behaviors in a visitor object, rather than dispersing them into separate node classes. When using the visitor mode, you should put as much object browsing logic as possible in the visitor class, not in its subclasses. Visitor mode can access member classes belonging to different hierarchies across several class hierarchies.
- Previous article:I bought a house, but the four-year lease with Qingke has strong contract terms. If I terminate the contract early, I will have to pay liquidated damages of about 350,000 yuan. Is the amount too high?
- Next article:How to write the validity period of the new credit card of CCB Mobile Banking?
- Related articles
- 12306 What if I can't receive the SMS verification code?
- What is a good ringtone? Please recommend it.
- What is the code of SMS center?
- What about the virus in the mobile phone? Can't get the SMS from the mobile company?
- +628 1280 150868 This is the number of China Mobile. How can I call or send a message to the above international long distance number? Especially texting.
- Does Meizu mx4 SMS delivery report need to be set?
- Catalogue list of safety production information
- How to view the notifications sent by the system after WeChat payment?
- I bought a second-hand house and signed a deposit receipt with the owner and the intermediary. I don't want to buy a house now Should I fine the agent?
- Message for the Year of Tiger and New Year!