Design Patterns

What is a Design Pattern?

A design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. Not all software patterns are design patterns. Design patterns deal specifically with problems at the level of software design. Other kinds of patterns, such as architectural patterns, describe problems and solutions that have alternative scopes.

Head First Design PatternsThe following are design patterns that were extracted from the Head First Design Patterns book and you can apply to Object-oriented design:

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 Principles:

  1. Identify the aspects of your application that very and separate them from what stays the same.
  2. Program to an interface, not an implementation.
  3. Favor composition over inheritance.

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 Principles:

  1. Strive for loosely coupled designs between objects that interact.
  2. Identify the aspects of your application that very and separate them from what stays the same.
  3. Program to an interface, not an implementation.
  4. Favor composition over inheritance.

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

Design Principles:

  1. Classes should be open for extension, but closed for modification.
The Factory Method Pattern – defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a clas defer instantiation to subclasses.

Design Principles:

  1. Depend upon abstractions. Do not depend upon concrete classes.

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

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 undo able 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 interface.

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.

Design Principles:

  1. Principle of Least Knowledge – talk only to your immediate friends.

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.

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

Design Principles:

  1. A class should have only one reason to change.

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 object 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 access to it.

The Compound Pattern – combines two or more patterns into a solution that solves a recurring or general problem.