Category Archives: Design

Flyweight Design Pattern

Flyweight pattern is one of the structural design patterns as this pattern provides ways to decrease object count thus improving application required objects structure. Flyweight pattern is used when we need to create a large number of similar objects (say 105). One important feature of flyweight objects is that they are immutable. This means that… Read More »

Curiously recurring template pattern (CRTP)

Background: It is recommended to refer Virtual Functions and Runtime Polymorphism as a prerequisite of this. Below is an example program to demonstrate run time polymorphism. Output : Time taken: 2613 nanoseconds See this for above result. When a method is declared virtual, compiler secretly does two things for us: Defines a VPtr in first… Read More »

Iterator Pattern

Iterator Pattern is a relatively simple and frequently used design pattern. There are a lot of data structures/collections available in every language. Each collection must provide an iterator that lets it iterate through its objects. However while doing so it should make sure that it does not expose its implementation. Suppose we are building an… Read More »

Opaque Pointer

What is an opaque pointer? Opaque as the name suggests is something we can’t see through. e.g. wood is opaque. Opaque pointer is a pointer which points to a DS whose contents are not exposed at the time of its definition. Following pointer is opaque. One can’t know the data contained in STest structure by… Read More »

Command Pattern

Like previous articles, let us take up a design problem to understand command pattern. Suppose you are building a home automation system. There is a programmable remote which can be used to turn on and off various items in your home like lights, stereo, AC etc. It looks something like this.   You can do it with… Read More »

Adapter Pattern

This pattern is easy to understand as the real world is full of adapters.   For example consider a USB to Ethernet adapter. We need this when we have an Ethernet interface on one end and USB on the other. Since they are incompatible with each other. we use an adapter that converts one to other.… Read More »

Strategy Pattern | Set 2 (Implementation)

We have discussed a fighter example and introduced Strategy Pattern in set 1. Strategy Pattern | Set 1 (Introduction) In this post, we apply Strategy Pattern to the Fighter Problem and discuss implementation. The first step is to identify the behaviors that may vary across different classes in future and separate them from the rest.… Read More »

Strategy Pattern | Set 1 (Introduction)

As always we will learn this pattern by defining a problem and using strategy pattern to solve it. Suppose we are building a game “Street Fighter”. For simplicity assume that a character may have four moves that is kick, punch, roll and jump. Every character has kick and punch moves, but roll and jump are… Read More »

Decorator Pattern | Set 3 (Coding the Design)

We have discussed Pizza design problem and different naive approaches to solve it in set 1. We have also introduced Decorator pattern in Set 2. In this article, design and implementation of decorator pattern for Pizza problem is discussed. It is highly recommended that you try it yourself first. The new class diagram (Click on… Read More »

The Decorator Pattern | Set 2 (Introduction and Design)

As we saw our previous designs using inheritance didn’t work out that well. In this article, decorator pattern is discussed for the design problem in previous set. So what we do now is take a pizza and “decorate” it with toppings at runtime: Take a pizza object. “Decorate” it with a Capsicum object. “Decorate” it… Read More »

Decorator Pattern | Set 1 (Background)

To understand decorator pattern let us consider a scenario inspired from the book “Head First Design Pattern”.  Suppose we are building an application for a pizza store and we need to model their pizza classes. Assume they offer four types of pizzas namely Peppy Paneer, Farmhouse, Margherita  and Chicken Fiesta. Initially we just use inheritance… Read More »