Head First Design Patterns Notes

来源:互联网 发布:mysql 启动失败 日志 编辑:程序博客网 时间:2024/06/11 01:26

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

Design Principle: 

Encapsulate what varies.

Favor composition over inheritance.

Program to an interface, not an implementation.

2.Observer

The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.

Design Principle:

Strive for loosely coupled designs between objects that interact.

Example: JDK Observable and Observer interface


3. Decorator

The Decorator Pattern attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.  

Design Principle: (Open-Close Principle)

Classes should be open for extension but closed for modification.

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.  

Design Principle (Dependency Inversion Principle):

Depend upon abstractions. Do not depend upon concrete classes.

5.  Abstract Factory

The Abstract Factory Pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes.

6.  Singleton

The Singleton Pattern ensures a class has only one instance, and provides a global point of access to it.

Dealing with multithreading

1) Do nothing if the performance of getInstance() isn’t critical to your application, just add synchronized for the getInstance() method

2) Move to an eagerly created instance rather than a lazily created one

public class Singleton {private static Singleton uniqueInstance = new Singleton();private Singleton() {}public static Singleton getInstance() {return uniqueInstance;}}

3) Use 'double-checked locking' to reduce the use of synchronization in getInstance()  NOTE: this doesn't work for JDK 1.4 
public class Singleton {private volatile static Singleton uniqueInstance;private Singleton() {}public static Singleton getInstance() {if (uniqueInstance == null) {synchronized (Singleton.class) {if (uniqueInstance == null) {uniqueInstance = new Singleton();}}}return uniqueInstance;}}


7. Command

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.

8. Adapter

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.

9. Facade

The Facade Pattern provides a unified interface to a set of interfaces in a subsytem. Facade defines a higherlevel interface that makes the subsystem easier to use.  
Design Principle (Least Knowledge):

Principle of Least Knowledge - talk only to your immediate friends.


10.  template method

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.  

Design Principle:

The Hollywood Principle - Don't call us, we'll call you.

example: Java Swing with Frame, Applet, and other frameworks.


11. Iterator

The Iterator Pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

Design Principle: (Single Responsibility)

A class should have only one reason to change.

12. Composite

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.

13. State

The State Pattern allows an object to alter its behavior when its internal state changes. The object will appear to change its class.

example: 状态机

14. Proxy

The Proxy Pattern provides a surrogate or placeholder for another object to control access to it.

Example: Java RMI/Corba the stub is the local proxy of remote object, Java's InvocationHandler for dynamic proxy. 

15.  Compound

A compound pattern combines two or more patterns into a solution that solves a recurring or general problem.
Example: MVC


原创粉丝点击