What is design pattern ？
Patterns are a reusable solution , It can be used to solve common problems encountered in project development and design , We're writing
Most patterns have formal descriptions , In order to use in different situations . The description of a pattern usually includes the following parts ：
- Briefly describe the problem and solution .
- It will further explain the problem and explain how patterns provide solutions .
- Show each part of the pattern and the relationship between them .
- Implementations in different languages provide code for popular programming languages , Let the reader better understand the thought behind the pattern .
Some other practical details are listed in part of the pattern introduction , For example, the applicability of patterns 、 Implementation steps and relationships with other patterns .
Design pattern categories
Design patterns are divided into three types according to their types , Create a model of design 、 Structural design pattern 、 Behavioral design patterns , There are more design patterns in the three types of design patterns .
One 、 Create a model of design
Creative design pattern is a kind of design pattern dealing with object creation , Control the creation of objects in some way to avoid design problems or increase design complexity when creating basic objects .
Creative models include ：
- Simple factory - Define an object's factory interface , Postpone the actual creation of product objects to specific sub factory classes
- Factory method - Generate an instance of several derived classes based on interface data or events
- Abstract factory - Create an instance of several series , There is no need to elaborate on the role of specific classes
- Prototype - An instance used to replicate or clone complete initialization
- Single case - A class has only one instance globally
- generator - Separate object construction from representation , Use the same creation code to generate different types and forms of objects
Two 、 Structural design pattern
Structural patterns are concerned with object composition , It is usually possible for users to find simple ways to establish relationships between different objects . This pattern helps to ensure that when a part of the program changes , The whole structure of the system does not need to be changed at the same time , At the same time, for parts of the system that are not suitable for a particular purpose , This model can also help them restructure .
Creative models include ：
- Decorator - Dynamically add alternative processing methods to objects
- appearance - The only class that hides the complexity of the entire subsystem
- Enjoying yuan - An example for efficient sharing of information contained elsewhere , Load more objects in limited memory capacity
- Adapter - Match interfaces of different classes , So classes can work together in the case of incompatible interfaces
- agent - The placeholder object represents the real object , Provide a substitute for an object or its place holder , The proxy controls access to the original object , And allows some processing before and after submitting the request to the object
- Bridging mode - A large class or a series of closely related classes can be divided into two independent hierarchies: abstraction and implementation
- Portfolio model - Combine objects into a tree structure , And you can use them as if you were using stand-alone objects
3、 ... and 、 Behavioral design patterns
Behavior patterns focus on improving or simplifying communication between different objects in a system .
Behavior patterns include ：
- iterator - Sequential access to elements in a collection , There is no need to understand the internal principles of the set
- Intermediary - Define simplified communication between classes , To prevent a set of classes from showing references to each other , Force them to cooperate through an intermediary object
- The observer - Notification of changes to multiple classes , To ensure consistency between classes
- The visitor - Separate the action of algorithmic mood
- Responsibility chain - Send along the chain of processors , Upon receipt of the request , Each handler requests processing from the visiting team , Or pass it on to the next handler on the chain
- Command mode - Convert the request into a separate object that contains all the information related to the request , According to different request methods , A parameterized , Delay request execution or put request execution in the queue , And can implement the revocable operation
- Memorandum - Save and restore the previous state of the object without exposing the implementation details of the object
- The state pattern - Another internal state change of an object is to change its behavior , Make it look like it has changed its own class
- The strategy pattern - Define a set of algorithms , And put each algorithm into a separate class , So that the objects of the algorithm can be replaced with each other
- Template method - Define an algorithm framework in a superclass , Allow subclasses to override specific steps of the algorithm without modifying the structure
Superclass ： The inherited class is generally called “ Superclass ”, It's also called a parent . It's a very important concept in inheritance , Together with subclasses, it vividly describes the hierarchy of inheritance . Good and bad of super class design , First of all, look at the internal reuse rate , High internal reuse rate , Inevitably, the external reuse rate is also high .
What are the benefits of design patterns
Since learning design patterns , Of course, it's also about our work , There are many benefits , Otherwise, the value of design patterns will not exist . Design patterns don't just bring us good coding standards , Perhaps the most important of these is thought .
Appreciation and pay rise
Almost all job interviews and reviews about programming have questions about patterns . Understanding this knowledge can help you discover a wider range of job opportunities , Or achieve the goal of promotion and salary increase .
Enhance code quality
Patterns allow you to customize existing solutions , Instead of completely self-developed . There will be fewer errors in the code , Because you're using a proven standard solution , It takes into account all the hidden problems .
Good for teamwork
Design patterns define a common language that allows you and your team members to communicate more efficiently . You just tell the programmer the name of the pattern , And you don't have to explain your brilliant design ideas and the role of each of them . It's easy to communicate with colleagues . You just have to say “ Oh , A single example is enough here ”, Everyone will understand the idea behind this suggestion . Just know the pattern and its name , You don't have to explain what a singleton is .
Faster problem solving
Design patterns are toolkits for common problems in software design , The tools are proven solutions . Even if you've never had these problems , Understanding patterns is still very useful , Because it can guide you how to use object-oriented design principles to solve various problems .
Antipatterns are patterns in software development that are considered bad programming practices . It's used to solve common problems and bring bad solutions . Unlike design patterns , Design patterns are a common way to solve common problems , These common questions have been formalized , It is generally considered a good development practice , The antipattern is the opposite , Is not desirable .
Some people think that antipatterns are due to the wrong use of commonly used design patterns , Some people think that anti pattern is just a bad habit . In a nutshell , Antipatterns are inefficiencies that are often used to deal with problems that are often faced , Bad , Or design patterns to be optimized / Method . even to the extent that , Antipatterns can also be a wrong development idea / idea .
There are compelling reasons behind the popularity of antipatterns , But antipatterns have a more serious impact on maintainability and the long-term development of software . In terms of technical debt , Every time you choose a shortcut, there is an implied cost , And these costs will have to be paid back at some point in the future . Those delayed refactorings will not only affect the next change , And it will continue to add interest like economic debt .
This article mainly makes some simple understanding to the design pattern , The next period of time, the author will update from time to time , As mentioned above
23 Design patterns . Let's learn about design patterns in the next few days .
Don't get too tangled in the code in the learning process , Learn and understand the thoughts , Master and be able to use these ideas in practical development projects , You'll find yourself growing up a lot .