Skip to content

lanzhiwang/python-patterns

Repository files navigation

python-patterns

A collection of design patterns and idioms惯用语法 in Python.

When an implementation is added or modified, be sure to update this file and rerun重新运行 append_output.sh (eg. ./append_output.sh borg.py) to keep the output comments at the bottom up to date.

Current Patterns:

Creational Patterns: 创作模式

Pattern Description
abstract_factory 抽象工厂模式 use a generic function with specific factories
borg or Monostate 伯格模式 or 单态模式 a singleton with shared-state among instances 实例之间具有共享状态的单例
builder 生成器模式 instead of using multiple constructors, builder object receives parameters and returns constructed objects 生成器对象不使用多个构造函数,而是接收参数并返回构造的对象
factory_method 工厂方法模式 delegate a specialized function/method to create instances 委托专门的功能/方法来创建实例
lazy_evaluation 延迟计算 lazily-evaluated property pattern in Python
pool 对象池模式 preinstantiate and maintain a group of instances of the same type 预先实例化并维护一组相同类型的实例
prototype 原型模式 use a factory and clones of a prototype for new instances (if instantiation is expensive) 使用工厂和原型的克隆用于新实例(如果实例化非常昂贵)

Structural Patterns: 结构模式

Pattern Description
3-tier 三层架构模式 data<->business logic<->presentation separation (strict relationships)
adapter 适配器模式 adapt one interface to another using a white-list
bridge 桥接模式 a client-provider middleman to soften interface changes
composite 组合模式 lets clients treat individual objects and compositions uniformly
decorator 装饰器模式 wrap functionality with other functionality in order to affect outputs
facade 外观模式 use one class as an API to a number of others
flyweight 享元模式 transparently reuse existing instances of objects with similar/identical state
front_controller 前端控制器模式 single handler requests coming to the application
mvc model<->view<->controller (non-strict relationships)
proxy 代理模式 an object funnels operations to something else

Behavioral Patterns: 行为模式

Pattern Description
chain_of_responsibility 责任链模式 apply a chain of successive handlers to try and process the data
catalog 设计目录模式 general methods will call different specialized methods based on construction parameter
chaining_method continue callback next object method
command 命令模式 bundle a command and arguments to call later
iterator 迭代器模式 traverse a container and access the container's elements
mediator 中介者模式 an object that knows how to connect other objects and act as a proxy
memento 备忘录模式 generate an opaque token that can be used to go back to a previous state
observer 观察者模式 provide a callback for notification of events/changes to data
publish_subscribe 发布订阅模式 a source syndicates events/data to 0+ registered listeners
registry 注册模式 keep track of all subclasses of a given class
specification 规格模式 business rules can be recombined by chaining the business rules together using boolean logic
state 状态模式 logic is organized into a discrete number of potential states and the next state that can be transitioned to
strategy 策略模式 selectable operations over the same data
template 模板模式 an object imposes a structure but takes pluggable components
visitor 访问者模式 invoke a callback for all items of a collection

Design for Testability Patterns:

Pattern Description
setter_injection 依赖注入模式 the client provides the depended-on object to the SUT via the setter injection (implementation variant of dependency injection)

Fundamental Patterns:

Pattern Description
delegation_pattern 委派模式 an object handles a request by delegating to a second object (the delegate)

Others:

Pattern Description
blackboard 黑板模式 architectural model, assemble different sub-system knowledge to build a solution, AI approach - non gang of four pattern
graph_search 图搜索模式 graphing algorithms - non gang of four pattern
hsm hierarchical state machine 层次状态机 hierarchical state machine - non gang of four pattern

web 框架中间件实现方法:

Middleware

ORM 框架实现:

orm

元类编程示例:

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages