Python design patterns

Creation mode#

Factory method##

Instance -> Class -> Class Factory

Abstract Factory##

Simply put, it is to encapsulate some classes with the same method and abstract the common method for invocation. It is an advanced version of the factory method.

Instance -> Class -> Class Factory -> Abstract Factory

Lazy evaluation

This Python can be implemented using @property, which is generated when it is called.

Builder

The Builder mode is mainly used to construct a complex object, but the algorithm for constructing this object is stable, and various parts of the object often change. The Builder mode is mainly to deal with the frequent demand changes of various parts of complex objects. However, it is difficult to cope with changes in the demand for algorithms. You must pay attention to this point, if you use it wrong, it will bring a lot of unnecessary trouble.

The point is to abstract the construction process of complex objects (abstract category), so that different implementation methods of this abstract process can construct objects with different manifestations (attributes).

Simply put: the sub-object changes more frequently, and the algorithm is relatively stable.

Singleton

There is only one instance of a class

Prototype Mode##

The characteristic is to return a new instance by copying an existing instance instead of creating a new instance.

It is mostly used to create complex or time-consuming instances, because in this case, copying an existing instance makes the program run more efficiently; or creating similar data with equal values but different names.

Object pool

An object pool is a set of objects that have been initialized and can be used, and objects can be created and destroyed when required. The user of the pool can obtain an object from the pool, manipulate it, and return it to the pool when it is not needed instead of destroying it instead of destroying it.

Object pool technology is implemented inside Python. For example, there are many data references like small integers, and creation and destruction will consume time, so it is stored in the object pool to reduce overhead.

Structural Mode#

Decorator Model

Decorators are in Python.

Proxy mode Proxy

For example, reference counting in Python.

Behavioral Mode#

Iterator##

Iterate over all the elements in the container.

Recommended Posts

Python design patterns
Python multithreading
Python CookBook
Python FAQ
Python3 dictionary
Python3 module
python (you-get)
Python string
Python descriptor
Python basics 2
Python exec
Python notes
Python3 tuple
CentOS + Python3.6+
Python advanced (1)
Python decorator
Python IO
Python multithreading
Python toolchain
Python3 list
Python multitasking-coroutine
Python overview
python introduction
Python analytic
Python basics
07. Python3 functions
Python basics 3
Python multitasking-threads
Python functions
python sys.stdout
python operator
Python entry-3
Centos 7.5 python3.6
Python string
python queue Queue
Python basics 4
Python basics 5
python3 simply implements the combined design pattern