Today, let's start to learn the common design patterns of JavaScript. In fact, design patterns, like algorithms, are universal and empirical ideas that deviate from the language. Front-end programmers are also software engineers, who need a solid professional foundation, and design pattern is also one of the important professional foundations of computer.
what is a design pattern
Design patterns are concise and elegant solutions to specific problems in the process of software design
when developing business code, you are faced with a variety of different needs, and different situations will sum up different solutions. We can sum up these experiences , and make rational use of to different problems, so that we can use common solutions to solve problems . To keep it simple, design patterns can be understood as tricks and tricks when writing code.
Design patterns have SOLID
five design principles
- single function principle (
S
ingle Responsibility Principle): a program only does one thing well - Open and closed principle (
O
pened Closed Principle): open to expansion, closed to modification - Internal substitution principle (
L
iskov Substitution Principle): subclasses can override the parent class and appear where the parent class appears - Interface isolation principle (
I
nterface Segregation Principle): keep the interface single and independent - dependency inversion principle (
D
ependency Inversion Principle): use methods to focus only on interfaces and not on the implementation of specific classes
Front-end development uses JavaScript. We should focus on the first two principles, namely, single function principle and Open and closed principle
Why do you need a design pattern
Why do you need design patterns, that is, what are the benefits of learning and using design patterns.
it is precisely because the business is getting bigger and bigger, and it needs to be iterated and updated constantly, so we should consider the readability, scalability, reusability and reliability of the code when writing the code.
- readability : it can improve the readability of the code and improve the efficiency of subsequent development
- Extensibility : decoupling the code can enhance the ease of modification and extensibility of the code
- reuse Nature: existing solutions can be reused without having to repeat the same work
- Reliability : it can increase the robustness of the system and make code writing truly engineering
Classification of design patterns
in terms of intention, design patterns can be divided into creative patterns, structural patterns and behavioral patterns
.
- the purpose of creating a schema is to encapsulate changes to the created object
- structural schema encapsulates the composite relationship between objects
- Behavioral patterns encapsulate changes in the behavior of objects.
JavaScript common design patterns
We often hear 23 design patterns. Today, lecturers who follow the byte beat will first learn the ten common design patterns of JavaScript:
- Singleton mode
- Policy mode
- Agent mode
- publish / subscribe mode
- Command mode
- combination mode
- decorator mode
- Adapter pattern
Let's learn about these design patterns one by one in the following blog posts.
Comments