Skip to main content


Showing posts from October 4, 2019

Template Method Design Pattern In Java


Template Method pattern is a behavioral pattern.
We put our businesss logic in abstract in a super class and let the sub classes override specific steps without changing the original structure.

Source code for this post can be found in my github account. [source-code]

When we have a common steps to do something but some steps vary, we can do it using bunch of if else statements. But the problem is things are tightly coupled and when new things need to be added we have to change our class. This leads our class difficult to main. For these kind of problems Template Method can be used.

Definition of Gof;
"Defines the skeleton of an algorithm in a method, deferring some steps to sub-classes. Template Method lets sub-classes redefine certain steps of an algorithm without changing the algorithm's structure."

"Don't call us, we'll call you. - allow low-level components to hook themselves into a system, but the high-level components determine when they are …

Strategy Design Pattern


Strategy pattern is a behavioral pattern. It is one of the easiest patterns to learn and one of the most used pattern.

Source code for this blog post can be found in my github account. [source-code]

Let's see the GoF definition;
"Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it."

"Algorithm" in this definition is not necessarily a mathematical algorithm. It is something that do a thing. Basically algorithm is just another name for a strategy.
This leads us to;

A class should be configured with an algorithm instead of implementing an algorithm directly.An algorithm should be selected an exchanged at run time.
When we start OOP, we are fond of inheritance. But when it comes to separating the code into more manageable way, we choose composition over inheritance.
"Consider 'has-a' instead of 'is-a'"
We achieve this by extracti…

Factory Method Design Pattern


Factory method design pattern is a creational design pattern which deals with creating objects.
Many misunderstand this pattern with simple factory helper class.

Source code for this post can be found in my github account. [source-code]

This is the definition given by GoF.
"Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory method lets a class defer instantiation to subclasses."
As in original GoF design pattern, we define an interface (abstract class) to create objects that lets the sub-class to decide how to create the object.

Let's see the actors in this design pattern.
Product - This is the final object we need to create. We are dealing only with abstraction here because we can have multiple types of products.Factory - A class to create Product. But it doesn't directly create object. Instead it provides abstract method to create the Product. Sub classes of the Factory needs to implement that creation…