preface

What are design patterns?

The Design pattern represents the best experimental summary and is usually adopted by experienced software engineers. Design pattern is a solution to the common problems faced by software engineers during software development. These solutions were developed over a long period of trial and error by quite a few software developers.

What is the purpose of design patterns? Why study design patterns?

To help us organize our applications into an easy-to-understand, maintainable, resilient architecture and build maintainable OO systems, the key is to keep in mind the changes that the system may need in the future and the principles for dealing with those changes.

1. Design mode can facilitate communication between professionals, as follows:

Programmer A: HERE I use the XXX design pattern

Programmer B: Then I have a rough idea of the design of your program

2, easy to maintain

Project Manager: Today the customer has such a demand…

Programmer: I see, I used XXX design pattern here, so it’s quick to change

Design pattern is the summary of programming experience

Programmer A: B, how did you get the idea to build your code this way

Programmer B: After I learned the XXX design pattern, it seemed natural to write this way to avoid some problems

4. Learning design patterns is not necessary and can be replaced by other methods

Programmer A: B, are you using the XXX design pattern for this code?

Programmer B: Sorry, I haven’t studied design patterns, but my experience tells me to write like this

Design patterns focus on semantics rather than syntax

A design pattern can be implemented using multiple grammars

Classification of design patterns

1. Create the schema

These design patterns provide a way to hide the creation logic while creating objects, rather than instantiating objects directly using the new operator. This gives the program more flexibility in deciding which objects to create for a given instance. Factory Pattern

Abstract Factory Pattern

Singleton Pattern

Builder Pattern

Prototype Pattern

2. Structural mode

These design patterns focus on combinations of classes and objects. The concept of inheritance is used to combine interfaces and define how composite objects acquire new functionality. Adapter Pattern

Bridge Pattern

Filter Pattern (Filter, Criteria Pattern)

Composite Pattern

Decorator Pattern

Facade Pattern

Flyweight Pattern

Proxy Pattern

3. Behavioral patterns

These design patterns are particularly concerned with communication between objects. Chain of Responsibility Pattern

Command Pattern

Interpreter Pattern

Iterator Pattern

Mediator Pattern

Memento Pattern

Observer Pattern

State Pattern

Null Object Pattern

Strategy Pattern

Template Pattern

Visitor Pattern

4 j2ee patterns

These design patterns pay particular attention to the presentation layer. These patterns were identified by the Sun Java Center. MVC Pattern (MVC Pattern)

Business Delegate Pattern

Composite Entity Pattern

Data Access Object Pattern

Front Controller Pattern

Intercepting Filter Pattern

Service Locator Pattern

Transfer Object Pattern

Concrete design pattern

The Abstract Factory pattern provides an interface for creating a series of related or interdependent objects without specifying their concrete classes.

Objectives and Benefits

First, the factory pattern is for decoupling: the process of creating and consuming objects is separated. If Class 0 wants to call Class 1, then 0 just calls the method of 1, and the instantiation of 1 is left to the factory Class.

Second, the factory pattern reduces code duplication. If the process of creating object B is complicated, requires a certain amount of code, and is used in many places, then there will be a lot of duplicate code. We can put the code that created object B in a factory and manage it all together. It not only reduces duplicate code, but also facilitates modification and maintenance of the creation process of B in the future.

Since the creation process is centrally managed by the factory, there is no need to find all the places where B needs to be created to correct one by one when business logic changes, but only need to modify in the factory to reduce maintenance costs. Similarly, if you want to change all calls of 1 to subclass 11 of B, you just need to change the object that produces B to B1 in the corresponding factory or factory method, rather than finding all new 1() to new 11().

In addition, because the factory manages the creation logic of objects, users do not need to know the specific creation process, but can just use it, reducing the errors caused by the creation logic of users.

Popular speaking

One of the most important principles of design patterns is: don’t change code, just add code. All old code is valuable and should be preserved as much as possible

When an object is new, the process of new is valuable knowledge of how to create old objects (some new is very complex, including many parameters). If the code is modified, then the old objects are not known how to use, and the whole system is broken

So to preserve the new process of the old object, save the new process and distribute it in a series of factory classes, which is called the factory pattern, generally there are three ways to encapsulate

Simple factory: Put object creation in a factory class and create different objects with parameters.

This disadvantage is that every time you add an object, you need to modify the simple factory (even though it’s not deleting the code, just adding a switch case, but still violating the “no change code” rule).

Factory method: Each product is created by one factory, and one factory holds a New

Basically perfect, completely following the “don’t change the code” principle

Abstract factory: A simple complication of the factory method that holds multiple new instances

It’s for big projects

Factory pattern is the most typical model of creating model, is mainly used to create objects, reduce the new when we in the use of an object () operation, I believe that everyone have such confusion, now my project is in the process of program development, there are so many new () operation appeared in the presentation layer, not through the factory to create the object, On the one hand, it may be because we are lazy and do not standardize the coding form of the project. On the other hand, it is also due to the tight schedule of the project and there is not so much time to go

Complete the unified creation of the factory, of course, for such a dynamic creation of the object factory, the recommended practice is the creation pattern we will cover later, abstract Factory Pattern.

If you don’t know the factory pattern is used to do, we can be illustrated by the following example, we now have a mineral water plant, for example, processing of mineral water, we now know that there are mineral water this object, so when I batch production of mineral water, we can through the factory to mass production, is equal to the bulk of our program to create objects. I had a bunch of subjects, a bunch of tourists, and they each asked for a bottle of mineral water, and if you think of tourists as different app modules,

In this case, when different tourists need mineral water, I will create () a mineral water and I find a processing plant to produce mineral water is obviously different, at this time, tourists should not have mineral water

And tourists don’t know how mineral water is produced, and they don’t need to care about these things.

When designing object-oriented programming, the principle is low coupling, object to object. Then the reference relationship between objects can be decoupled by abstracting the interface and using the excuse of dependency to reduce the coupling of the system.

Do Java people, who does not know horse soldiers?

I still remember ten years ago when I started to learn Java, he was the one who led me into the pit, haha.

Recently, when I was chatting with Mr. Ma, I got his latest videos of “23 Design Modes” and “Algorithms”.

I opened it and looked at it carefully. I felt it was the most complete [design mode]. For example, in the factory mode, Teacher Ma directly talked about the principle of SpringIOC.

For example, in the dynamic proxy mode and Visitor mode, Mr. Ma directly talked about ASM

Friends who are interested in teacher Ma’s design mode video can help forward the article and follow the private reply [learning] to get it for free

Let’s go over Mr Ma’s catchphrase again:

“Here, look at my desk.”

“The bean sprouts grow a room high, it is also a vegetable.”

“Ride the donkey to find the horse, but don’t mistreat the donkey, don’t make trouble with the company, the company will not give you a hard time.”

“Class, use your thighs to think about which one is the most… ? Yes, that’s right, everybody’s got big thighs.”

“Vladimir Radmanovic is simply Lenin.”

Teacher Ma’s video is very good, don’t you think?