gauravbytes.com is now codefoundry.dev.

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._import.ImportStrategy;import com.gauravbytes.export.ExportStrategy;import com.gauravbytes.importexport.factory.AbstractImportExportFactory;import com.gauravbytes.importexport.factory.ImportExportFactoryProducer;import com.gauravbytes.importexport.factory.ImportExportFactoryTypes;

/**
 * @author Gaurav Rai Mazra
 */
public class AbstractFactoryTest {

 public static void main(String[] args) {
  int factoryType = ImportExportFactoryTypes.TYPE_EXPORT;
  int strategyType = AbstractImportExportFactory.TYPE_EXCEL;
  
  AbstractImportExportFactory ioFactory = ImportExportFactoryProducer.getInstance().getFactory(factoryType);
  
  switch (factoryType) {
   case ImportExportFactoryTypes.TYPE_EXPORT :
    ExportStrategy exportStrategy = ioFactory.getExportStrategy(strategyType);
    exportStrategy.export();
    break;
    
   case ImportExportFactoryTypes.TYPE_IMPORT : 
    ImportStrategy importStrategy = ioFactory.getImportStrategy(strategyType);
    importStrategy.importFile();
    break;
   default:
    break;
  }
 }
}

2. FactoryProducer class, In our case ImportExportFactoryProducer

package com.gauravbytes.importexport.factory;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * This is Factory producer class
 * @author Gaurav Rai Mazra
 *
 */
public class ImportExportFactoryProducer {
 private static ImportExportFactoryProducer factoryInstance = null;
 private static final Lock lock = new ReentrantLock();
 private ImportExportFactoryProducer() {
  
 }
 
 public static ImportExportFactoryProducer getInstance() {
  try {
   lock.lock();
   if (factoryInstance == null)
    factoryInstance = new ImportExportFactoryProducer();
  }
  finally {
   lock.unlock();
  }
  return factoryInstance;
 }
 
 
 public AbstractImportExportFactory getFactory(int factoryType) {
  AbstractImportExportFactory factory = null;
  switch (factoryType) {
   case ImportExportFactoryTypes.TYPE_IMPORT:
    factory = ImportFactory.getInstance(lock);
    break;
    
   case ImportExportFactoryTypes.TYPE_EXPORT:
    factory = ExportFactory.getInstance(lock);
    break;
 
   default:
    break;
  }
  return factory;
 }
}

3. Factory of Factory  and the concrete factories . In our case AbstractImportExportFactory, ImportFactory and ExportFactory.

package com.gauravbytes.importexport.factory;

import com.gauravbytes._import.ImportStrategy;import com.gauravbytes.export.ExportStrategy;

/**
 * This is factory of factory gives the instance for particular strategy
 * @author Gaurav Rai Mazra
 *
 */
public abstract class AbstractImportExportFactory {
 public static final int TYPE_EXCEL = 1;
 public static final int TYPE_PDF = 2;
 public static final int TYPE_PLAIN = 3;
 
 
 public abstract ImportStrategy getImportStrategy(int strategyType);
 public abstract ExportStrategy getExportStrategy(int strategyType);
}
package com.gauravbytes.importexport.factory;

/**
 * @author Gaurav Rai Mazra
 */
public abstract class ImportExportFactoryTypes {
 public static final int TYPE_IMPORT = 1;
 public static final int TYPE_EXPORT = 2;
}
package com.gauravbytes.importexport.factory;

import com.gauravbytes._import.ExcelImport;import com.gauravbytes._import.ImportStrategy;import com.gauravbytes._import.PdfImport;import com.gauravbytes._import.PlainTextImport;import com.gauravbytes.export.ExportStrategy;
import java.util.concurrent.locks.Lock;

/**
 * Import factory
 * @author Gaurav Rai Mazra
 */
public class ImportFactory extends AbstractImportExportFactory {

 private static ImportFactory importFactory = null;
 private ImportFactory() {
  
 }
 
 public static ImportFactory getInstance(Lock lock) {
  try {
   lock.lock();
   if (importFactory == null)
    importFactory = new ImportFactory();
  }
  finally {
   lock.unlock();
  }
  return importFactory;
 }
 
