All excerpts from This is How Design Patterns should Be Learned

This article has been serialized since October 29, 2012. Please stay tuned to….

The preface

Design Patterns: Elements of Reusable Object-oriented Software (Design Patterns), It is written by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides (Addison-Wesley, 1995). These Four authors are often referred to as the Gang of Four, and this book is known as the GoF book. For the first time, they gave us a set of experiences that can be used repeatedly in software development, helping us improve the reusability of code, the maintainability of system, etc., and solving complex problems in software development.

Design patterns have been in existence for more than 20 years, during which numerous classic works on design patterns have been published. If GoF’s Design Patterns is the “Bible” in the field of design patterns, then the various subsequent books on design patterns can be called “annotated editions” or “vernacular editions” of the bible. This book is based on GoF’s Design Patterns.

Design Patterns is a book of lessons. Don’t memorize them. Here’s a general preview of the classification and summary of design patterns, as shown in the table below.

Divide class explanation For example
Creational Design Patterns This type of design pattern provides a way to hide the creation logic while creating objects, rather than instantiating them directly with new operators, giving programs more flexibility in deciding which objects to create for a given instance Factory Method Pattern,Abstract Factory Pattern,Singleton Pattern,Prototype Pattern,Builder Pattern
Structural design Pattern This type of design pattern focuses on combinations of classes and objects. The concept of inheritance is used to combine interfaces and define how composite objects acquire new functionality Proxy Pattern,Facade Pattern,Decorator Pattern,Flyweight Pattern,Composite Pattern,Adapter Pattern,Bridge Pattern
Behavioral design Patterns This type of design pattern is particularly concerned with communication between objects Template Method Pattern,Strategy Pattern,Chain of Responsibility PatternIterator Pattern, Command Pattern, State Pattern, Memento Pattern, Mediator Pattern Interpreter Pattern, Observer Pattern, Visitor Pattern

This article is my “Bible” practice of the essence of the summary, the full content is excerpted from the “Design pattern should be learned”, this is a can really be ground “design pattern” book, is currently the only combination of framework source code how to ground “design pattern” this Angle to understand the design pattern book. This paper will also be combined with JDK, Spring, MyBatis, Tomcat, Netty and other classic framework source code to expand the analysis of the design pattern. , of course, this article also combined with my years of experience in “trample hole filling holes” and “answering questions of teaching experience, with more than the” bible “, more comprehensive, more popular, more lively, more interesting, more ground gas way and combining with the real business scenario analysis the advantages and disadvantages of each design pattern, cure “design mode selection difficult disease”. Choosing a design pattern is like choosing a partner on a blind date. Once you’re ready to accept their flaws, they belong to you. Therefore, this article is more instructive for daily development.

Tom plays architecture, only play dry goods without water, all the content to share in this article is from the actual combat perspective, do not talk about the concept, only about actual combat and application

Summary of frequency of use of various design modes

The following is a summary of the frequency of use of design patterns based on my personal experience, which is not academic basis but for your reference only. Because the choice of design pattern is still dependent on the specific business scenario, everyone is in contact with different business areas, the choice of natural design pattern will be different.

1.1 Creative design pattern

As shown in the figure below, the use frequency of creative design pattern from high to low is factory method pattern, abstract factory pattern, Builder pattern, singleton pattern and prototype pattern. Archetypal patterns tend to have off-the-shelf toolclasses, and it’s rare to build your own wheels.

1.2 Structural design mode

As shown in the figure below, the frequency of structural design mode is adapter mode, decorator mode, agent mode, facade mode, combination mode, share mode and bridge mode in descending order. The bridge mode generally has a ready-made tool class, the case of their own wheels is relatively rare.

1.3 Behavioral design pattern

As shown in the figure below, the frequency of use of behavioral design patterns from high to low is policy pattern, observer pattern, chain of responsibility pattern, interpreter pattern, template method pattern, iterator pattern, mediator pattern, command pattern, visitor pattern, memo pattern, and state pattern. Among them, observer mode, interpreter mode, iterator mode, mediator mode, command mode, visitor mode, memo mode generally have a ready-made tool class, the self-made wheel is relatively rare.

