Tsinghua graduates will take you to grind and master 23 design patterns, totaling 6.17G, and consolidate your development foundation

Preface

Software development is becoming more and more complex, and the requirements for software design are getting higher and higher, and the introductory skill of software design and architecture is to deeply understand and master design patterns. Therefore, the importance of design patterns is self-evident.

Many friends have realized the importance of design patterns and read a lot of books and materials. However, they often hear complaints like this: "I have read a lot of books on design patterns, and I think I understand them all, but I don’t know how How to use these design patterns in development", so that the design pattern is "looks very beautiful."

In fact, this is not the case. The reason for this situation is that these friends do not have a good understanding of design patterns, and they feel that they understand. In fact, they are still far away, and they have not "true" understanding and mastering design patterns.

This article is aimed at this situation, so that you can truly understand and master the 23 design patterns, and be able to use them flexibly, I hope you will like it! ! !

Tsinghua graduate student takes you to grind and master 23 design patterns, totaling 6.17G

table of Contents

Tsinghua graduate student takes you to grind and master 23 design patterns, totaling 6.17G

 

main content

Chapter 1 Basics of Design Patterns. What is a pattern? Taken literally, model means the meaning of model and template: style means way and method. Taken together, the so-called model is: a way or method that can be used as a model or template. Simply put, it is a way or method that can be used as a model, similar to the familiar example.

According to the above understanding, the design pattern refers to the design template, that is, the way or method of design.

Design pattern: Refers to a proven solution in software development to solve recurring, specific problems in a specific environment.

Tsinghua graduate student takes you to grind and master 23 design patterns, totaling 6.17G

Chapter 2 Simple Factory, Simple Factory is not a standard design pattern, but it is too common, simple and magical, so you need to master it, just as a warm-up exercise to learn design patterns. In order to maintain consistency, we try to follow the steps of learning other modes to learn.

Tsinghua graduate student takes you to grind and master 23 design patterns, totaling 6.17G

Chapter 3 Appearance Mode, Appearance Mode has the following advantages.

  • Loose coupling: The appearance mode loosens the coupling relationship between the client and the subsystem, making the modules inside the subsystem easier to expand and maintain.
  • Simple and easy to use : The appearance mode makes the subsystem easier to use. The client no longer needs to understand the internal implementation of the subsystem, nor does it need to interact with the modules inside many subsystems. It only needs to interact with the appearance, which is equivalent to the appearance The class provides a one-stop service for external clients to use the subsystem.
  • Better divide the level of access: By using Facade reasonably, it can help us to better divide the level of access. Some methods are external to the system, and some methods are used internally. The functions that need to be exposed to the outside are concentrated in the appearance, so that it is convenient for the client to use, and it also hides the internal details.

Tsinghua graduate student takes you to grind and master 23 design patterns, totaling 6.17G

Chapter 4 Adapter Mode (Adapter), the adapter mode has the following advantages.

  • Better reusability: If the functions are already there, but the interfaces are not compatible, then these functions can be better reused through the adapter mode.
  • Better scalability: When implementing the adapter function, you can call the function developed by yourself, thereby naturally expanding the function of the system.

 

Tsinghua graduate student takes you to grind and master 23 design patterns, totaling 6.17G

Chapter 5 Singleton Mode (Singleton)

1. The function of singleton mode

The singleton mode is used to ensure that only one instance of this class will be created during runtime. In addition, the singleton mode also provides a globally unique access point to this class instance, which is the getInstance method. Regardless of whether the lazy or hungry implementation method is adopted, this global access point is the same.

Regarding the singleton pattern, no matter what implementation method is adopted, it only cares about the creation of class instances and does not care about specific business functions.

2. The scope of the singleton pattern

That is, in what scope is it a singleton?

Observing the above implementation, we can know that the current singleton implemented in Java is within the scope of a virtual machine. Because the function of loading a class is a virtual machine, a virtual machine will create an instance of the class when it loads a singleton class through its own ClassLoader.

Tsinghua graduate student takes you to grind and master 23 design patterns, totaling 6.17G

Chapter 6 Factory Method

Function of factory method pattern

The main function of the factory method pattern is to allow the parent class to complete its own function calls without knowing the specific implementation: and the specific implementation is delayed to the subclass.

In this way, when designing, you don’t need to think about the specific implementation. If you need an object, return it through the factory method and return it. When you use these objects to implement functions, you still operate through the interface, which is similar to IoC/DI The thought of this will be introduced in detail later.

