The open/closed principle (OCP) states that a module should be open to extension but closed for modification. It is one of famous 5 solid principles and very important object oriented design principle. 1. Definition of open closed principle There are two popular definitions to describe this principle – 1.1. The Meyer definition Bertrand Mayer, in […]
The single responsibility principle (SRP) states that a software component (in general, a class) must have only one responsibility. The fact that the class has a sole responsibility means that it is in charge of doing just one concrete thing, and as a consequence of that, we can conclude that it must have only one reason to change. It is one of 5 famous SOLID principles.
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 possible state of an object. […]
Mediator helps in establishing loosely coupled communication between objects and helps in reducing the direct references to each other.
According to GoF definition, 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 observer pattern, there are many observers (subscriber objects) that are observing a particular subject (publisher object). Observers register […]
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 on the object, which the […]
An iterator design pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
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 initialized original object. 1. When […]
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 pattern We can use flyweight […]
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 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 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 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 Introduction Problem Statement Solution Code […]
Composite design pattern is a structural pattern which modifies the structure of an object. This pattern is most suitable in cases where you need to work with objects which form a tree like hierarchy. In that tree, each node/object (except root node) is either composite or leaf node. Implementing the composite pattern lets clients treat […]
Bridge design pattern is used to decouple a class into two parts – abstraction and it’s implementation – so that both can evolve in future without affecting each other. It increases the loose coupling between class abstraction and it’s implementation. Table of Contents Design participants of bridge design pattern When we need bridge design pattern […]
Ever tried to use a your camera memory card in your laptop. You cannot use it directly simply because there is no port in laptop which accept it. You must use a compatible card reader. You put your memory card into card reader and then inject card reader into laptop. This card reader can be […]
The builder pattern, as name implies, is an alternative way to construct complex objects. This should be used only when you want to build different immutable objects using same object building process. Before starting the discussion, I want to make it clear that the builder pattern which I am going to discuss in this post, […]
In software engineering, decorator design pattern is used to add additional features or behaviors to a particular instance of a class, while not modifying the other instances of same class. Decorators provide a flexible alternative to sub-classing for extending functionality. Please note that the description above implies that decorating an object changes its behavior but […]
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 in this post. If you […]
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 than one object an opportunity […]