-
Compound Pattern
State Pattern Definition The compound pattern is that combines at least two patterns into a solution that solves a recurring or general problem. For example, Model–view–controller (MVC) pattern for implementing user interfaces on computers. It divides a given software application into three interconnected parts, so as to separate internal representations of information from the ways that information is presented to or accepted from the user. Components A typical collaboration of the MVC components. The model...
-
State & Proxy Pattern
State Pattern Definition The state pattern is a behavioral software design pattern that implements a state machine in an object-oriented way. With the state pattern, a state machine is implemented by implementing each individual state as a derived class of the state pattern interface, and implementing state transitions by invoking methods defined by the pattern’s superclass. Implementation State Interface & Concrete class public interface State { void insertQuarter(); void ejectQuarter(); void turnCrank(); void dispense(); }...
-
Iterator & Composite Pattern
Iterator Pattern Definition The iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container’s elements. The iterator pattern decouples algorithms from containers; in some cases, algorithms are necessarily container-specific and thus cannot be decoupled. Implementation Base object public interface Menu { public Iterator<MenuItem> createIterator(); } public class MenuItem { String name; String description; boolean vegetarian; double price; public MenuItem(String name, String description, boolean vegetarian, double...
-
Template Method Pattern
Template Method Pattern Definition The template method pattern is a behavioral design pattern that defines the program skeleton of an algorithm in an operation, deferring some steps to subclasses. It lets one redefine certain steps of an algorithm without changing the algorithm’s structure. Template method’s abstract class may also define hook methods that may be overridden by subclasses. Implementation Template Method Abstract Class public abstract class CaffeineBeverage { final void prepareRecipe() { boilWater(); brew(); pourInCup();...
-
Adapter & Facade Pattern
Adapter Pattern Definition The adapter pattern is a software design pattern (also known as Wrapper, an alternative naming shared with the Decorator pattern) that allows the interface of an existing class to be used as another interface. It is often used to make existing classes work with others without modifying their source code. Both Adapter pattern and Decorator pattern is usually called as Wrapper. But the purpose is basically different. At decorator pattern, we usually...
-
Command Pattern
Definition Command Pattern The command pattern is a behavioral design pattern in which an object is used to encapsulate all information needed to perform an action or trigger an event at a later time. This information includes the method name, the object that owns the method and values for the method parameters. Command A command object knows about receiver and invokes a method of the receiver. Values for parameters of the receiver method are stored...
-
Singleton Pattern
Definition Singleton Pattern The singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects. The term comes from the mathematical concept of a singleton. Implementation Classical Example...
-
Factory Pattern
Definition Factory Method Pattern The factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created. This is done by creating objects by calling a factory method—either specified in an interface and implemented by child classes, or implemented in a base class and optionally overridden by derived classes—rather than by calling a constructor....
-
Decorator Pattern
Definition The Decorator pattern (also known as Wrapper, an alternative naming shared with the Adapter pattern) is a design pattern that allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class. The decorator pattern is often useful for adhering to the Single Responsibility Principle, as it allows functionality to be divided between classes with unique areas of concern. OCP (Open-Closed Principle)...
-
Observer Pattern
Definition The Observer pattern is a software design pattern in which an object, called the Subject, maintains a list of its dependent object, called Observers, and notifies them automatically of any state changes, usually by calling one of their methods. It is mainly used to implement distributed event handling systems. Loose Coupling Loose Coupling means that they interact each other, but they don’t know each other well. It is loosely coupled between Subject and Observer....