Tsinghua graduate student takes you to grind and master 23 design patterns, totaling 6.17G

Chapter 7 Abstract Factory Mode (Abstract Factory)

Function of abstract factory pattern

The function of the abstract factory is to create an interface for a series of related objects or interdependent objects. It must be noted that the methods in this interface are not arbitrarily stacked, but a series of related or interdependent methods. For example, the CPU and motherboard in the above example are all related to the assembly of a computer.

In a sense, the abstract factory is actually a product series or product cluster. The abstract factory in the above example can be regarded as a computer cluster, and each different installation scheme represents a specific computer series.

Tsinghua graduate student takes you to grind and master 23 design patterns, totaling 6.17G

Chapter 8 Builder Mode (Builder)

The main function of the generator mode is to build complex products, and it is a detailed and step-by-step construction product, that is, the generator mode focuses on solving the problem of constructing complex objects step by step. It is not enough to just understand the function of the generator mode.

To put it bluntly, the focus of the generator mode is to separate the construction algorithm from the specific construction implementation, so that the construction algorithm can be reused. The specific construction can be easily expanded and switched, so that it can be flexibly combined to construct different product objects.

Tsinghua graduate student takes you to grind and master 23 design patterns, totaling 6.17G

Chapter 9 Prototype Mode (Prototype)

The function of the prototype mode actually includes two aspects:

  • One is to create new object instances through cloning;
  • The other is to copy the values ​​of the prototype instance properties for the cloned new object instance.

The main function of the prototype mode is to create new object instances through cloning. Generally speaking, the data of the newly created instance is the same as the prototype instance. However, how to implement cloning needs to be implemented by the program itself. The prototype model does not have uniform requirements and implementation algorithms.

Tsinghua graduate student takes you to grind and master 23 design patterns, totaling 6.17G

Chapter 10 Mediator Mode (Mediator)

The function of the intermediary is very simple, it is to encapsulate the interaction between objects. If the operation of an object will cause changes in other related objects, or a certain operation needs to cause subsequent or joint operations of other objects, and the object does not want to handle these relationships by itself, then you can find an intermediary and put all Throw the trouble to it, only notify the intermediary when needed, and let the intermediary handle the rest.

Conversely, when other objects are manipulated, they may cause changes to this object, and this can also be done.

In the end, the objects are completely separated, and no one directly interacts with other objects. Then the mutual relations are all concentrated in the intermediary object, and all the objects are only communicating with the intermediary object, and there is no more between them. contact.

Encapsulating the interaction between all objects in the intermediary, invisibly can also get another advantage, that is, the ability to centrally control the interaction of these objects, so that when there are changes, it is very convenient to modify.

Tsinghua graduate student takes you to grind and master 23 design patterns, totaling 6.17G

Chapter 11 Proxy Mode (Proxy)

The proxy mode is to create a proxy object and use the proxy object to represent the real object. After the client gets the proxy object, it has no effect on the client, and it is used as if it were a real object.

When the client operates the proxy object, in fact, the function will eventually be completed by the real object, but it is operated through the proxy, that is, the client operates the proxy, and the proxy operates the real object.

It is precisely because there is a proxy object sandwiched between the client and the real object being proxied, which is equivalent to a transfer. Then there are many tricks to play during the transfer. For example, judge the permissions. If you don’t have enough permissions, don’t I've been transferred to you.

Tsinghua graduate student takes you to grind and master 23 design patterns, totaling 6.17G

Chapter 12 Observer Mode (Observer)

According to the definition of the model, there is a typical one-to-many relationship between the target and the observer.

However, it should be noted that if there is only one observer, it is also possible. In this way, the one-to-one relationship between the target and the observer is realized in disguise. This also makes the state change of one object affect the other in the process. For an object, you can also consider using the observer mode.

Similarly, an observer can also observe multiple targets. If the notification update method defined by the observer for multiple targets is the update method, this will cause trouble, because it needs to receive notifications from multiple targets, if it is one by one An update method needs to be distinguished within the method. In the end, which target the update notification comes from, and different targets have different follow-up operations.

Under normal circumstances, observers should define different callback methods for different observer targets. This is the simplest implementation and does not need to be distinguished within the update method.

Tsinghua graduate student takes you to grind and master 23 design patterns, totaling 6.17G

Chapter 13 Command Mode (Command)