 @Override
 public ImportStrategy getImportStrategy(int strategyType) {
  ImportStrategy importStrategy = null;
  switch (strategyType) {
   case TYPE_EXCEL:
    importStrategy = new ExcelImport();
    break;
   
   case TYPE_PDF:
    importStrategy = new PdfImport();
    break;
    
   case TYPE_PLAIN:
    importStrategy = new PlainTextImport(); 
    break;
 
   default:
    break;
  }
  return importStrategy;
 }

 @Override
 public ExportStrategy getExportStrategy(int strategyType) {
  return null;
 }

}
package com.gauravbytes.importexport.factory;

import com.gauravbytes._import.ImportStrategy;import com.gauravbytes.export.ExcelExport;import com.gauravbytes.export.ExportStrategy;import com.gauravbytes.export.PdfExport;import com.gauravbytes.export.PlainTextExport;
import java.util.concurrent.locks.Lock;


/**
 * Factory to get proper strategy object based on its type
 * @author Gaurav Rai Mazra
 */
public class ExportFactory extends AbstractImportExportFactory {

 private static ExportFactory exportFactory = null;
 
 private ExportFactory() {
  
 }
 
 public static ExportFactory getInstance(Lock lock) {
  try {
   lock.lock();
   if (exportFactory == null)
    exportFactory = new ExportFactory();
  }
  finally {
   lock.unlock();
  }
  return exportFactory;
 }
 
 @Override
 public ImportStrategy getImportStrategy(int strategyType) {
  return null;
 }

 @Override
 public ExportStrategy getExportStrategy(int strategyType) {
  ExportStrategy strategy = null;
  switch (strategyType) {
   case TYPE_EXCEL:
    strategy = new ExcelExport();
    break;

   case TYPE_PDF:
    strategy = new PdfExport();
    break;

   case TYPE_PLAIN:
    strategy = new PlainTextExport();
    break;

   default:
    break;
  }
  return strategy;
 }
}

4. ImportStrategy and their concrete classes

package com.gauravbytes._import;

/**
 * @author Gaurav Rai Mazra
 */
public interface ImportStrategy {
 public void importFile();
}
package com.gauravbytes._import;

/**
 * @author Gaurav Rai Mazra
 */
public class ExcelImport implements ImportStrategy {

 @Override
 public void importFile() {
  // Logic to import excel file goes here
 }

}
package com.gauravbytes._import;

/**
 * @author Gaurav Rai Mazra
 */
public class PdfImport implements ImportStrategy {

 @Override
 public void importFile() {
  //Logic to import pdf goes here
 }

}
package com.gauravbytes._import;

/**
 * @author Gaurav Rai Mazra
 */
public class PlainTextImport implements ImportStrategy {
 @Override
 public void importFile() {
  //Logic to import plain text file
 }
}

5. ExportStrategy with their concrete classes

package com.gauravbytes.export;

/**
 * @author Gaurav Rai Mazra
 */
public interface ExportStrategy {
 public void export();
}
package com.gauravbytes.export;

/**
 * @author Gaurav Rai Mazra
 */
public class ExcelExport implements ExportStrategy {

 @Override
 public void export() {
  // Excel export code goes here
 }

}
package com.gauravbytes.export;

/**
 * @author Gaurav Rai Mazra
 */
public class PlainTextExport implements ExportStrategy {
 @Override
 public void export() {
  // Plain text export goes here
 }
}
package com.gauravbytes.export;

/**
 * @author Gaurav Rai Mazra
 */
public class PdfExport implements ExportStrategy {

 @Override
 public void export() {
  //Pdf export Goes here
 }

}

What is Serialization?

Serialization is the process of encoding an object to byte stream and reverse of it is called deserialization.It is platform independent process which means you can serialize the object in one JVM and can transport the object over network and / or store it in filesystem and then deserialize it in other or on same JVM.

Class needs to implements marker interface Serializable in order to make their object to be eligible for Serialization.

fields with transient and/or static modifiers are not serialized by regular serialization process.

ObjectInputStream and ObjectOutputStream are high level stream classes which have methods to serialize and de-serialize Java objects.

ObjectOutputStream class has many write methods but the method that is usually used method is:

public final void writeObject(Object obj) throws IOException

ObjectInputStream class has many read methods but the method that is usually used method is:

public final Object readObject() throws IOException, ClassNotFoundException

Basic Serialization Example

Let's first define Dog pojo and then use ObjectInputStream and ObjectOutputStream for de-serialization and serialization.

