《 Design mode study room 》 series , seeing the name of a thing one thinks of its function , This series takes you through common design patterns .

however , In the beginning , I'd like to introduce the design pattern as a whole , Let us know why you want to learn design patterns .

So the main content of this article is :

  • My understanding of design patterns

  • The ultimate goal of design patterns : decoupling ( High cohesion and low coupling )

  • Classification of design patterns

  • The design principles that design patterns follow

  • Why do I often write code without design patterns ?

The article will be updated gradually On my blogs ( See the introduction at the end of the article ), I hope that all of you will be able to pay attention to my official account. : Back end technology , All the articles will be posted on it , Don't miss wonderful articles .

Why we need design patterns ?

My understanding of design patterns

When I just came into contact with the program , First heard “ Design patterns ” These four words are , I often think about a problem , Why is this thing so awkward . Just like I heard “ Discrete Mathematics ”,“ Concrete mathematics ” equally , There's a feeling that I can't feel my head .

With this question , I tried to read a few articles about design patterns , They all introduce design patterns like this :

Software design pattern (Design pattern), Also called design pattern , It's a set that's been used over and over again 、 Most people know that 、 Catalogued 、 Summary of code design experience . Design patterns are used for reusable code 、 Make code easier to understand 、 Ensure code reliability 、 Reusability of program .

After watching the , I'm even more confused . Looking at what singleton pattern , The code for the chain of responsibility model , I feel that the teacher has never mentioned these , Why write the code like this , It's good to write the function you want ???

Yes , Design patterns , For beginners , It's a bit of a castle in the air , Especially for people who have not been exposed to large projects , The code is so strange , It's even a little “ kick up a cloud of dust ”.

however , Life is often so much “ however ”! When you get started with programming , Come into contact with large , Complex , After the code that needs cooperation of many people , Looking back at design patterns , Often there is more and more clear understanding .

With my experience , Come back and review the design patterns , There's always a sense of awakening . Let's talk about my understanding of design patterns :

What is the English of design pattern ?Design pattern, This is a very accurate word , I think it's much better than Chinese translation .

Patten Chinese meaning :. n. Pattern ; pattern ; sample . vt. imitation

pattern It often means a set rule , You can see from its verbs that , He means to imitate , in other words , This code design , It's a series of experiences left by our predecessors , Just write code like this , It's often able to make your code , More concise , More robust , More elegant !

I think he should have a free translation of the name , be called : Best practices for code design !

Is it familiar to you , I remember that it was very popular recently 《 Alibaba Java Development Manual 》, And the classic 《Effective Java》 Do you ? They are all experience based knowledge , The goal is to help programmers write more elegant code !

If you haven't heard of the books above , No problem , Of course , You can also look at my previous article 《 Alibaba Java Development Manual 》 Reading notes , Get a general idea of what they're talking about , You'll understand what I'm saying “ They are a type of ”.

High cohesion and low coupling

Design pattern is a kind of code design idea , Its most essential purpose is to decouple , If you extend it a little bit , And for scalability and robustness , But it's all based on decoupling .

Some questions ,“ decoupling ” What does that mean ? Let's look at the following two concepts :

High cohesion

In the system A、B The two modules interact , If it changes A modular , It doesn't affect the module B The job of , So think A It's highly cohesive .


Low coupling

So when B When there is a change ,A The module still works , So think of it as A And B The coupling is low .


So decoupling , Essentially, it's about letting different blocks of code do their jobs , Mutual interference !

Classification of design patterns

Create pattern

The creation pattern abstracts the process of creating objects , It can also be understood as encapsulating the process of creating objects , As a client, you just need to use objects , Instead of the logic of creating objects .

  • Simple factory model (Simple Factory): The simple factory model is not GoF Summed up 23 One of the design patterns

  • Factory method model (Factory Method)

  • Abstract factory pattern (Abstract Factory)

  • Creator mode (Builder)

  • Archetypal model (Prototype)

  • The singleton pattern (Singleton)

Structural mode

Structural pattern is to solve how to assemble existing classes , Design the way they interact . for example : Extensibility ( appearance 、 form 、 agent 、 decorate )、 encapsulation ( Adapter 、 The bridge ). Because how to design the structure of an object 、 Inheritance and dependency affect the maintainability of subsequent programs 、 The robustness of the code 、 Coupling, etc .

  • Appearance mode / Facade mode (Facade Facade mode )

  • Adapter pattern (Adapter)

  • The proxy pattern (Proxy)

  • Decoration mode (Decorator)

  • Bridge mode / Bridging mode (Bridge)

  • Portfolio model (Composite)

  • The flyweight pattern (Flyweight)

