“
Hello, everyone. I’m One. This is my first article with the Nuggets.
【 Vernacular design Pattern 】 Official opening, a total of 23 design patterns each use vernacular + diagram to explain to everyone, and then illustrate business scenarios, really do learn to apply.
This article is a navigation, mainly introduces the design pattern and 23 design patterns in one sentence interpretation.
“
preface
These are some of the most common questions you’ll hear in an interview:
Tell me about some design patterns you have used.
What design patterns are used in Spring?
These are common interview questions, but it’s not easy to answer them well.
First of all, there are 23 design patterns, and the official definition of the back of the chair can leave an interviewer speechless with a single question,
Secondly, even if you have several years of experience in development, you may not really have used design patterns in development. As long as the interviewer mentions business scenarios, he will be ignorant!
So, for a column that determines a design pattern, make it clear that each design pattern has at least one business scenario. Apply what you learn to your practice.
First glimpse of design patterns
What are design patterns
Who first put forward the design pattern?
In 1994, four authors, Erich Gamma, John Friesides, Ralph Johnson, and Richard Helm, published Design Patterns: The Foundations of Reusable Object-oriented Software, which applied the concept of design patterns to the field of program development. The book, which provides 23 patterns for solving problems in object-oriented programming, quickly became a bestseller. The title was so long that it was shortened to “The Gang of Four (GoF) Book”, and was soon further simplified to “THE GoF book”.
What are design patterns
In a word: Design patterns are typical solutions to common problems in software design. They are like prefabricated blueprints that can be adapted to meet requirements and can be used to solve recurring design problems in your code.
How is it different from an algorithm
Both are generic solutions to problems. The difference is:
Algorithms are more like recipes: they provide specific steps to achieve a goal. Patterns, on the other hand, are more like blueprints: you can see the end result and the functionality of the pattern, but you need to determine the implementation steps yourself.
Classification of design patterns
“
As long as it helps us understand and remember design patterns as a whole, there are 23 of them, and it’s not easy to remember their names.
“
Divide according to purpose
- The creation pattern describes how objects are created. Its main feature is the separation of object creation and use.
- Structural pattern: Describes how objects and classes can be assembled into a larger structure while keeping the structure flexible and efficient.
- Behavioral patterns: Describe how classes or objects work together to accomplish tasks that a single object could not accomplish alone, and how responsibilities are assigned.
According to the scope of action
- Class pattern: Used to deal with relationships between classes and subclasses, which are established by inheritance and are static and determined at compile time.
- Object pattern: Used to deal with relationships between objects, which can be implemented through composition or aggregation, that can change at run time and be more dynamic.
One list
Scope/Objective | Creation pattern | Structural mode | Behavioral pattern |
---|---|---|---|
Class model | The factory method | (Class) adapter | Template methods, interpreters |
Object pattern | Singleton prototype abstract factory builder | The broker (object) adapter Bridges the decorative appearance share element combination | Policy command responsibility chain status observer mediator iterator visitor memo |
Benefits of using design patterns
Perhaps you have been developing programs for years and have no idea what a singleton pattern is. A lot of people do. Even so, you may have used some design patterns without even knowing it. So why not take some time to learn more about them?
- Design patterns are toolboxes for common problems in software design, and the tools are proven solutions. Even if you have never encountered these problems, knowing patterns is still very useful because it can guide you in how to use object-oriented design principles to solve various problems.
- Design patterns define a common language that allows you to communicate more effectively with your team members. Just say, “Oh, let’s use singletons here,” and everyone will understand the idea behind the suggestion. As long as you know the schema and its name, you don’t need to explain what a singleton is.
One-sentence interpretation
“
To the last dish, a table, a word for each design pattern, do not understand you to the public number to find me.
“
Design patterns | Standard definition | Easy to interpret | Detailed interpretation of the |
---|---|---|---|
The factory methodFactory Method |
Define an interface for creating objects and let subclasses decide which class to instantiate. | Arsenal: Here you goM416 You took it and ate the chicken; Give your girlfriend a handAK47 Fall into a box. |
pending |
The buildersBuilder |
Separating the construction of a complex object from its representation allows the same construction process to create different representations. | Production line: it used to be the handicraft industry, where the individual production parts were assembled step by step by man. Later, with the industrial revolution, it was replaced by the production line. | pending |
The prototypePrototype |
Specify the type of object to create with a prototype instance, and create new objects by copying the prototype. | Printing: in the past can only be pasted copy time-consuming and laborious, the efficiency is very low, with printing, sudden. | pending |
The singletonsingleton |
Ensure that a class has only one instance and provide a global access point to access it. | Make sure you are unique: you have 6 cats, they come to you when they are hungry, you are the only one, there will be no second. | pending |
The adapterAdapter |
Translate the interface of a class into another interface that the customer wants. Enables classes that would otherwise not work together due to interface incompatibilities to work together. | When you go outside America, you can only speak Chinese, the local people can only speak English, and the guide can meet Adapter | pending |
A decoratorDecorator |
Add some extra responsibilities to an object dynamically. It is more flexible than subclassing in terms of extending functionality. | Clothes: people rely on clothes horse saddle, dress up, do some decoration, much better | pending |
The bridgeBridge |
Separate the abstract part from its implementation part so that they can all change independently. | Horses are black and white, male and female. saidThis is a horse Too abstract, sayThis is a black stallion I’m too rigid. I just talkThis is a (black or white) (male or female) horse Is flexible and elegant. |
pending |
combinationComposite |
Group objects into a tree structure to represent a partial-whole hierarchy. Make customer use of single objects and composite objects consistent. | Great family: children have grandchildren, grandchildren have grandchildren, children and grandchildren are endless, and the realization of this model is to organize the numerous and diverse population into a great family arranged in order of generations. | pending |
appearanceFacade |
Provide a consistent interface for a set of interfaces in a subsystem, and define a high-level interface that makes the subsystem easier to use. | Seeking common ground while reserving differences: Middle school and high school are required for high school graduates, as well as for PHDS, so the country has made middle school and high school compulsory education for all. | pending |
The flyweightFlyweight |
Use sharing techniques to effectively support a large number of fine-grained objects. | Once and for all: know 3000 Chinese characters, can cope with daily reading and writing, we can see the importance of the existence of this Chinese character library in mind. | pending |
The agentProxy |
Provide a proxy for other objects to control access to this object. | Administration from behind a curtain: just like the four government ministers in the Kangxi years of the Qing Dynasty, many rights are not in the hands of the emperor, must be handled by the minister for assisting. | pending |
Chain of responsibilityChain of Responsibility |
To decouple the sender and receiver of a request so that multiple objects have the opportunity to process the request. Connect the objects into a chain and pass the request along the chain until an object handles it. | Rent a house: in order to find a house before everywhere to inquire, now only need to find a housing intermediary, you find an intermediary, intermediary find the landlord, until you rent a satisfactory house. | pending |
The commandCommand |
Encapsulate a request as an object, allowing you to parameterize clients with different requests. Queue or log requests, and support cancelable operations. | Borrow knife to kill: before is want to kill who kill, oneself start work, lead to enmity too much, then false hand others, sow discord between others so as to achieve their own goals. | pending |
The interpreterInterpreter |
Given a language, define a representation of its grammar and define an interpreter that uses that representation to interpret sentences in the language. | Classical Chinese commentary: a passage of classical Chinese, translated into vernacular Chinese. | pending |
The iteratorIterator |
Provides a way to access the elements of an aggregate object sequentially without exposing the object’s internal representation. | Kill them all: Search them one by one, and never drop them. | pending |
brokerMediator |
A mediation object encapsulates a set of object interactions. The mediator loosens the coupling by eliminating the need for objects to explicitly refer to each other, and can change their interactions independently. | Triangle debt: the original relationship of numerous debts, suddenly out of an intermediary, everything, so the triangle relationship has become an independent tripartite seeking a fourth intermediary relationship; | pending |
The memoMemento |
Capture the internal state of an object and store the state outside of the object without breaking encapsulation. This allows you to restore the object to its saved state later. | Notepad: poor memory, write down what happened. | pending |
The observerObserver |
Define a one-to-many dependency between objects so that when an object’s state changes, all dependent objects are notified and refreshed automatically. | Watchman: Once the watchman has any abnormal situation, he will respond in time. | pending |
stateState |
Allows an object to change its behavior when its internal state changes. Object appears to have modified the class to which it belongs. | Publish a blog: a blog has a draft, pending review, and has been published in three states, in which the release has different behavior. | pending |
strategyStrategy |
Define a set of algorithms, encapsulate them one by one, and make them interchangeable. This pattern makes changes to the algorithm independent of the customers using it. | Long illness into good doctor: the person is ill can have all sorts of symptoms, but through long-term groping, can sum up a cold, lung disease, hepatitis and so on a few kinds; | pending |
Template methodTemplate Method |
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Allows subclasses to redefine specific steps of an algorithm without changing its structure. | Theory doesn’t have to be practice: the coach’s students can swim, and it doesn’t matter if the coach can. | pending |
The visitorVisitor |
Represents an operation that operates on elements in an object structure. It allows you to define new operations on elements without changing their classes. | Selling insurance: A good insurance agent can always offer different policies for different types of groups. | pending |
“
The above is a simple interpretation of the 23 design patterns, with more detailed code related to business scenarios, one of which will be updated gradually, and the corresponding article address will be updated in this table.
Anxious to see, welcome to a public number an IT urges.
“
I am a computer programmer in the Internet.
When the path is long and obstructed, it will come. Everyone’s [likes, collections, attention] is a creation of the biggest power, we will see you next time!
Note: If you have any questions or suggestions about this blog, please leave comments!