The Art of Design Patterns--Basic Concepts of Design Patterns

The Art of Design Patterns – Basic Concepts of Design Patterns

Disclaimer: This series is a summary of the content of Mr. Liu Wei's blog ( http://blog.csdn.net/lovelion ), there are complete blog posts about design patterns in the blog, as well as the author's published book recommendations

This series of content ideas analysis draws on the content of Mr. Liu Wei's blog post, and at the same time uses C# code for code demonstration and analysis (too much Java material, C# means silence).

The series is divided into 4 parts

Part 1 : Basic knowledge, mainly introduces the basic knowledge related to design patterns,
including an overview of design patterns, UML class diagrams, 7 principles of object-oriented design, etc.

Part 2 : The Art of Creation, introduces 6 commonly used creational design patterns, namely the singleton
pattern , the simple factory pattern, the factory method pattern, the abstract factory pattern, the prototype pattern and the builder pattern.

Part 3 : The Art of Composition, introduces seven commonly used structural design patterns, namely the adapter pattern, the bridge pattern, the composition pattern, the decoration pattern, the appearance pattern, the flyweight pattern and the proxy pattern.

Part 4 : The Art of Interaction, introduces 11 commonly used behavioral design patterns, namely the chain of responsibility pattern, the command pattern, the interpreter pattern, the iterator pattern, the mediator pattern, the memo pattern, the observer pattern, the state pattern, and the strategy pattern. , the template method pattern and the visitor pattern.


first part

Chapter 1 Basic Concepts of Design Patterns

1.1 Definition of Design Patterns

Design Pattern
- a set of repeated, most known, catalogued, summaries of code design experience
- is a method for documenting solutions to recurring design problems in software systems Technology
- is a technology to share the design experience of experts
- purpose: for reusable code, make code easier to understand by others, improve code reliability

1.2 Classification of Design Patterns

According to the purpose (what the pattern is used for), it can be divided into three categories: Creational, Structural and Behavioral:
- Creational mode is mainly used to create objects
- Structural mode is mainly used for Handling composition of classes or objects
- Behavioral patterns are mainly used to describe how classes or objects interact and how responsibilities are assigned

According to the scope, that is, whether the schema mainly deals with the relationship between classes or the relationship between objects, it can be divided into two types: class schema and object schema:
- The class schema deals with the relationship between classes and subclasses, and these relationships are established through inheritance , which is determined at compile time and is a static relationship
- the object schema deals with relationships between objects that change at runtime and are more dynamic
write picture description here

1.3 Advantages of Design Patterns

  • Incorporating the experience of many experts and making it available to a wide range of developers in a standard form
  • Provides a set of common design vocabulary and a common language to facilitate communication and communication between developers, making design solutions more accessible
  • Make it easier and easier to reuse successful designs and architectures
  • Make the design more flexible and easy to modify
  • It will improve the development efficiency and software quality of the software system, and save the design cost to a certain extent
  • It is helpful for beginners to have a deeper understanding of object-oriented ideas, facilitates reading and learning of existing class libraries and source codes in other systems, and can also improve the design level and code quality of software

1.4 Design Pattern Learning Author's Viewpoint

As a loyal fan and promoter of design patterns, before I formally learn design patterns, I will share with you a few personal views based on years of pattern application and education and training experience for reference:

  1. Mastering design patterns is not difficult. The key is to think more and practice more. You must have your own thinking and dissociation process. As long as you study hard, design patterns will be the same. Must have confidence.

  2. When learning each design pattern, you should at least master the following points: what is the purpose of this design pattern, what problem does it solve, and when can it be used; how does it solve it, master its structure diagram, and remember it The key code; can think of at least two examples of its application, one in life, one in software; what are the advantages and disadvantages of this mode, and what should be paid attention to when using it. When you can answer all the above questions, congratulations, you understand a design pattern, as for mastering it, then use it in development, and you will naturally master it if you use it more.

  3. The purpose of learning a design pattern is to apply it. If you don't know how to use a design pattern, but you just learned it, you can tell its purpose and draw its structure. At best, you can only say that you understand the pattern. Strictly speaking: no Using a pattern flexibly in development is basically the same as not learning it. So make sure to do it: talk less and do more.

  4. Don't abuse patterns, don't try to use them all in one system. Each pattern has its own applicable scenarios, can't you use patterns for the sake of using patterns? Abusing the mode is worse than not using the mode, because the result of abuse will not get the same software as "art", which is likely to be a pile of junk code.

  5. If the design pattern is compared to "36 strategies", then each pattern is a strategy, it was born to solve a certain type of problem, no matter how difficult the design pattern is, everyone should study hard and learn more A mode also means that you have an extra "count", and you may use it accidentally at any time.

  6. The "superior" realm of design patterns: "No patterns in the hand, but patterns in the heart". The highest level of pattern use is that you no longer know the definition and structure of a specific design pattern, but you will flexibly choose a design solution [actually a design pattern] to solve a problem, and design patterns have become Part of your development skills can be easily captured.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325565245&siteId=291194637