Head First设计模式:设计原则


目录
  • Head First Design Patterns
    • 1 设计原则(Design Principles)
      • 1.1 Identify the aspects of your application that vary and separate them from what stays the same!
      • 1.2 Program to an interface, not an implementation!
      • 1.3 Favor composition over inheritance.
      • 1.4 Loose Coupling
      • 1.5 Open-Closed
      • 1.6 Dependency Inversion
    • 2 设计模式(Design Patterns)
      • 2.1 Strategy
      • 2.2 Observer
      • 2.3 Decorator
      • 2.4 Factory Method
      • 2.5 Abstract Factory
    • 3 注意事项
      • 3.1 implement an interface
      • 3.2

Head First Design Patterns

最近看了 Head First Design Patterns,做了一些笔记。持续更新中。。。

1 设计原则(Design Principles)

1.1 Identify the aspects of your application that vary and separate them from what stays the same!

找出程序中的变化部分,然后将他们和不变的部分分开。
Encapulate what varis.

1.2 Program to an interface, not an implementation!

面向接口编程,而非实现。
注:此处的接口,并不仅仅指java中的interface,而是广义上的接口,可以理解为超类型;父型(supertype)。

1.3 Favor composition over inheritance.

多用组合,少用继承。

1.4 Loose Coupling

松耦合原则:
Strive for loosely coupled designs between objects that interact.
为了交互对象间松耦合的设计而努力。

1.5 Open-Closed

开闭原则:
Classes should be open for extension, but closed for modification.

1.6 Dependency Inversion

依赖倒置原则:
Depend upon abstractions. Do not depend upon concrete classes.
依赖抽象,不要依赖具体类。

The following guidelines can help you avoid OO designs that violate the Dependency Inversion Principle:

  1. No variable should hold a reference to a concrete class.
  2. No class should derive from a concrete class.
  3. No method should override an implemented method of any of its base classes.
    Note: Like many of our principles, this is a guideline you should strive for, rather than a rule you should follow all the time. Clearly, every single Java program ever written violates these guidelines!

2 设计模式(Design Patterns)

2.1 Strategy

策略模式:
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.
策略模式定义了算法族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化独立于使用算法的客户。

建议:

  1. build a community of pattern users. 建立一个模式用户社区(可以在自己组织内部,或者互联网上),大家相互交流学习。

2.2 Observer

观察者模式:
defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.
定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新。

2.3 Decorator

装饰者模式:
attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
动态地将责任附加到对象上。对于扩展功能,装饰者提供了比子类化更灵活的替代方案。

小结:

  1. 装饰者模式是通过组合而非继承获取到被装饰者的行为的,并且,此处的 组合,更具体、更准确的描述,是 包装(wrap)。也就是说, 包装组合 的其中一种方法。

2.4 Factory Method

工厂方法模式:
The Factory Method Pattern defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
工厂方法模式定义了一个创建对象的接口,但是让子类决定实例化哪个类。工厂方法推迟了一个类的实例化动作到子类。

2.5 Abstract Factory

抽象工厂模式:
provides an interface for creating families of related or dependent objects without specifying their concrete classes.
提供了一个接口,用于创建相关或依赖对象的家族,而不需要明确指定具体类。

3 注意事项

3.1 implement an interface

in design patterns, the phrase “implement an interface” does NOT always mean “write a class that implements a Java interface, by using the ‘implements’ keyword in the class declaration.” In the general use of the phrase, a concrete class implementing a method from a supertype (which could be a abstract class OR interface) is still considered to be “implementing the interface” of that supertype.
设计模式中所谓的“实现一个接口”并不一定表示“写一个类,并在类声明中使用 implements 关键字来实现某个Java接口”。该短语的一般用法是,一个具体类实现某个超类型(可以是类或接口)的某个方法,则被认为是“实现该超类型的接口”。

3.2