设计模式本科课程笔记
引言
Definition
A software design pattern is a solution to a commonly occuring problem in a context.
Definetion Software Architecture
Software architecture encompasses the set of
significant decisions about the organization of a software system:
- Selection of the structural elements and their
interfaces by which a system is composed - Behavior as specified in collaborations among those
elements - Composition of these structural and behavioral
elements into larger subsystems - Architectural style that guides this organization
Design Objectives and Principles
Design Objectives
General properties of good designs that can guide you as you create your own designs.
Design principles
Guidelines for decomposing a system's required functionality and behavior into modules.
Design objectives
- Low Coupling, High Cohesion
- Information Hiding & Encapsulation
- Abstraction
- Low Redundancy
- Modularity
- Simplicity
Design Principle
- Encapsulate what varies
- Program to interface, not implementations
- Favor composition over inheritance
Strategy pattern
Problem
- Define a family of algorithms, encapsulate each one, and make them interchangeable.
- Strategy lets the algorihtm vary independently from clients that use it.
Consequences
- Families of related algorithms. Hierarchies of Strategy classes define a family of algorithms or behaviors for contexts to reuse. Inheritance can help factor out common functionality of algorithms.
- An alternative to subclasseing. Inheritance offers another way to support a variety of algorithms or behaviors. You can subclass a Context class directly to give it different behaviors. You can subclass a Context class directly to give it different behaviors.But this hard‐wires the behavior into Context. It mixes the algorithm implementation with Context's, making Context harder to understand, maintain, and extend. And you can't vary the algorithm dynamically. You wind up with many related classes whose only difference is the algorithm or behavior they employ. Encapsulating the algorithm in separate Strategy classes lets you vary the algorithm independently of its context, making it easier to switch, understand, and extend.
- Strategies eliminate conditional statements. The Strategy pattern offers an alternative to conditional statements for selecting desired behavior. When different behaviors are lumped into one class, it's hard to avoid using conditional statements to select the right behavior. Encapsulating the behavior in separate Strategy classeseliminates these conditional statements.
## Observer
### Problem - Intent: Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified.
- Motivation: When objects exchange data to maintain consistency, the high cohesion/low coupling principle encourages these objects to cooprate in a loosely coupled way. An example of one way to achieve this is shown on the next slide.
Consequences
- Abstract coupling between Subject and Observer.
- Support for broadcast communication
- Unexpected updates.