Interesting insights into java23 design patterns

Source: Internet
Author: User


Create pattern

1, factory-chase mm, please eat, McDonald's chicken wings and KFC chicken wings are all mm love to eat things,
Although the taste is different, but whether you take mm to McDonald's or KFC, just say to the waiter "Four Chicken Wings"
On the line. McDonald's and KFC are the factory to produce chicken wings.

Factory mode: Separate the customer class from the factory class. Consumers need a product at any time, just request it from the factory
。 Consumers can accept new products without modification. The disadvantage is that when the product is modified, the factory class should also make corresponding changes
。 such as: How to create and how to provide to the client.

2, builder-mm most love to listen to is "I love you" this sentence, see different places of MM, to be able to use their
Dialect speak to her this sentence oh, I have a multi-lingual translation machine, each of the above language has a button, see
MM I just press the corresponding key, it will be able to speak the corresponding language "I love you" this sentence, foreign mm can also
With ease, this is my "I love You" builder. (This must be better than the American translation machine used in Iraq.)


Construction mode: Separates the internal appearance of the product from the production process, thus enabling a construction process to generate
Product objects that have different internal representations. The construction mode makes the interior appearance of the product can change independently, the customer does not need
Know the details of the internal composition of the product. The construction model can enforce a step-by-stage construction process.

3, FACTORY method-please mm to McDonald's to eat hamburgers, different mm have different tastes, to each remember is a
Piece of annoying things, I generally use factory method mode, with mm to the waiter there, said "to a Han
Fort ", specifically what kind of hamburger, let mm directly with the waiter said on the line.

Factory method Mode: The Core factory class is no longer responsible for the creation of all products, but rather the specific creation of the work to sub-class
To be an abstract factory role, only to give the interface that a specific factory class must implement, without touching which
A product class should be instantiated with this detail.

4, prototype-and mm with QQ chat, must say some affectionate words, I collected a lot of sweet nothings,
Need as long as copy out into QQ inside on the line, this is my sweet nothings prototype. (100 bucks a share.)
, do you want to)

Original model mode: By giving a prototype object to indicate the type of object to be created, and then copying the
The method of the prototype object creates more objects of the same type. Original model mode allows for dynamically increasing or decreasing product classes
, the product class does not need to have any predetermined hierarchy structure, the original model pattern applies to any hierarchy structure
。 The disadvantage is that each class must be equipped with a clone method.

5, singleton-I have 6 beautiful wives, their husbands are me, I am the husband of our family Sigle
ton, they just say "husband", all refers to the same person, that is I (just had a dream, which has this
Good thing)

Singleton mode: A singleton mode ensures that a class has only one instance, and instantiates it itself and provides it to the system as a whole.
Example Singleton mode. Singleton mode should only be used when there is a real "single instance" requirement.

Structural mode

