Gang of Four Design Patterns - shoikot/shoikot.github.io GitHub Wiki

Design Patterns

Design patterns provide solutions to common software design problems. In the case of object-oriented programming, design patterns are generally aimed at solving the problems of object generation and interaction, rather than the larger scale problems of overall software architecture. They give generalized solutions in the form of templates that may be applied to real-world problems.It is also important to apply patterns appropriately. Using the incorrect pattern for a situation or applying a design pattern to a trivial solution can over complicate your code and lead to maintainability issues.

The Gang of Four

The Gang of Four are the authors of the book, "Design Patterns: Elements of Reusable Object-Oriented Software". This important book describes various development techniques and pitfalls in addition to providing twenty-three object-oriented programming design patterns. The four authors were Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides

Gang of Four Design Patterns

This section gives a high-level description of the twenty-three design patterns described by the Gang of Four. Each pattern description includes a link to a more detailed article describing the design pattern and including a UML diagram, template source code and real world example using java

Creational Patterns

The first type of design pattern is the creational pattern. Creational patterns provide ways to instantiate single objects or groups of related objects. There are five such patterns:

  • Abstract Factory. The abstract factory pattern is used to provide a client with a set of related or dependent objects. The "family" of objects created by the factory are determined at run-time. In object-oriented programming (OOP), a factory is an object for creating other objects – formally a factory is a function or method that returns objects of a varying prototype or class from some method call, which is assumed to be "new".[a] More broadly, a subroutine that returns a "new" object may be referred to as a "factory", as in factory method or factory function.

  • Builder. The builder pattern is used to construct complex objects by separating construction and representation.The Builder design pattern solves problems like:

  1. How can a class (the same construction process) create different representations of a complex object?
  2. How can a class that includes creating a complex object be simplified?

Creating and assembling the parts of a complex object directly within a class is inflexible. It commits the class to creating a particular representation of the complex object and makes it impossible to change the representation later independently from (without having to change) the class.

  • Factory Method The factory pattern is used to replace class constructors, abstracting the process of object generation so that the type of the object instantiated can be determined at run-time. It creates objects without specifying the exact class to create.

  • Prototype This creates objects by cloning an existing object. The prototype pattern is used to instantiate a new object by copying all of the properties of an existing object, creating an independent clone. This practice is particularly useful when the construction of a new object is inefficient.

  • Singleton restricts object creation for a class to only one instance. The singleton pattern ensures that only one object of a particular class is ever created. All further references to objects of the singleton class refer to the same underlying instance.

Structural Patterns

The second type of design pattern is the structural pattern. Structural patterns provide a manner to define relationships between classes or objects.

  • Adapter allows classes with incompatible interfaces to work together by wrapping its own interface around that of an already existing class. The adapter pattern is used to provide a link between two otherwise incompatible types by wrapping the "adaptee" with a class that supports the interface required by the client.

  • Bridge decouples an abstraction from its implementation so that the two can vary independently The bridge pattern is used to separate the abstract elements of a class from the implementation details, providing the means to replace the implementation details without modifying the abstraction.

  • Composite composes zero-or-more similar objects so that they can be manipulated as one object. The composite pattern is used to create hierarchical, recursive tree structures of related objects where any element of the structure may be accessed and utilized in a standard manner.

  • Decorator dynamically adds/overrides behavior in an existing method of an object. The decorator pattern is used to extend or alter the functionality of objects at run-time by wrapping them in an object of a decorator class. This provides a flexible alternative to using inheritance to modify behavior.

  • Facade provides a simplified interface to a large body of code. The facade pattern is used to define a simplified interface to a more complex subsystem.

  • Flyweight reduces the cost of creating and manipulating a large number of similar objects. The flyweight pattern is used to reduce the memory and resource usage for complex models containing many hundreds, thousands or hundreds of thousands of similar objects.

  • Proxy provides a placeholder for another object to control access, reduce cost, and reduce complexity. The proxy pattern is used to provide a surrogate or placeholder object, which references an underlying object. The proxy provides the same public interface as the underlying subject class, adding a level of indirection by accepting requests from a client object and passing these to the real subject object as necessary.

Behavioral Patterns

The final type of design pattern is the behavioral pattern. Behavioral patterns define manners of communication between classes and objects.

  • Chain of Responsibility delegates commands to a chain of processing objects. The chain of responsibility pattern is used to process varied requests, each of which may be dealt with by a different handler.

  • Command creates objects which encapsulate actions and parameters. The command pattern is used to express a request, including the call to be made and all of its required parameters, in a command object. The command may then be executed immediately or held for later use.

  • Interpreter implements a specialized language. The interpreter pattern is used to define the grammar for instructions that form part of a language or notation, whilst allowing the grammar to be easily extended

  • Iterator. The iterator pattern is used to provide a standard interface for traversing a collection of items in an aggregate object without the need to understand its underlying structure.

  • Mediator allows loose coupling between classes by being the only class that has detailed knowledge of their methods. The mediator pattern is used to reduce coupling between classes that communicate with each other. Instead of classes communicating directly, and thus requiring knowledge of their implementation, the classes send messages via a mediator object. In computing and systems design a loosely coupled system is one in which each of its components has, or makes use of, little or no knowledge of the definitions of other separate components. Subareas include the coupling of classes, interfaces, data, and services.

  • Memento provides the ability to restore an object to its previous state (undo). The memento pattern is used to capture the current state of an object and store it in such a manner that it can be restored at a later time without breaking the rules of encapsulation.

  • Observer is a publish/subscribe pattern which allows a number of observer objects to see an event. The observer pattern is used to allow an object to publish changes to its state. Other objects subscribe to be immediately notified of any changes.

  • State The state pattern is used to alter the behavior of an object as its internal state changes. The pattern allows the class for an object to apparently change at run-time.

  • Strategy. The strategy pattern is used to create an interchangeable family of algorithms from which the required process is chosen at run-time.

  • Template Method defines the skeleton of an algorithm as an abstract class, allowing its subclasses to provide concrete behavior.

  • Visitor. The visitor pattern is used to separate a relatively complex set of structured data classes from the functionality that may be performed upon the data that they hold.