Understanding strategy pattern by designing game of chess

Today we will try to understand Strategy Pattern with the help of an example.
The example we will consider is The Game of Chess. The intention here is to explain strategy pattern and not to build a comprehensive Chess Game solution.

Strategy Pattern : The Strategy pattern is known as a behavioural pattern – it’s used to manage algorithms, relationships and responsibilities between objects. The main benefit of strategy pattern is to choose the algorithm/behaviour at runtime.

StrategyPattern

Lets try to understand this by implementing this to design the chess game.

Read More »

Advertisements

Pizza problem- builder vs decorator

Problem Statement :
We need to build the software for a pizza company who wants to prepare different types of pizzas, e.g Chicken Pizza, Flat Bread, Pepperoni Pizza with Extra Cheese, put add on toppings on it.

Lets try to see which design pattern suits this problem statement and under what scenario.
Traditionally, for pizza problem, builder pattern is most commonly used. However there are some examples using decorator as well, both the approaches are correct but there is difference in use case.
Builder is an object creation pattern whereas decorator is used to change the already built object at runtime.
Lets try to understand this by the examples :
Read More »

Difference between Factory and Abstract Factory

The main difference between the two is Inheritance vs Composition.
Abstract Factory uses composition whereas Factory like template uses inheritance.

Abstract factory pattern can use factory method to actulay implement different types of object creation, but it is not necessary.

I would say Abstract Factory Pattern is more into design product creations and how different variants of the product would be created , it is to supply a “kit” to create product and its different variants. It may use prototype/factory method and other builder pattern inside when actualy instantiating the object.

So In Abstract Factory Pattern ,we would provide an interface of product which can be used to create its families without specifying their concrete implementations. Lets try to understand this by example.
Read More »