6, Adapter-met a beautiful woman at a friend's party, Sarah, from Hong Kong, but I will not speak Cantonese, she does not
Can speak Putonghua, had to resort to my friend Kent, he as between me and Sarah Adapter, let me and SA
RAH can talk to each other (and I don't know if he will play with me)

Adapter (transformer) mode: The interface of a class is transformed into another interface that the client expects, so that the original
The two classes that could not work together because of a mismatch in the interface cause can work together. The adaptation class can be returned according to the parameters
A suitable instance to the client.

7, bridge-morning met mm, to say good morning, at night met mm, to say good evening; met mm wearing a new dress
, to say that your clothes are very beautiful oh, met mm new hair, to say that your hair is very beautiful oh. Don't ask me, "morning.
Hit mm new do a hairstyle how to say "This kind of problem, their own use of bridge combination is not OK

Bridge mode: The abstraction and implementation decoupling, so that they can be independent of the change, that is, their
Strong associations become weak associations, which means using a combination/aggregation relationship between abstraction and implementation of a software system
Instead of inheriting relationships, so that they can change independently.

8, Composite-mary today's birthday. "My birthday, you want to give me a gift." "Well, well, go
Shop, pick your own. "This t-shirt is pretty, buy, this skirt looks good, buy, this bag is also good, buy." ”
"Hey, bought three pieces, I only promised to send a gift of OH." "What, the T-shirt plus the skirt plus the bag, just
A set, miss, please wrap it up. "" ... ", MM will use composite mode, you will not
Yes?

Compositing mode: The composition mode organizes objects into a tree structure that can be used to describe the relationship between the whole and the part. Synthetic die
is a pattern that handles the tree structure of an object. The composition mode represents the relationship of the part and the whole with the tree structure.
。 The compositing mode allows the client to treat a single constituent object in the same way as a composite object made up of them.


9, decorator-mary after the turn to sarly birthday, or do not call her own pick, or this month food
The fee must have finished, take out my last year on the top of the mountain photo, write on the back "the best gift is to love you
Fita ", and then to the street gift shop bought a box (selling gifts mm is also very beautiful OH), and then find the next door to do art set
Mike designed a nice box to pack up ... we're all Decorator, and we're all in the end decorating my
Man, what's up, do you understand?

Decorative mode: The adornment mode expands the function of the object in a transparent manner to the client, and is an alternative to the inheritance relationship
Offer more flexibility than inheritance. Dynamically adds functionality to an object that can then be dynamically undone
。 Adds a very large amount of functionality resulting from the combination of some basic functions.

10, facade-I have a professional Nikon camera, I like to manually adjust the aperture, shutter, so that the
To the photo is professional, but mm can not understand these, taught for a long while will not. Fortunately, the camera has facade design mode,
The camera adjusts to the automatic, as long as the target is pressed to the shutter, everything is automatically adjusted by the camera, so mm can also
Take a picture of me with this camera.

Façade mode: External communication with a subsystem must be done through a unified façade object. Façade mode provides
A high-level interface that makes subsystems easier to use. Each subsystem has only one façade class, and the door
A polygon class has only one instance, which means it is a singleton pattern. But the entire system can have multiple façade classes.

11, flyweight-every day with the mm texting, fingers are exhausted, recently bought a novice machine, you can put some common
The sentence exists in the mobile phone, to use the time, directly to take out, in front of the name of the MM can be sent, and then
Do not use a word to knock a word. The shared sentence is FLYWEIGHT,MM's name is the extracted external feature.
, which is used according to context conditions.

Enjoy meta-mode: FLYWEIGHT is the most lightweight in a boxing match. Enjoy a shared approach to support large
The amount of fine-grained objects. The key to sharing the shared meta-mode is to differentiate between intrinsic state and external state. Intrinsic State storage
stored in the inside of the element, will not vary with the environment. The state of the outer Yun is changed with the change of the environment. Outside
Yun state can not affect the intrinsic state, they are independent of each other. The state that will be shared and the state that cannot be shared
Separate from the regular class and remove the state from the class that cannot be shared. Clients are not allowed to create direct
Shared objects, you should use a factory object that is responsible for creating the objects that are shared. Significant reduction in the mode of enjoyment
The number of objects in memory.

12, proxy-and MM in the online chat, a beginning always "Hi, hello", "Where do you come from?" "How old are you?"
? "How tall?" "These words, really annoying, write a program as my proxy bar, usually receive these words
have set up the automatic answer, receive other words, then notify me to answer, how, cool bar.

Proxy mode: The proxy mode provides a proxy object to an object, and the proxy object controls the citation of the source object.
Use. An agent is a person or an institution acting on behalf of another person or an institution. In some cases, the customer
The user does not want or can not directly reference an object, the proxy object can be directly in the client and the target object to mediate
Role. The client cannot distinguish between a proxy subject object and a real subject object. Proxy mode may not know the true
Proxy object, but only the interface of a Proxied object, the proxy object is not able to create a proxy object
, the proxy object must be created and passed in on behalf of the other roles of the system.

13, CHAIN of responsibleity-evening to go to the English class, in order to good getaway sat to the last row, wow, before
Face sat several beautiful mm hey, find a note, write "Hi, can do my girlfriend?" If you do not want to ask
Forward ", the note on one after the transmission up, bad, to the first row of MM to pass the note to the teacher, listen to
Say it's an old maid, run! Responsibility chain mode: In the chain of responsibility model, many objects are controlled by each object
The reference to the home, and then


Up to form a chain. The request is passed on this chain until an object on the chain decides to process the request. Customer
It is not known which object on the chain is ultimately processing this request, and the system can be dynamic without affecting the client
Re-organization chain and distribution responsibilities. The processor has two choices: take responsibility or push the responsibility to the other person. A please
Not be accepted by any receiving end object.

14, command-I have a mm home tube special strict, can not meet, had to rely on her brother in both of us
Send a message, she gave me instructions, write a note for her brother to bring me. No, her brother sent it again.
Came to a command, in order to thank him, I asked him to eat a bowl of mixed sauce noodles, how he said: "I also give my sister
Three boyfriends Send command, count you the most stingy, just ask me to eat noodles. ”,:-(

Command mode: The command mode encapsulates a request or action into an object. Command mode the responsibility to issue the order
The responsibility for any and executing commands is split and delegated to different objects. The command pattern allows the requesting party and the sending of a
So that the requesting party does not have to know the interface of the party receiving the request, not to know how the request
is received, and whether the operation is performed, when it is executed, and how it is executed. The system supports undo of the command.


15, interpreter-I have a "bubble mm canon", which has a variety of bubble mm raiders, such as to eat Western food
Steps, the way to see the film and so on, with mm date, as long as a interpreter, according to the above script
Yes, you can.

Interpreter mode: 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 the sentences in the language. The interpreter mode describes
How to use pattern design to interpret these statements after having a simple grammar. Mentioned in the interpreter mode.
Language refers to any combination that any interpreter object can interpret. In the interpreter mode, you need to define a representative grammar
Hierarchy of command classes, which is a series of combinations of rules. Each command object has an interpretation method,
Represents the interpretation of a Command object. Any permutation combination of objects in the hierarchy of command objects is a language.




16. iterator-I fell in love with Mary and was desperate to marry her.

Mary: "If you want me to marry you, I have to promise my terms."

Me: "What are the terms I promise, you say?"

Mary: "I have a crush on that one-carat diamond."

Me: "I buy, I buy, still have?" ”

Mary: "I have a crush on the villa by the lake."

Me: "I buy, I buy, still have?" ”

Mary: "Your penis has to be 50cm long."

My head is humming, sitting in a chair, a bite: "I cut, I cut, and there?" ”

......

Iterative sub-patterns: Iterative sub-patterns can sequentially access an element in a cluster without exposing the aggregated interior representation.
Aggregates are collectively called aggregates, and clustered objects are container objects that can contain a set of objects.
The iterative sub-pattern encapsulates the iterative logic into a separate sub-object, which is separated from the aggregation itself. Iterative sub-pattern
Simplifies the aggregation of the interface. Each clustered object can have one or more iteration sub-objects, each of which iterates
The iteration states of the generation son can be independent of each other. Iterative algorithms can be independent of aggregate role changes.

17, mediator-four mm playing mahjong, between each other who should give who how much money is not clear, fortunately, I was in
Next, according to their respective chips count money, earn money from my here to take, lost the money also pay me, everything OK
, I got a four mm phone.

Mediator mode: The Mediator pattern wraps a series of object interactions so that these objects do not have to be mutually clear
function. So that they can be loosely coupled. When the effects of some objects change, they do not immediately affect
The role of some other objects. Ensure that these effects can change independently of each other. The mediator pattern will be many-to-many
Interactions into one-to-many interactions. The Mediator pattern abstracts the behavior and collaboration of objects,
The interaction of small-scale behavior with other objects is treated separately.

18, memento-at the same time with a few mm chat, be sure to remember what just said with mm what words, otherwise mm found
will be unhappy oh, fortunately, I have a memo, just with which mm said what words I have a copy to put on memo
Record, so you can view the previous records at any time.

Memo Mode: A Memo object is an object used to store a snapshot of the internal state of another object. Memo
The intent of the pattern is to catch, and externally, store the state of an object without destroying the package,
This allows the object to be restored to a stored state at the appropriate time in the future.

19, observer-want to know our latest mm information? Join the company's MM intelligence mail group on the line, Tom Negative
Information, he found that the new information without a notice to us, directly to the Mail group, we as a subscription
Reader (Observer) to receive information in a timely manner.

Observer pattern: The Observer pattern defines a multiple-team dependency, allowing multiple observer objects to simultaneously listen to a
A Subject object. When the Subject object changes state, all observer objects are notified so that they can
Enough to automatically update themselves.

20, state-and mm contact, must pay attention to her state oh, in different states, her behavior will be different
, for example, if you ask her to go to a movie tonight, MM will say "something" to you, and you don't hate it, but
Not like the MM will say "Yes, but can I take my colleague?" "And already like your mm will say
"What time Is it?" Why don't you go clubbing after the movie? "And of course you do well in the process of watching movies, you can also
The status of MM never hate to become like OH.

State mode: state mode allows an object to change behavior when its internal state changes. This object looks
Like the class that changed it. The state pattern wraps the behavior of the object under study in different state objects, each
A state object belongs to a subclass of an abstract state class. The intent of the state mode is to have an object
When the state of the ministry changes, its behavior changes. State mode requires a possible state for each system to create a
A subclass of a State class. When the state of the system changes, the system changes the selected subclass.

21, strategy-with different types of mm dating, to use a different strategy, some movies better, some go
Eat snacks effect is good, some to the seaside romantic most suitable, single purpose is to get mm heart, my chase mm Kam
There are many strategy in the SAC.

Policy mode: Policy mode for a set of algorithms, each algorithm is encapsulated in a separate class with a common interface,
So that they can be replaced with each other. The policy mode allows the algorithm to change without affecting the client
Of. The strategy model separates behavior from the environment. The Environment class is responsible for maintaining and querying the behavior of the class, various algorithms in the specific policy
Available in the class. Because the algorithm and the environment independent, the algorithm increases or decreases, the modification does not affect the environment and the client


22, TEMPLATE method--read "How to persuade girls to bed" This classic article? Girls from the realization to the upper
The constant steps of the bed are divided into coincidence, break the deadlock, pursue, kiss, foreplay, hands, Caress, go into eight big
Steps (Template method), but each step has a different approach for each situation, which depends on your
Improvise (concrete implementation);

Template method Pattern: Template method Schema prepares an abstract class that takes part of the logic in concrete ways and concrete constructs
, and then declare some abstract methods to force the subclass to implement the remaining logic. Different subclasses can be used without
Implement these abstract methods in the same way, thus having different implementations of the remaining logic. First make a top-level logical box
The logical details are left to the specific subclass to implement.

23, visitor-Valentine's Day to, to each mm send a bouquet of flowers and a card, but each mm sent flowers
For her personal characteristics, each card should be based on the characteristics of individuals to pick, I am a person who can make clear, or
Find Florist boss and gift shop owner do a visitor, let flower shop owner according to the characteristics of mm choose a bouquet of flowers, let gift shop
The boss also choose a card according to each person's characteristics, so it is much easier;

Visitor pattern: The purpose of the visitor pattern is to encapsulate some action that is applied to a data structure element. Once
These operations need to be modified so that the data structure that accepts the operation can remain intact. The visitor pattern applies to the number of
The structure is relatively undecided, and it frees the data structure and the interaction between the structure and the operation, making
The set of operations can evolve relatively freely. The visitor pattern makes it easy to add new operations by adding a
A new visitor class. The visitor pattern concentrates the behavior on a visitor object, rather than spreading it to a
In each of the node classes. When using the visitor pattern, put as much of the object navigation logic in the visitor class as possible
, instead of putting it in its subclass. The visitor pattern can span several classes of hierarchy access to different levels
The member class of the struct.

Interesting insights into java23 design patterns

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.