This project contains many sample codes for demonstrating the usage of the following design patterns:
- The Strategy Pattern: Encapsulates interchangeable behaviors and uses delegation to decide which one to use.
- The Observer Pattern: Allows objects to be notified when state changes.
- The Decorator Pattern: Wraps an object to provide new behavior.
- The Factory Pattern:
- Factory Method: Subclasses decide which concrete classes to create.
- Abstract Factory: Allows a client to create families of objects without specifying their concrete classes.
- The Singleton Pattern: Ensures one and only object is created.
- The Command Pattern: Encapsulates a request as an object.
- The Adapter and Facade Patterns:
- Adapter: Wraps an object and provides a different interface to it.
- Facade: Simplifies the interface of a set of classes.
- The Template Method Pattern: Subclasses decide how to implement steps in an algorithm.
- The Iterator and Composite Patterns:
- Iterator: Provides a way to traverse a collection of objects without exposing its implementation.
- Composite: Clients treat collections of objects and individual objects uniformly.
- The State Pattern: Encapsulates state-based behaviors and uses delegation to switch between behaviors.
- The Proxy Pattern: Wraps an object to control access to it.
- The Compound Pattern: Combines two or more patterns into a solution that solves a recurring or general problem.
- The problem in each module is from the book Head First Design Patterns, 2nd Edition by Eric Freeman and Elisabeth Robson.
- The code, definitions, principles, and some diagrams in this project are referenced from that book.
- 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 upon abstractions. Do not depend upon concrete classes.
- Principle of Least Knowledge: talk only to your immediate friends (see demonstrations here for more details).
- The Hollywood Principle: don't call us, we'll call you (see demonstrations here for more details).
- The Single Responsibility Principle: a class should have only one reason to change.
- Creational patterns involve object instantiation and all provide a way to decouple a client from the objects it needs to instantiate:
- Factory Method Pattern
- Abstract Factory Pattern
- Singleton Pattern
- Any pattern that is a Behavioral Pattern is concerned with how classes and objects interact and distribute responsibility:
- Strategy Pattern
- Observer Pattern
- Command Pattern
- Template Method Pattern
- Iterator Pattern
- State Pattern
- Structural patterns let you compose classes or objects into larger structures:
- Decorator Pattern
- Adapter Pattern
- Facade Pattern
- Composite Pattern
- Proxy Pattern