public class Dog implements Serializable {
  private static final long serialVersionUID = 8661314562327474362L;

  private int height;
  private String name;

  // getters/ setters/ toString/ constructors
}

public class SimpleSerializationExample {
  public static void main(String[] args) {
    Dog dog = new Dog(50, "Titan"); // create dog object with height 50 and name Titan 
    System.out.println("Before Serialization");
    System.out.println(dog);
    try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("dog.ser"))) {
      oos.writeObject(dog);// serialize the dog object
    }
    catch (IOException ioEx) { /* Don't Swallow exception in real projects */ }
   
    dog = null; // let clear old dog object reference
    try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("dog.ser"))) {
      dog = (Dog) ois.readObject();// deserialize dog object
    }
    catch (IOException | ClassCastException | ClassNotFoundException ex) { /* Don't Swallow exception in real projects */ }

    System.out.println("After Serialization");
    System.out.println(dog);
  }
}
Point to remember, if the class is not implementing Serializable interface then writeObject() method of ObjectOutputStream will throw run-time exception java.io.NotSerializableException

The above class was pretty straightforward with two primitive instance members. But, in real world could there be a case when

case 1: super class is not serializable but sub-class is.

case 2: You class have composed object of another class which doesn't implements Serializable interface.

case 1: super class is not serializable but sub-class is

There could be situation when you are extending some class which is not serializable but you want the sub-class be serializable. It is only possible if your super-class has

  • default no-args constructor.
  • supports ways to initialize safely the shared fields.
If the super-class doesn't implements Serializable interface then the inherited fields in the sub-class will be initialized to their default values and the Constructor will be called from first instance of non-serializable class till all the super class constructors are run. But in case of serializable class, then constructor will never run at the time of deserialization.

case 2: You have composed of object of another class which doesn't support serialization

The only solution is to mark that field reference as transient and do custom serialization for that.

writeObject and readObject to rescue for case 1 and case 2

Java serialization have special mechanism just for this. It has a set of private methods you can implement in your serializable class which will be invoked automatically during serilization and deserilization. The signature of these methods are :

private void writeObject(ObjectOutputStream os) {
 //Your custom code for serilization goes here
}

private void readObject(ObjectInputStream is) {
 //Your custom code for deserilization goes here
}

These methods let you step into the middle of serialization and deserialization.

Let's move to example.

public class Collar {
  private int size;
  // getters/ setters/ toString
}

public class Dog implements Serializable {

  private static final long serialVersionUID = 6870143058315212650L;

  private int height;
  private String name;
  private transient Collar myCollar;
  
  //getters/ setters/ toString
  private void writeObject(ObjectOutputStream os) throws IOException {
    try {
      os.defaultWriteObject();// lets first have default serialization happens
      os.writeInt(myCollar.getSize()); // save custom values
    } catch (IOException ex) { }
  }

  private void readObject(ObjectInputStream is) throws IOException, ClassNotFoundException {
    try {
      is.defaultReadObject();// lets first have default deserialization happens
      myCollar = new Collar(is.readInt()); // custom read values
    } catch (IOException | ClassNotFoundException ex) {
    }
  }
}

public class SimpleSerializationExample {
  public static void main(String[] args) {
    Dog dog = new Dog(1, "BuBu", new Collar(5)); // create dog object with height 50 and name Titan 
    System.out.println("Before Serialization");
    System.out.println(dog);
    try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("dog_collar.ser"))) {
      oos.writeObject(dog);// serialize the dog object
    }
    catch (IOException ioEx) {
      /* Don't Swallow exception in real projects */
    }

    dog = null; // let clear old dog object reference
    try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("dog_collar.ser"))) {
      dog = (Dog) ois.readObject();// deserialize dog object
    }
    catch (IOException | ClassCastException | ClassNotFoundException e) {
      /* Don't Swallow exception in real projects */
    }
   
    System.out.println("After Serialization");
    System.out.println(dog.toString());
  }
}
Explanation
  • Line 15 and 22 declares writeObject and readObject methods in the Dog class.
  • Line 17 invoked defaultWriteObject() of ObjectOutputStream from writeObject(). We are telling JVM to perform the normal serialization. Line 18 did custom writing to the serialized object.
  • Line 24 invoked defaultReadObject() of ObjectInputStream from readObject() to handle normal serialization and then our custom deserialization starts at Line 25.
