Design Patterns

Design patterns represent the best practices used by experienced object-oriented software developers. Design patterns are solutions to general problems that software developers faced during software development. These solutions were obtained by trial and error by numerous software developers over quite a substantial period of time.

Uses of Design Patterns

Design patterns can speed up the development process by providing tested, proven development paradigms. Effective software design requires considering issues that may not become visible until later in the implementation. Reusing design patterns helps to prevent subtle issues that can cause major problems and improves code readability for coders and architects familiar with the patterns.

Often, people only understand how to apply certain software design techniques to certain problems. These techniques are difficult to apply to a broader range of problems. Design patterns provide general solutions, documented in a format that doesn’t require specifics tied to a particular problem.

In addition, patterns allow developers to communicate using well-known, well understood names for software interactions. Common design patterns can be improved over time, making them more robust than ad-hoc designs.

The 23 Gang of Four (GoF) patterns are generally considered the foundation for all other patterns. Let us learn with examples.

Related Tags

Tutorials

SOLID principles

Single Responsibility Principle

The single responsibility principle (SRP) states that a software component (in general, a class) must have one and only one responsibility.

State Design Pattern

The state pattern is a behavioral design pattern. According to GoF definition, a state allows an object to alter its behavior when its internal state changes. The object will appear to change its class. It can be drawn from above definition that there shall be a separate concrete class per …

Mediator Design Pattern

Mediator helps in establishing loosely coupled communication between objects and helps in reducing the direct references to each other.

Observer Design Pattern

According to the GoF definition, the observer pattern defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. It is also referred to as the publish-subscribe pattern. In the observer pattern, many observers (subscriber objects) observe a particular subject …

Memento Design Pattern

Memento design pattern is behavioral pattern and one of 23 design patterns discussed by Gang of Four. Memento pattern is used to restore state of an object to a previous state. It is also known as snapshot pattern. A memento is is like a restore point during the life cycle …

Iterator Design Pattern

An iterator design pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

Proxy Design Pattern

According to GoF definition of proxy design pattern, a proxy object provide a surrogate or placeholder for another object to control access to it. A proxy is basically a substitute for an intended object which we create due to many reasons e.g. security reasons or cost associated with creating fully …

Flyweight Design Pattern

As per GoF definition, flyweight design pattern enables use sharing of objects to support large numbers of fine-grained objects efficiently. A flyweight is a shared object that can be used in multiple contexts simultaneously. The flyweight acts as an independent object in each context. 1. When to use flyweight design …

Facade Design Pattern

Facade design pattern provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.

Command Design Pattern

Command pattern is a behavioral design pattern which is useful to abstract business logic into discrete actions i.e. commands. It motivates loose coupling.

Strategy Design Pattern

Strategy design pattern is behavioral design pattern where we choose a specific implementation of algorithm or task in run time – out of multiple other implementations for same task.

Template Method Design Pattern

Template method design pattern is widely accepted behavioral design pattern to enforce some sort of algorithm (fixed set of steps) in the context of programming. It defines the sequential steps to execute a multi-step algorithm and optionally can provide a default implementation as well (based on requirements). Table of Contents …

Bridge Pattern (with Example)

Bridge pattern is about preferring composition over inheritance. Implementation details are pushed from a hierarchy to another object with a separate hierarchy.

Adapter Pattern

The adapter design pattern is a structural design pattern used to allow two unrelated interfaces to work together. The object that joins these unrelated interfaces is called an Adapter. The definition of Adapter provided in the original Gang of Four book on Design Patterns states: “Convert the interface of a …

Java Builder Pattern: Build Complex Objects Efficiently

The builder pattern, as the name implies, is an alternative way to construct complex objects. This pattern should be used when we want to build different immutable objects using the same object-building process. 1. The GoF Builder Pattern Before starting the discussion, I want to make it clear that the …

Decorator Pattern

In software engineering, the decorator design pattern is used to add additional features or behaviors to a particular instance of a class without modifying the other instances of the same class. The decorator pattern is often useful for adhering to the Single Responsibility Principle, as it allows functionality to be …

Visitor Design Pattern Example

Design patterns are used to solve the problems which occur in a pattern, we all know that, right? Also we know that behavioral design patterns are design patterns that identify common communication patterns between objects. One of such behavioral patterns is visitor pattern, which we are going to learn about …

Chain of Responsibility Design Pattern

The Chain of Responsibility is known as a behavioral pattern. The main objective of this pattern is that it avoids coupling the sender of the request to the receiver, giving more than one object the opportunity to handle the request. The core logic defined by GoF is : “Gives more …

Prototype Pattern

The prototype design pattern is used in scenarios where an application needs to create a number of instances of a class that have almost the same state or differ very little.

Abstract Factory Pattern

Abstract factory is a factory of factories; a factory that groups the individual but related/dependent factories together without specifying their concrete classes.

Factory Pattern

The factory pattern simply generates an instance for client without exposing its instantiation logic to the client.

Singleton Pattern (with Example)

To create a singleton, make the constructor private, disable cloning, disable extension and create a static variable to house the instance

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.