设计模式本科课程笔记

引言

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:

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

Design Principle

Strategy pattern

Problem

Consequences

  1. 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.

  2. 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.

  3. 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

Consequences