Python Design Patterns 简明教程

Python Design Patterns - Introduction

设计模式用于表示开发人员创建软件或 Web 应用程序所用的模式。这些模式基于需求分析进行选择。模式描述了问题的解决方案、何时何地应用解决方案以及实现的后果。

Design patterns are used to represent the pattern used by developers to create software or web application. These patterns are selected based on the requirement analysis. The patterns describe the solution to the problem, when and where to apply the solution and the consequences of the implementation.

Structure of a design pattern

设计模式的文档以一种更侧重于所用技术及其方式的方式来维护。下图解释了设计模式文档的基本结构。

The documentation of design pattern is maintained in a way that focuses more on the technology that is used and in what ways. The following diagram explains the basic structure of design pattern documentation.

design pattern

Pattern Name

简短有效地描述了该模式。

It describes the pattern in short and effective manner.

Intent/Motive

描述了该模式的作用。

It describes what the pattern does.

Applicability

描述了该模式适用的情况列表。

It describes the list of situations where pattern is applicable.

Participants and consequences

参与者包括参与设计模式的类和对象以及与该模式相关的后果列表。

Participants include classes and objects that participate in the design pattern with a list of consequences that exist with the pattern.

Why Python?

Python 是一种开源脚本语言。它拥有支持各种设计模式的库。Python 的语法易于理解并使用英语关键字。

Python is an open source scripting language. It has libraries that support a variety of design patterns. The syntax of python is easy to understand and uses English keywords.

Python 提供对下面列出的设计模式的支持。这些设计模式将在本教程中用到 -

Python provides support for the list of design patterns that are mentioned below. These design patterns will be used throughout this tutorial −

  1. Model View Controller Pattern

  2. Singleton pattern

  3. Factory pattern

  4. Builder Pattern

  5. Prototype Pattern

  6. Facade Pattern

  7. Command Pattern

  8. Adapter Pattern

  9. Prototype Pattern

  10. Decorator Pattern

  11. Proxy Pattern

  12. Chain of Responsibility Pattern

  13. Observer Pattern

  14. State Pattern

  15. Strategy Pattern

  16. Template Pattern

  17. Flyweight Pattern

  18. Abstract Factory Pattern

  19. Object Oriented Pattern

Benefits of using design pattern

以下是设计模式的不同优点 -

Following are the different benefits of design pattern −

  1. Patterns provide developer a selection of tried and tested solutions for the specified problems.

  2. All design patterns are language neutral.

  3. Patterns help to achieve communication and maintain well documentation.

  4. It includes a record of accomplishment to reduce any technical risk to the project.

  5. Design patterns are highly flexible to use and easy to understand.