Skip to main content


Showing posts with the label Design pattern

Design patterns - Abstract Factory with Java Example

Introduction Abstract Factory design pattern comes under creational design pattern. It is also called factory of factories . It provides a way to encapsulate a group of factories which have common behaviour or theme without specifying which underlying concrete objects are created. It is one level of abstraction higher than Factory pattern. In Factory Design Pattern , we get instance of one of several sub-classes whereas in Abstract Factory Design pattern we get instance of one of several factories which then get instance of one of several sub-classes . Implementation We are going to create Factories for Import and Export. We have one producer class ImportExportFactoryProducer which produces the factory based on type i,e. if instance of ImportFactory is required or ExportStrategy is required which then initialize the concrete classes for ImportStrategy or ExportStrategy . 1. AbstractFactoryTest class package com . gauravbytes . test ; import com.gauravbytes ._impor

Design patterns - Strategy pattern with Java Example

What is Strategy Design pattern? Strategy design pattern comes under behavioural design pattern . It is also known as Policy pattern. It defines a family of algorithms and encapsulates each algorithm. The algorithm can be interchanged without changing much code. It bury algorithm implementation details in derived classes. It favours composition over inheritance. Derived classes could be implemented using the template design pattern (We will discuss about it later on). Implementation We will take example of Compressing files. For compressing files, we can either use zip or can use rar compression strategy. In Strategy Design pattern, we will have following classes and interface. 1. We will define our Strategy to compress files. package com . gauravbytes.strategy.client ; import ; import ; import java.util.List ; public interface CompressionStrategy { //Compression algorithm public void compressFiles ( List < F

Design Patterns - Singleton inJava with example

This post is about Singleton Design pattern in java. All of us may know, there are different way to create singleton instance of class. I am going to write one of them. How we can make singleton object. But before going further, we will first learn what singleton is? What is Singleton? Singleton is design pattern which comes under creational pattern. Sometimes, it is important to have one instance of class or we can say, it provide single global entry point to the object. How we can create Singleton? There are many ways to create Singleton. Each one of them have their pros and cons. Today, we will discuss how to create Singleton with lazy Initialization with SingletonInstanceHolder class. package singleton ; public class Singleton { private static class SingletonInstanceHolder { private static int MODCOUNT = 0 ; private static Singleton INSTANCE = null ; private static final Object MUTEX = new Object (); static { if ( INSTANCE == n