Skip to content

Software design pattern

“software 的sing pattern”即软件设计模式,是前人总结的解决特定问题的最佳实践(best practice)。我们平时提及design pattern的时候,往往第一反应是Object-oriented design pattern,其实design pattern不仅限于Object-oriented programming,在各个领域中,都能够总结出domain-specific patterns

维基百科Software design pattern

In software engineering, a software design pattern is a general, reusable solution to a commonly occurring problem within a given context in software design. It is not a finished design that can be transformed directly into source or machine code. It is a description or template for how to solve a problem that can be used in many different situations. Design patterns are formalized best practices that the programmer can use to solve common problems when designing an application or system.

NOTE: design pattern是formalized best practices

Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. Patterns that imply mutable(可变的) state may be unsuited for functional programming languages, some patterns can be rendered unnecessary in languages that have built-in support for solving the problem they are trying to solve, and object-oriented patterns are not necessarily suitable for non-object-oriented languages.

NOTE: 关于functional programming,参见Theory\Programming-paradigm\Functional-programming章节。

Practice

Design patterns can speed up the development process by providing tested, proven development paradigms. Effective software design requires considering issues that may not become visible until later in the implementation. Freshly written code can often have hidden subtle issues that take time to be detected, issues that sometimes can cause major problems down the road. Reusing design patterns helps to prevent such subtle issues , and it also improves code readability for coders and architects who are familiar with the patterns.

NOTE: 使用design pattern的优势。

software design和城市规划有点类似,都需要以发展的眼光来进行规划。

In order to achieve flexibility, design patterns usually introduce additional levels of indirection, which in some cases may complicate the resulting designs and hurt application performance.

NOTE: 参见文章分层、文章Create-larger-concept.md

By definition, a pattern must be programmed anew(重新,再次) into each application that uses it. Since some authors see this as a step backward from software reuse as provided by components, researchers have worked to turn patterns into components. Meyer and Arnout were able to provide full or partial componentization of two-thirds of the patterns they attempted.

Software design techniques are difficult to apply to a broader range of problems. Design patterns provide general solutions, documented in a format that does not require specifics tied to a particular problem.

Classification

design pattern如此之多,如何对它们进行分类至关重要,这是本节需要讨论的问题,本节的内容参考自:

refactoring Classification of patterns

Design patterns differ by their complexity, level of detail and scale of applicability to the entire system being designed. I like the analogy to road construction: you can make an intersection(十字路口) safer by either installing some traffic lights or building an entire multi-level interchange with underground passages for pedestrians(行人).

The most basic and low-level patterns are often called idioms. They usually apply only to a single programming language.

The most universal and high-level patterns are architectural patterns. Developers can implement these patterns in virtually any language. Unlike other patterns, they can be used to design the architecture of an entire application.

In addition, all patterns can be categorized by their intent, or purpose.

这段总结是非常好的,在c++中,我们总结了More C++ Idioms,显然idiom是language-specific,它是某种具体的programming的pattern。我们平时最最常听说的design pattern,往往指的是的OOP的pattern,显然只要某种programming language支持OOP,那么我们就可以使用OOP design pattern,显然它是programming-paradigm-specific的。最最universal、hight-level的是 architectural patterns,显然它不是面向某种具体的programming language、某种具体的programming paradigm的,它是面向application的,比如web application。下面以表格的形式对这段内容进行整理:

说明 example
Language-specific pattern 某种具体的programming的pattern More C++ Idioms
Programming-paradigm-specific pattern 某种具体的programming paradigm的pattern OOP design pattern
Application-specific pattern 面向某种application的pattern Architectural design pattern

维基百科Software design pattern:

Design patterns were originally grouped into the categories: creational patterns, structural patterns, and behavioral patterns, and described using the concepts of delegation, aggregation, and consultation. For further background on object-oriented design, see coupling and cohesion, inheritance, interface, and polymorphism. Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model–View–Controller pattern.

OOP design pattern

在工程programming-language的Theory\Design-pattern\OOP-design-pattern章节进行详细描述。

Concurrency patterns

参见工程Parallel-computing的Concurrent-computing\Design-pattern章节。

Architectural design pattern

参见Software-design\Design-pattern\Architecture-pattern章节。