1. The key to command mode

The key to the command mode is to encapsulate the request into an object, which is a command object, and define a unified interface for performing operations. This command object can be stored, forwarded, recorded, processed, cancelled, etc. The entire command mode is around This object is in progress.

2. Assembly and call of command mode

In the command mode, there is often a command assembler who uses it to maintain the relationship between the "virtual" implementation of the command and the real implementation. If it is a super-intelligent command, that is to say, the command object is completely implemented by itself and does not need a receiver, that is the degradation of the command mode. There is no need for a receiver, and naturally there is no need for an assembler.

The real user is to specify the content of the request, and then submit the request to trigger it. The real user will trigger the command through the Invoker.

In the actual development process, Client and Invoker can be integrated together. When the client uses the command mode, the command object and receiver are assembled first. After the assembly is completed, the command execution request can be invoked.

Tsinghua graduate student takes you to grind and master 23 design patterns, totaling 6.17G

Chapter 14 Iterator Mode (terator)

The function of the iterator pattern is mainly to provide iterative access to aggregated objects. Iterators focus on this "access" and extend many functions. For example: traverse the aggregate object in different ways, such as forward, backward, etc.

■ Perform multiple traversals on the same aggregation at the same time.

■ Use different traversal strategies to traverse the aggregation, such as whether filtering is required.

The meaning of polymorphic iteration is to provide a unified iterative interface for different aggregation structures, that is to say, different aggregation structures can be accessed through an iteration interface, which is called polymorphic iteration. The above example has achieved polymorphic iteration. In fact, the implementation of the standard iteration mode basically supports polymorphic iteration.

Tsinghua graduate student takes you to grind and master 23 design patterns, totaling 6.17G

Chapter 15 Composite Mode (Composite)

The purpose of the combined mode is to let the client no longer distinguish whether the operation is a combined object or a leaf object, but to operate in a unified way.

The key to achieving this goal is to design an abstract component class so that it can represent composite objects and leaf objects. In this way, the client does not need to distinguish between composite objects or leaf objects, and only needs to treat them all as component objects for unified operations.

Tsinghua graduate student takes you to grind and master 23 design patterns, totaling 6.17G

Chapter 16 Template Method

The function of the template method mode is to fix the algorithm skeleton, and make the specific algorithm scalable.

This is very extensive in practical applications, especially when designing framework-level functions. The framework defines the steps of the algorithm, and allows developers to expand at appropriate points to implement specific algorithms. For example, design a general addition, deletion, modification, and check function in DAO implementation. This will give you an example later.

The template method pattern also provides an additional benefit, that is, you can control the expansion of subclasses. Because the algorithm steps are defined in the parent class, the method implemented by the subclass is only called at certain fixed points, so it is only allowed to extend the function at these points. These methods that can be overridden by subclasses to extend functionality are usually called "hook" methods, and examples will be given later.

Tsinghua graduate student takes you to grind and master 23 design patterns, totaling 6.17G

Chapter 17 Strategy Mode (Strategy)

The function of the strategy mode is to separate the implementation of specific algorithms from specific business processing, and implement them as separate algorithm classes, thereby forming a series of algorithms and allowing these algorithms to be interchangeable.

The focus of the strategy model is not how to implement algorithms, but how to organize and call these algorithms, so that the program structure is more flexible, with better maintainability and scalability.

Tsinghua graduate student takes you to grind and master 23 design patterns, totaling 6.17G

Chapter 18 State Mode (State)

The so-called state of an object usually refers to the value of the attribute of the object instance: behavior refers to the function of the object, and to be more specific, most behaviors can correspond to methods.

The function of the state mode is to separate the behavior of the state, and to call different functions corresponding to different states by maintaining the changes of the state.

In other words, state and behavior are related, and their relationship can be described as: state determines behavior.

Since the state is changed during the runtime, the behavior will also change during the runtime according to the state change. It seems that the same object, at different runtimes, behaves differently, just like the class is modified same.

Tsinghua graduate student takes you to grind and master 23 design patterns, totaling 6.17G

Chapter 19 Memento Mode (Memento)

The function of the memo mode, first of all, is to capture the internal state of an object without destroying the encapsulation.

There are two points to pay attention to here. One is not to destroy the encapsulation, that is, the object cannot expose details that it should not expose; the other is to capture the internal state of the object, and it is usually the internal state of the object at a certain time during operation.