If we are customizing the serialization and deserialization using readObject() and writeObject() methods, then we need to read (readObject()) in order of writing(writeObject())  in the ObjectOutputStream because it saves in the sequence we write it. If we don't do so, we may end up in half-baked or wrongly deserialized object.

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 java.io.File;
import java.io.FileNotFoundException;
import java.util.List;

public interface CompressionStrategy {
     //Compression algorithm
     public void compressFiles(List<File> files) throws FileNotFoundException;
}

2. We will now create two concrete classes which define the compression Strategy. i.e. ZIP and RAR strategy to compress files.

package com.gauravbytes.strategy.client;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;
public class ZipCompressionStrategy implements CompressionStrategy {
   @Override
    public void compressFiles(List<File> files) throws FileNotFoundException {
     //using ZIP approach

    }
}

package com.gauravbytes.strategy.client;
import java.io.File;
import java.util.List;
public class RarCompressionStrategy implements CompressionStrategy {
     @Override
     public void compressFiles(List<File> files) {
             //using RAR approach
    }
}

3. We need on context/utility which will actually interact with the actual strategies to get work done.

package com.gauravbytes.strategy.client;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;

public class CompressionUtility {
     private CompressionStrategy strategy;
     
     //this can be set at runtime by the application preferences
     public void setCompressionStrategy(CompressionStrategy strategy) {
             this.strategy = strategy;
    } 

    public void createArchive(List<File> files) throws Exception, FileNotFoundException {
         if (files == null)
             throw new RuntimeException(new NullPointerException("ListFile is null"));

         strategy.compressFiles(files);
    }
}

4. Now, we require client which will use the context/utility and generate the compressed files
package strategy.client;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Client {
    public static void main(String[] args) {
          List<File> files = new ArrayList<File>();
          CompressionUtility fileCompressor = new CompressionUtility();
          boolean isZipTypeCompression = false;
          //we could assume context is already set by preferences
         CompressionStrategy strategy = isZipTypeCompression ? new ZipCompressionStrategy() : new RarCompressionStrategy();

        fileCompressor.setCompressionStrategy(strategy);
       //get a list of files
       try {
         fileCompressor.createArchive(files); 
       } 
       catch (Exception e) { }
   }
}

Important note

I would like to explain how composition is favoured in Strategy design pattern. In class CompressionUtility, we have reference to CompressionStrategy interface and in context/utility class, there is one setter method for compression strategy i.e. you can change the alogorithm at run-time in context class if required.

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 == null)
   { 
    synchronized (MUTEX) {
     if (INSTANCE == null)
      try {
       INSTANCE = new Singleton();
      } catch (IllegalAccessException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
    }
   }
  }
 }
 
 private Singleton() throws IllegalAccessException {
  if(SingletonInstanceHolder.MODCOUNT > 0)
   throw new IllegalAccessException("Multiple instance"); 

  SingletonInstanceHolder.MODCOUNT++;
 }
 
 public static final Singleton getInstance() {
  return SingletonInstanceHolder.INSTANCE;
 }
 
 // Avoiding duplicate objects with serialization
 public Object readResolve() {
  return getInstance();
 }
}

The above mentioned class Singleton. This class can't have multiple objects even using reflection api because our code will throw exception if more than one instance is created with reflection.

package singleton;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class SingletonTest {
  public static void main(String[] args) throws ClassNotFoundException, SecurityException, NoSuchMethodException, InstantiationException,
     IllegalAccessException, IllegalArgumentException,InvocationTargetException {
 
  Class demo = Class.forName("singleton.Singleton");
  Constructor con = demo.getDeclaredConstructor();
  con.setAccessible(true);
  
  for(int i=0; i<10; i++) {
   Singleton s1 = (Singleton) con.newInstance();
   System.out.println(s1.toString());
  }
 }
}

The above code when run will throw following exception. It shows that the Singleton class's object will be created only once and then it will throw exception if tried to called constructor multiple times even with reflection.

Exception in thread "main" java.lang.reflect.InvocationTargetException
 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
 at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
 at singleton.SingletonTest.main(SingletonTest.java:15)
Caused by: java.lang.IllegalAccessException: Multiple instance
 at singleton.Singleton.<init>(Singleton.java:29)
 ... 5 more