Behavioral patterns

Behavior patterns describe complex control flows that are difficult to trace when a program is running , It can be further divided into behavior class pattern and behavior object pattern .

  1. The behavior class pattern uses inheritance mechanisms to assign behaviors between classes .

  2. The behavior object pattern uses object aggregation to allocate behavior . Some behavioral object patterns describe how a group of peer-to-peer objects collaborate to accomplish tasks that none of them can do alone .

  • Template method pattern (Template Method)

  • Observer mode (Observer)

  • The state pattern (State)

  • The strategy pattern (Strategy)

  • Responsibility chain pattern (Chain of Responsibility)

  • Command mode (Command)

  • Visitor mode (Visitor)

  • Mediator mode (Mediator)

  • Memo mode (Memento)

  • Iterator pattern (Iterator)

  • Interpreter mode (Interpreter)

The differences and connections between the three

Creating patterns provide a living environment , Structural patterns provide a reason for survival , Behavioral patterns provide how to survive .

The creative pattern provides an environment for the use of the other two patterns .

Structural patterns focus on the use of interfaces , All it does is interaction between objects or classes , Provide a door .

Behavioral patterns, as the name suggests , Focus on specific behaviors , So the concept of responsibility allocation and algorithm communication and so on .

Design principles

Design patterns are elegant code implementations , So we will pay attention to the standard design principles , The common design principles are as follows :

  • Opening and closing principle : Open to expansion , Turn off for changes

  • The Ritchie principle : Subclass inherits parent , It can run alone

  • Dependence Inversion Principle : Reference an object , If the object has an underlying type , Reference the underlying type directly

  • Interface isolation principle : Every interface should be a role

  • synthesis / Principle of aggregation and reuse : New objects should use some existing objects , Make it part of a new object

  • The Demeter principle : One object should know as little as possible about other objects

  Why do I often write code without design patterns ?

This point , I added it temporarily , Because I had this kind of confusion before . Looking at so many design patterns , Why don't I think of using it in my daily use ? I would like to give a few answers :

First of all , In our daily development , Often you use frameworks to build large projects , The framework has taken too much design into account for us , We've got it out of the box . So we often just need to CRUD( Additions and deletions ). In fact, the source code of the framework , A lot of design patterns are used , This is why we recommend a lot of big cow source code , We are often advised to look at design patterns first . Otherwise, when you look at the source code , It's going to be very hard , Because I don't know why I write code like this .

second , Design patterns are often used after your programming experience has accumulated to a certain extent , After a lot of problems , You will want to design a reasonable code structure to solve some common problems , Now , You'll start to think about using design patterns ! So don't rush , You can start without forcing design patterns on your projects , But it's important to understand the various design patterns first .

Third , If you're in a hurry to start practicing design patterns , that , Do a project that doesn't rely on a large framework at all , It can be a tool class , It can be a small function script , As long as you don't rely on complex frameworks , Soon you'll find out where design patterns can be applied in your code , Don't ask me how I know , Go and build your own wheel !


This paper gives an overview of what design patterns are , In the following article , We will introduce design patterns one by one , Coming soon !

Reference resources

Pay attention to me

I'm a back-end development engineer .

Focus on back-end development , Data security , Reptiles , The Internet of things , Edge calculation, etc , Welcome to exchange .

I can be found on all platforms

  • WeChat official account : Back end technology

  • Github:@qqxx6661

  • CSDN:@Rude3Knife

  • You know :@Zhendong

  • Simple books :@ There are three knives

  • Nuggets :@ There are three knives

The main content of the original blog

  • Java Review the whole manual for interview knowledge points

  • Design patterns / data structure Self-Study Room

  • Leetcode/ The finger of the sword offer Algorithm problem analysis

  • SpringBoot/SpringCloud Rookie introduction series

  • Reptile related technical articles

  • Back end development related technical articles

  • anecdote / Share good books / Personal interests

Official account number : Back end technology

official account : Back end technology .jpg

If it helps you , May as well collect , Coin-operated , forward , It looks like ~