The following according to my years of experience in the study of design mode, will be the first time the container dry goods released throughout the network. If this article is helpful to you must collect, also welcome to pay attention to and like; If you have any suggestions can also leave a comment or private letter, your support is my motivation to adhere to the creation. Pay attention to “Tom bomb architecture” for more technical dry goods!

2. Why must we learn design patterns

The title note
Tom Plays architecture: Why do you have to learn design patterns 2021/10/29 has been updated

Seven architecture Design Principles

The title note
Open Closed Principle (OCP) 2021/10/21 has been updated
Tom missile architecture: Dependence Inversion Principle (DIP) 2021/10/22 has been updated
Tom framework: Simple Responsibility Principle (SRP) 2021/10/23 has been updated
Tom Framework: Interface Segregation Principle (ISP) 2021/10/24 has been updated
Tom: Law of Demeter (LoD) 2021/10/25 has been updated
Tom shells: Liskov Substitution Principle (LSP) 2021/10/26 has been updated
Tom Elastic Architecture: Composite/Aggregate Reuse Principle, CARP 2021/10/27 has been updated

The catalog only represents the update plan, and may not be serialized in sequence due to the allocation of energy. The serialization will be completed within one month (i.e., before November 31, 2021). Please pay attention to the update of this article.

4. Creative design patterns

The title note
Tom plays architecture: Simple Factory Pattern (Simple Factory Pattern) >> Thoroughly explain Simple Factory details that you haven’t paid attention to 2021/11/10 has been updated
Tom play architecture: Factory Method Pattern >> fully transparent in-depth analysis of Factory Method Pattern 2011/11/11 has been updated
Abstract Factory Pattern >> Handwritten database connection pool, so that the Abstract Factory is no longer Abstract 2021/11/12 has been updated
How many of the 9 destroyed Singleton patterns have you encountered? 2021/10/26 has been updated
Tom play architecture: Prototype Pattern >> article to understand the relationship between deep cloning and shallow cloning 2021/11/01 has been updated
Using the Builder Pattern to implement an ORM framework against SQL injection 2021/10/26 has been updated

5. Structural design mode

The title note
No one has ever analyzed Proxy Pattern so thoroughly 2021/10/27 has been updated
Tom plays architecture: Facade Pattern >> You don’t know you use Facade Pattern every day 2021/11/13 has been updated
Interesting talk about Decorator patterns that you will never forget 2021/11/01 has been updated
Flyweight Pattern = Flyweight Pattern = Flyweight Pattern = Flyweight Pattern = Flyweight Pattern = Flyweight Pattern 2021/11/01 has been updated
# Infinite level menu trees with no performance bottlenecks should be designed like this 2021/11/04 has been updated
>> # How to quickly handle third-party login and easy to expand? 2021/11/03 has been updated
Use Bridge patterns to design complex messaging systems 2021/11/08 has been updated

Behavioral design patterns

The title note
You think the Delegate Pattern is mysterious, but you use it every day 2021/11/09 has been updated
Tom plays the framework: Template Method Pattern (Template Method Pattern) >> understand the hook Method and Template Method, read this article is enough 2021/11/07 has been updated
Tom shell architecture: Strategy Pattern >> Using Strategy Pattern to reconstruct e-commerce discount and payment scenarios 2021/11/05 has been updated
This is an elegant use of the Chain of Responsibility Pattern 2021/10/27 has been updated
Iterator Pattern: Iterator Pattern To be updated…
Tom shell architecture: Command Pattern To be updated…
Tom shell architecture: State Pattern To be updated…
Tom: Memento Pattern To be updated…
Mediator Pattern: Mediator Pattern To be updated…
Tom shells: Interpreter Pattern To be updated…
Tom shell architecture: Observer Pattern To be updated…
Tom structure: Visitor Pattern To be updated…

New design patterns

The title note
Object Pool Pattern To be updated…
Specification Pattern of Tom shells To be updated…
Tom shell architecture: Null Object Pattern To be updated…
Tom: Employee Pattern To be updated…

8. Summarize design patterns in one sentence

The comparison of various design modes and the summary of programming ideas are shown in the following table.

