Sitemap
chanthirakumar chayanthithan

chanthirakumar chayanthithan

Design Patterns

8 min readAug 30, 2023

--

Press enter or click to view image in full size
design patterns

Design pattern is a general reusable solution to commonly occurring problem in software design within a given context.Design patterns are formalized best practices that the programmer can use to solve common problems when designing an application or system

Why do we need to know design patterns?

  • Design patterns are a toolkit of tried and tested solutions to common problems in software design.
  • Design Patterns provide easy-to-recognize and use OOP solutions to common problems.
  • Design patterns define a common language that you and your teammates can use to communicate more efficiently.
  • Design patterns can speed up the development process.

Classification of patterns

  1. Creational

In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or in added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation..

Five well-known design patterns that are parts of creational patterns are the

a. Abstract factory

which provides an interface for creating related or dependent objects without specifying the objects’ concrete classes

uml diagram for abstract factory patterns
Abstract factory-UML class diagram

The pattern describes how to solve such problems:

  • Encapsulate object creation in a separate (factory) object by defining and implementing an interface for creating objects.
  • Delegate object creation to a factory object instead of creating objects directly.

Here’s a simple example of the Abstract Factory design pattern in Java:

b. Builder

The builder pattern is a design pattern designed to provide a flexible solution to various object creation problems in object-oriented programming. The intent of the builder design pattern is to separate the construction of a complex object from its representation. It is one of the Gang of Four design patterns.

builder ulm class diagram

Here’s a simple example of the Abstract Factory design pattern in Java:

c. Factory method

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.

factor method uml class diagram

Here’s a simple example of the factory method design pattern in Java:

d. Prototype

which specifies the kind of object to create using a prototypical instance, and creates new objects by cloning this prototype.

prototype uml class diagram

Here’s a simple example of the prototype design pattern in Java:

e. Singleton

which ensures that a class only has one instance, and provides a global point of access to it

UML class diagram for singleton

Here’s a simple example of the singleton design pattern in Java:

2. Structural

These patterns define how objects relate to each other. They are easy to design by identifying a simple way to realize relationships between entities.

a.Adapter

It is allows classes with incompatible interfaces to work together by wrapping its own interface around that of an already existing class.

uml class diagram for the adapter design pattern

Here’s a simple example of the adapter design pattern in Java:

b.Bridge

decouples an abstraction from its implementation so that the two can vary independently.

uml class diagram for the bridge design pattern

Here’s a simple example of the bridge design pattern in Java:

c.Composite

Composite composes zero-or-more similar objects so that they can be manipulated as one object.

uml class diagram for the composite design pattern

Here’s a simple example of the composite design pattern in Java:

d.Decorator

dynamically adds/overrides behaviour in an existing method of an object.

uml class diagram for the decorator design pattern

Here’s a simple example of the decorate design pattern in Java:

e.Facade

Get chanthirakumar chayanthithan’s stories in your inbox

Join Medium for free to get updates from this writer.

provides a simplified interface to a large body of code.

uml class diagram for the facade design pattern

Here’s a simple example of the facade design pattern in Java:

f.Flyweight

reduces the cost of creating and manipulating a large number of similar objects.

uml class diagram for the flyweight design pattern

Here’s a simple example of the flyweight design pattern in Java:

g.Proxy

provides a placeholder for another object to control access, reduce cost, and reduce complexity.

uml class diagram for the proxy design pattern

Here’s a simple example of the proxy design pattern in Java:

3. Behavioral

These patterns are designed depending on how one class communicates with others. They focus on the interaction between cooperated objects. In simple words, Behavioral Design Patterns are responsible for communication between objects.

a.Chain of responsibility

The Chain of Responsibility design pattern is one of the twenty-three well-known GoF design patterns that describe common solutions to recurring design problems when designing flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse.

UML class and sequence diagram for the Chain of Responsibility

Here’s a simple example of the chain for responsibility design pattern in Java:

b.Command

The command design pattern is one of the twenty-three well-known GoF design patterns that describe how to solve recurring design problems to design flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse.

UML class and sequence diagram for the command

Here’s a simple example of the command design pattern in Java:

c.Interpreter

The Interpreter design pattern is one of the twenty-three well-known GoF design patterns that describe how to solve recurring design problems to design flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse.

UML class and sequence diagram for the interpreter

Here’s a simple example of the interpreter design pattern in Java:

d.Iterator

The essence of the Iterator Pattern is to “Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation

Press enter or click to view image in full size
UML class and sequence diagram for the iterator

Here’s a simple example of the iterator design pattern in Java:

e.Mediator

The essence of the mediator pattern is to “define an object that encapsulates how a set of objects interact”. It promotes loose coupling by keeping objects from referring to each other explicitly, and it allows their interaction to be varied independently. Client classes can use the mediator to send messages to other clients, and can receive messages from other clients via an event on the mediator class.

UML class and sequence diagram for the mediator

Here’s a simple example of the mediator design pattern in Java:

f.Memento

The memento pattern is a software design pattern that exposes the private internal state of an object. One example of how this can be used is to restore an object to its previous state (undo via rollback), another is versioning, another is custom serialization.

Press enter or click to view image in full size
UML class and sequence diagram for the memento

Here’s a simple example of the memento design pattern in Java:

g.Observer

Observer pattern is used when there is one-to-many relationship between objects such as if one object is modified, its depenedent objects are to be notified automatically. Observer pattern falls under behavioral pattern category.

UML class and sequence diagram for the observer

Here’s a simple example of the observer design pattern in Java:

h.State

A state design pattern is used when an Object changes its behavior based on its internal state. If we have to change the behavior of an object based on its state, we can have a state variable in the Object and use the if-else condition block to perform different actions based on the state. The state pattern is used to provide a systematic and lose-coupled way to achieve this through Context and State implementations.

UML class and sequence diagram for the state

Here’s a simple example of the state design pattern in Java:

i.Strategy

In Strategy pattern, we create objects which represent various strategies and a context object whose behavior varies as per its strategy object. The strategy object changes the executing algorithm of the context object.

UML class and sequence diagram for the strategy

Here’s a simple example of the strategy design pattern in Java:

j.Template method

In Template pattern, an abstract class exposes defined way(s)/template(s) to execute its methods. Its subclasses can override the method implementation as per need but the invocation is to be in the same way as defined by an abstract class. This pattern comes under behavior pattern category.

UML class and sequence diagram for the template method

Here’s a simple example of the template method design pattern in Java:

k.Visitor

In Visitor pattern, we use a visitor class which changes the executing algorithm of an element class. By this way, execution algorithm of element can vary as and when visitor varies. This pattern comes under behavior pattern category. As per the pattern, element object has to accept the visitor object so that visitor object handles the operation on the element object.

Press enter or click to view image in full size
UML class and sequence diagram for the visitor

Here’s a simple example of the visitor design pattern in Java:

--

--