Chapter 1. Introduction to Design Pattern
Design Principle
- Identify the aspects of your application that vary and separate them from what stays the same.
- Program to an interface (java), not an implementation. (polymorphism)
- Favour composition over inheritance. Composition allows to change behaviour at runtime.
Strategy Pattern
The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
Advantages
- Encapsulate what varies. Make the program extensible and maintainable.
- Define a family of algorithms and allow runtime modification.
Example: SimUDuck app


本文介绍设计模式中的策略模式,强调将变化的部分与不变的部分分离。通过编程接口而非具体实现进行编程,利用组合而非继承来实现行为的动态改变。文章通过一个鸭子模拟应用的例子详细展示了策略模式在C++中的实现。
710

被折叠的 条评论
为什么被折叠?



