This repository is used to have a quick overview on those design patterns introduced from the book Head First Design Patterns.
A Pattern is a solution to a problem in a context:
- The context is the situaion in which the pattern applies. This should be a recurring situation.
- The problem refers to the goal you are trying to achieve in this context, but it also referes to any constraints that occur in the context.
- Ths solution is what you're after: a general design that anyone can apply which resolves the goal and set of constraints.
Note that the diagram is created via Draw.io.
- To export reusable SVG format, config the export:
- ZOOM: 100%
- Border Width: 0
- Include a copy of my diagram: check
- To export the PNG diagram, config the export:
- ZOOM: 100%
- Border Width: 20
-
Favor composition over inheritance.
-
Strive for loosely coupled designs between objects that interact.
-
Identify the aspects of your application that vary and separate them from what stays the same.
-
Program to an interface, not an implementation.
-
Classes should be open for extension, but closed for modification.
-
Depend upon abstractions. Do not depend upon concrete classes.
-
Principle of Least Knowledge - talk only to your immediate friends.
-
A class should have only one reason to change.
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.
Example Diagram:
How Strategy is actually applied:
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.
For simplicity, the connection between Subject
& StatisticsDiplay
in Push mode, and connection between Observable
& StatisticsDiplay
in Pull mode is not drawn.
In Push mode, it's WeatherData
to automatically push notifications to all observers. Whenever there is a change, WeatherData
would call each observer's update()
method with its attributes to notify.
Example Diagram:
Like the Push mode, WeatherData
would notify all observers when there is a change under Pull mode. However, unlike data is forcely pushed to observers under Push mode, it's the observer's choice to decide whether to "pull" data from the Observable
since the update()
method in Pull mode pass the Observable
in. That's the key difference between Pull and Push.
Note that the code implemented in Pull mode uses existing Observer
and Observable
from java.util
library.
Example Diagram:
The Decorator Pattern attaches additional responsibilities to an object dynamically. Decorator provides a flexible alternative to subclassing for extending functionality.
Example Diagram:
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.
The Abstract Factory Pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes.
Factory Method is not the only technique for adhering to the Dependency Inversion Principle, but it is one of the more powerful ones. The following guidelines can help you avoid OO designs that violate the Dependency Inversion Principle:
- No variable should hold a reference to a concrete class
- No class should derive from a concrete class
- No method should override an implemented method of any of its base classes
Defining a simple factory as a static method is a common technique and is often called a static factory. Why use a static method? Because you don’t need to instantiate an object to make use of the create method. But remember it also has the disadvanage that you can’t subclass and change the behavior of the create method.
Set Pizza
to be abstract is because a pizza must be concrete, such as CheesePizza
, VeggiePizza
.
Example Diagram:
The Factory Method Pattern encapsulates object creation by letting subclasses decide what objects to create.
For Simple Factory, there is one factory to createPizza()
. However, it's concrete PizzaStore
's job to createPizza()
when it comes to Factor Method.
Example Diagram:
How Factory Method is actually applied:
The higher level diagram of the Factory Method.
Example Diagram:
The higher level diagram of the Abstract Factory.
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.
Example Diagram:
The mapping of the simple remote control is as below:
- Invoker:
SimpleRemoteControl
- Command:
Command
- ConcreteCommand:
LightOnCommand
- Receiver:
Light
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.
Example Diagram:
The sketch mapping of the example is as below:
- Target:
Duck
- Adapter:
TurkeyAdapter
- Adaptee:
WildTurkey
The Facade Pattern provides a unified interface to a set of interfaces in a subsytem. Facade defines a higher-level interface that makes the subsystem easier to use.
Example Diagram:
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.
Example Diagram:
The Iterator Pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
Example Diagram:
The sketch of pattern Iterator is:
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.
Example Diagram:
The sketch of pattern Composite is:
The State Pattern allows an object to alter its behavior when its internal state changes. The object will appear to change its class.
Example Diagram:
The sketch of pattern State is:
The Proxy Pattern provides a surrogate or placeholder for another object to control access to it.
Example Diagram:
The sketch of pattern Proxy is:
- Online book: https://www.oreilly.com/library/view/head-first-design/0596007124/
- Official source codes: https://wickedlysmart.com/headfirstdesignpatterns/
- Book pdf: http://ce.sharif.edu/courses/98-99/2/ce484-1/resources/root/Design%20Patterns/Eric%20Freeman,%20Elisabeth%20Freeman,%20Kathy%20Sierra,%20Bert%20Bates-Head%20First%20Design%20Patterns%20-OReilly%20(2008).pdf
- Design Patterns - Refactoring.Guru: https://refactoring.guru/design-patterns
- 图说设计模式: https://design-patterns.readthedocs.io/zh_CN/latest/
- Java Design Patterns: https://github.com/iluwatar/java-design-patterns