Head First Design Patterns I | Book Report

Tseng1026
tseng1026
Published in
4 min readJul 9, 2022

Chapter 2 — Observer Pattern 觀察者模式

Let you define a subscription mechanism to notify multiple objects about any events that happen to the object they’re observing.

  • Define a one-to-many relationship between subject and observers
  • Observers are loosely coupled
    1. the subject know nothings about them;
    2. they implement the subject interface
  • To implement
    1. subjects can push data to observers;
    2. observers can pull data from subjects (consider more “correct”)

Chapter 3 — Decorator Pattern 裝飾者模式

Let you attach new behaviors to objects by placing these objects inside special wrapper objects that contain the behaviors.

  • Attach additional responsibilities to an object dynamically
  • Provide a flexible alternative to subclassing for extending functionality
  • Decorators are transparent to the component
  • Overuse can be complex and lead to low readability
  • To implement
    1. a set of decorators wrap the concrete components;
    2. the decorators and the components are the same type

Chapter 4 — Factory Pattern 工廠模式

Provide an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.

  • Define an interface to create an object, but let subclasses decide which class to instantiate
  • Let a class defer instantiation to its subclasses
  • Rely on inheritance

Let you produce families of related objects without specifying their concrete classes.

  • Provide an interface to create families of related or dependent objects without having to depend on their concrete classes
  • Rely on object composition

Chapter 5 — Singleton Pattern 單件模式

Let you ensure that a class has only one instance, while providing a global access point to this instance.

  • Ensure that a class has just a single instance.
  • Provide a global access point to that instance.
  • To implement
    1. make the default constructor private, to prevent other objects to initiate the Singleton class;
    2. create a static creation method that acts as a constructor. Under the hood, this method calls the private constructor to create an object and saves it in a static field

Chapter 6 — Command Pattern 命令模式

Turn a request into a stand-alone object that contains all information about the request. Let you pass requests as a method arguments, delay or queue a request’s execution, and support undoable operations.

  • Decouple an object, making a request from the one that knows how to perform it.
  • Parameterize clients with different requests, queue or log requests, and support undoable operations
  • A command object is at the center of the decoupling and encapsulates a receiver with an action
  • An invokers makes a request of a command object by calling its execute() method

--

--

Tseng1026
tseng1026

BS in Computer Science, National Taiwan University (NTU); MS in Artificial Intelligence, National University of Singapore (NUS)