Design patterns Sum up in one sentence The eye of Life case Framework source code examples
Factory Pattern Product standardization,

More efficient production
Package creation details Physical plant LoggerFactory, Calender
Singleton Pattern There is only one me Guarantee uniqueness CEO The BeanFactory, Runtime
Prototype Pattern Pull out a monkey’s hair,

Blow out thousands
Creating objects efficiently cloning ArrayList, PrototypeBean
Builder Pattern High matching medium matching and low matching,

You can go with whatever you want
Open the personality configuration step optional The StringBuilder,

BeanDefinitionBuilder
Proxy Pattern No resources, no time,

We need a matchmaker to help us
Strengthen the responsibility matchmaker ProxyFactoryBean,

JdkDynamicAopProxy, CglibAopProxy
Facade Pattern Open a door,

To the world
Unified access portal The front desk JdbcUtils, RequestFacade
Decorator Pattern His great uncle, his second uncle,

Is his uncle
Flexible expansion,

Of the same
pancakes BufferedReader, InputStream
Flyweight Pattern Optimize resource allocation,

Reduce duplication and waste
Shared Resource Pool National Social Security Network String, Integer, ObjectPool
Composite Pattern People in groups are called gangs,

Hearts together make a team
Unify the whole and the individual Organization structure tree A HashMap, SqlNode
Adapter Pattern Universal charger Compatible with conversion power adaption AdvisorAdapter, HandlerAdapter
Bridge Pattern Convention over Configuration Inheritance is not allowed The bridge DriverManager
Delegate Pattern The requirement is simple,

I don’t care how
Only responsible for results Power of attorney This,

BeanDefinitionParserDelegate
Template Pattern All processes are standardized,

Need fine tuning please overwrite
Logic reuse Put the elephant in the fridge JdbcTemplate, HttpServlet
Strategy Pattern All roads lead to Beijing.

Which one is up to you
Give the choice to the user Choose payment method The Comparator,

InstantiationStrategy
Chain of Responsibility Pattern Each man sweeps the snow from his own door,

Never mind the frost on another’s tile
Decouple processing logic The buck FilterChain, Pipeline
Iterator Pattern Sitting on an assembly line all day,

Scan each package once
Unify access to collections Check in one by one Iterator
Command Pattern Strategizing,

A victory thousands of miles away
Decouple requests and processing The remote control A Runnable, TestCase
State Pattern State-driven behavior,

Behavior-determined state
Bind state and behavior Order status tracking Lifecycle
Memento Pattern If I stumble, I’ll be damned.

Start over if you want to
Backup, regret mechanism Draft box StateManageableMessageContext
Mediator Pattern I’ll give you the contact information,

How do I care
Manage network resources in a unified manner Circle of friends Timer
The Interpreter Pattern I want to speak dialect,

All interpretation is my prerogative
Implement specific syntax parsing Morse code The Pattern, ExpressionParser
Observer Pattern Let me know when it arrives Decouple the observer from the observed The alarm clock ContextLoaderListener
Visitor Pattern Viewed horizontally as a ridge forming a peak on its side,

Near, far, high and low
Decouple data structures and data operations The KPI assessment FileVisitor, BeanDefinitionVisitor

There are also numerous UML diagrams and confusing design pattern comparison case studies in This is How Design Patterns should Be Learned.

In everyday use, design patterns are never used independently of one another. In practice, multiple design patterns are often used in a mix, where you are part of me and I am part of you. The following diagram completely describes the mixed relationships between design patterns, which I hope will be helpful.

This article is scheduled to be serialized within one month (i.e., before November 31, 2021). Please continue to pay attention to the update of this article. You can follow and bookmark this article first. Pay attention to “Tom play architecture” reply to “design pattern” can obtain the complete source code. The next serialized feature is “Spring Core Principles and source code Analysis”.

This article is “Tom play structure” original, reproduced please indicate the source. Technology is to share, I share my happiness! If this article is helpful to you, welcome to follow and like; If you have any suggestions can also leave a comment or private letter, your support is my motivation to adhere to the creation. Pay attention to “Tom bomb architecture” for more technical dry goods!