Why capture the internal state of this object? What is the use of capturing this internal state?

Tsinghua graduate student takes you to grind and master 23 design patterns, totaling 6.17G

Chapter 20 Flyweight

The focus of the Flyweight model design is to separate change and change. The state of an object is divided into an internal state and an external state. The internal state is constant and the external state is variable. Then by sharing the unchanging part, the goal of reducing the number of objects and saving memory is achieved. When the flyweight object needs it, it can pass in external state to the shared object from the outside, and the shared object will use its own internal state and these external states when processing functions.

In fact, separating change and unchanging is one of the most basic ways in software design, such as reserving an interface. Why do you want to reserve an interface in this place?-A common reason is that there are changes here, which may need to be expanded or expanded in the future. It is to change the existing implementation, so the interface is reserved as a "guarantee of pluggability."

Tsinghua graduate student takes you to grind and master 23 design patterns, totaling 6.17G

Chapter 21 Interpreter Mode (Interpreter)

Interpreter mode uses interpreter objects to represent and process corresponding grammatical rules. Generally, an interpreter processes a grammatical rule. In theory, as long as the interpreter object can be used to express grammatical expressions, and can form an abstract syntax tree, it can be processed using the interpreter mode.

Tsinghua graduate student takes you to grind and master 23 design patterns, totaling 6.17G

Chapter 22 Decoration Mode (Decorator)

The decoration mode can dynamically add functions to an object, which is to add functions to an object from outside the object, which is equivalent to changing the appearance of the object. After decoration, from the perspective of the external use system, the original object is no longer used, but the object that has been decorated by--series of decorators.

In this way, the function of an object can be changed flexibly. As long as the dynamically combined decorator changes, the function of the final object will also change.

Another benefit is obtained in disguise, that is, the multiplexing of decorator functions. You can add the same decorator to an object multiple times, or you can use the same decorator to decorate different objects.

Tsinghua graduate student takes you to grind and master 23 design patterns, totaling 6.17G

Chapter 23 Chain of Responsibility (Chain of Responsibility)

The chain of responsibility model is mainly used to deal with the situation that "the client sends a request, and multiple objects have the opportunity to process this request, but the client does not know who will handle his request". That is, the requester and the receiver need to be decoupled, so that the receiver can be dynamically switched and combined.

If you want to transform the use of the responsibility chain, you can let the request continue to pass, and each responsibility object performs certain functional processing on this request, thereby forming a functional chain for processing the request.

Tsinghua graduate student takes you to grind and master 23 design patterns, totaling 6.17G

Chapter 24 Bridge Mode (Bridge)

In bridging mode, the concept of bridging is not well understood. What is bridging? Why do we need to bridge? How to bridge? When these problems are clarified, we will basically understand the meaning of bridging.

Let's take a look at what is bridging one by one? The so-called bridging, in layman's terms, is to build a bridge between different things so that they can be connected and communicate and use each other. So what exactly is bridged in the bridging mode? It is to bridge the separated abstract part and the realization part, such as the abstract message and concrete message sending in the previous example.

Tsinghua graduate student takes you to grind and master 23 design patterns, totaling 6.17G

Chapter 25 Visitor Mode (Visitor)

The visitor mode can transparently add new functions to a series of objects, so as to avoid modifying the series of objects during maintenance, and it can also realize the functions of the visitors in a disguised form.

Because it is a series of object operations, this also leads to some trouble if you only want to add functions to some of the objects in a series of objects; and you must always ensure that this series of objects are called, no matter if it is a loop, Still recursive, in short, every object must be accessed.

Tsinghua graduate student takes you to grind and master 23 design patterns, totaling 6.17G

This [grinding 23 design patterns] has a total of 802 pages. Friends who need the full version can forward this article to follow the editor, check the picture at the end of the article or private message editor [Technology] to get it! ! !

There are also 6.17G 23 design pattern videos and documents to learn, I hope everyone will like it! !

Tsinghua graduate student takes you to grind and master 23 design patterns, totaling 6.17G

 

Tsinghua graduate student takes you to grind and master 23 design patterns, totaling 6.17G

 

Learn 23 design patterns well, and you won’t be afraid to travel around the world! ! The editor can only help everyone here, I hope everyone can cherish this learning opportunity! ! If you want to get deep learning, please check the picture below, scan and add~~~

 

Guess you like

Origin blog.csdn.net/Java_msb666/article/details/108753651