程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python] Python language learning: design mode, singleton mode

編輯:Python

01

Design patterns

1.1 Introduction to design mode

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 .

1.2 Design pattern classification

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 .

1.3 Object oriented design principles

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 .

1 to open up / Closed principle

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 .

2 Control reversal principle

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 .

3 Interface isolation principle

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 .

4 Principle of single responsibility

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 .

5 Substitution principle

Substitution principle , Derived classes must be able to completely replace base classes .

02

Singleton design pattern

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 :


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved