01
Design patterns To solve specific problems Solution .
design-mode advantage :
1 It can be reused in multiple projects .
2 The problem can be solved at the architecture level .
3 After time verification and good proof , yes Developers and architects Our valuable experience .
4 It has reliability and dependency .
In order to better understand Design patterns , Let's first understand these terms .
1 Code segment : A piece of code written in a language with a specific purpose . for example ,Python Code snippet of language linked database .
2 Design : Excellent solutions to specific problems .
3 standard : A way to solve a certain kind of problem , Very versatile , And apply to the current situation .
4 Pattern : A time tested 、 Efficient 、 Scalable solutions , Can solve a class of known problems .
GoF In their design pattern books 《Design Patterns: Elements of Reusable Object-Oriented Software》 Speak to the 23 Design patterns , Divided into three categories .
1 Create pattern
2 Structural mode
3 Behavioral patterns
The classification of patterns is mainly based on the creation of objects 、 How classes and objects are constructed in software applications , It also involves the interaction between objects .
Create pattern The nature of .
1 Their operating mechanism is based on the way objects are created .
2 They isolate the details of object creation .
3 The code is independent of the type of object created .
The singleton pattern This is an example of a creative pattern .
Structural mode The nature of .
1 We are committed to designing the structure of objects and classes that get more powerful functions through composition .
2 The focus is on simplifying the structure and identifying the relationship between classes and objects .
3 They mainly focus on class inheritance and composition .
Adapter pattern Is an example of a structural pattern .
Behavioral patterns The nature of .
1 Pay attention to the interaction between objects and the responsiveness of objects .
2 Objects should be able to interact , While still maintaining loose coupling .
Learning and understanding of design patterns , We need to program object-oriented OOP Have a certain understanding .
Python Language learning : object-oriented programming , This article introduces OOP Basic knowledge of .
Object oriented design principles , It is conducive to our in-depth study and understanding of design patterns . Follow and apply these principles , Can let us software architecture 、 Design and implementation are more robust 、 Stable 、 flexible 、 Powerful .
to open up / Closed principle , Classes and objects and their methods for extensions , Is open ; For modification , It should be closed .
The advantages of this principle .
1 Existing classes will not be modified , Therefore, the possibility of degradation is small .
2 Help maintain backward compatibility of previous code .
Control reversal principle , High level modules should not rely on low-level modules , They should rely on abstraction . No two modules should be interdependent in a tight way .
The advantages of this principle .
1 Weaken the tight coupling between modules , Therefore, the complexity in the system is eliminated .
2 Because there is a clear abstraction layer between dependent modules ( Provided by hook or parameter ), Therefore, it is convenient to deal with the dependencies between modules in a better way .
Interface isolation principle , Clients should not rely on interfaces that they do not need to use .
The advantages of this principle .
1 It forces developers to write slimming interfaces , And make the method closely independent of the interface .
2 Prevent random filling of methods into the interface .
Principle of single responsibility , Class has a single responsibility , The cause of class change is single .
The advantages of this principle .
1 Whenever a function changes , Except that specific classes need to be changed , There is no need to change other categories .
2 If a class has multiple functions , Then the class that depends on it must undergo many modifications for many reasons , This should be avoided .
Substitution principle , Derived classes must be able to completely replace base classes .
The singleton design pattern is the simplest and most famous Create design patterns .
Singleton design pattern Intention :
1 Make sure that the class has and only one object is created .
2 Provide an access point for the object , So that the program can access the object globally .
3 Control parallel access to shared resources .
Singleton design pattern Application scenarios :
1 Printer spooler
2 Database operating procedures
wait .
Singleton mode reference code .
class Singleton(object):
def __new__(cls, *args, **kwargs):
if not hasattr(cls, "instance"):
cls.instance = super().__new__(cls)
return cls.instance
s = Singleton()
print("Object created", s)
s1 = Singleton()
print("Object created", s1)
Running results
Past highlights
It is suitable for beginners to download the route and materials of artificial intelligence ( Image & Text + video ) Introduction to machine learning series download Chinese University Courses 《 machine learning 》( Huang haiguang keynote speaker ) Print materials such as machine learning and in-depth learning notes 《 Statistical learning method 》 Code reproduction album
AI Basic download machine learning communication qq Group 955171419, Please scan the code to join wechat group :