2007/11/01

《Head First Design Patterns》简短读书笔记

《Head First Design Patterns》是O'Reilly公司Head First系列图书中的一本,定价44.95美刀(Amazon上打折卖29.67美刀),东南大学的影印版开价98人民币(China-Pub上卖73.50)。虽然相信O'Reilly的品质会物有所值,但是中关村图书大厦里居然把Head First系列全都用塑料薄膜裹起来卖,这也太小气了吧。不管怎样,这本书绝不只是有一个香艳的封面。

全书共14章,在前11章中重点介绍了14种设计模式,第12章以MVC为例讲了设计模式如何组合在一起工作(模式的模式),第13章讲了模式的一般概念,模式的分类,以及反模式,强调设计原则比设计模式更重要,避免设计模式的滥用,第14章也就是附录,对四人帮23种设计模式中还未提及的9种模式一笔带过。

Tools for your Design Toolbox
OO Basics(Knowing concepts like abstraction, inheritance, and polymorphism do not make you a good OO designer.)

  • Abstraction
  • Encapsulation
  • Polymorphism
  • Inheritance
OO Principles
  • Encapsulate what varies.
  • Favor composition over inheritance.
  • Program to interfaces, not implementations.
  • Strive for loosely coupled designs between objects that interact.
  • Classes should be open for extension but closed for modification.
  • Depend on abstractions. Do not depend on concrete classes.(The Dependency Inversion Principle
  • Only talk to your friends.(Principle of Least Knowledge
  • Don't call us, we'll call you.(The Hollywood Principle,High-level components control when and how low-level components can participate in the computation. But never call high-level components directly.)
  • A class should have only one reason to change.(Single Responsibility Principle
OO Patterns
  • The Strategy Pattern defines a family of algorithms, encapsulates each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.(通过object composition实现。)
  • The Observer Pattern(Subject + Observers,在JDK中用得最多的模式) defines a one-to-many dependency between objects so that one object changes state, all of its dependents are notified and updates automatically.(Subject关于Observer知道的唯一一点是Observer实现了Observer接口。Java内置了Observable类。)
  • The Decorator Pattern attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.(通过object composition获得新的行为。)
  • 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.(They say DECIDE not because the pattern allows subclasses themselves to decide at runtime, but because the creator class is written without knowledge of the actual products that will be created, which is decided purely by the choice of the subclass that is used. 通过inheritance实现。)
  • The Abstract Factory Pattern provides an interface for creating familities of related or dependent objects without specifying their concrete classes.(通过object composition创建entire families of products。)
  • The Singleton Pattern ensures a class has only one instance, and provides a global point of access to it.
  • The Command Pattern encapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue or log requests, and support undoable operations.
  • The Adapter Pattern converts the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
  • The Facade Pattern provides a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
  • The Template Method Pattern defines the skeleton of an algorithm in a method, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.(通过inheritance实现。)
  • The Iterator Pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
  • The Composite Pattern allows you to compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.
  • The State Pattern allows an object to alter its behavior when its internal state changes. The object will appear to change its class.
  • The Proxy Pattern provides a surrogate or placeholder for another object to control accsss to it.(可应用为Remote Proxy、Virtual Proxy、Firewall Proxy、Smart Reference Proxy、Caching Proxy、Synchoronization Proxy、Complexity Hiding Proxy(有时叫Facade Proxy)、Copy-On-Write Proxy。In Java 5, RMI and Dynamic Proxy got together and now stubs are generated dynamically using Dynamic Proxy. The stub is a java.lang.reflect.Proxy instance(with an invocation handler) that is automatically generated.)

0 块板砖 :