Design Pattern – Prototype Pattern – Part VI

In this post, I want to show how to use Prototype design pattern. If you want to read about previous posts related to design patterns, series of posts about design patterns are

  1. Introduction to design patterns
  2. Singleton Pattern
  3. Factory Pattern
  4. Abstract Factory Pattern
  5. Builder Pattern

The prototype design pattern will cover the creation design pattern that we have been writing about till now.

When to use?

Since this is a creational design pattern, this is used when decision is to reduce the creation cost of object in a standard way. There can be argument about how this is then different from abstract factory pattern. The key benefit of Prototype design pattern is that it optimizes the use case where multiple objects of same type will have mostly same data.

Major example is reading configuration data from a file/database over a network. Also if you want to hide the logic of creating new instance from the client.

How to use?

Firstly, in this pattern, there is an interface of Prototype that has method for clone and any concrete class implementing this interface, implements the method to clone the object.

 

public interface Car {

Car clone();

}

We have an interface Car which we will implement in our concrete classes.

package com.bettterjavacode.designpatterns.prototypeexample;

public class Toyota implements Car 
{

    private final String CARNAME = "Toyota";

    public Car clone() 
    {
       return new Toyota();
    }

    @Override
    public String toString() 
    {
      return CARNAME;
    }

}

 

We will have a factory class that will get us a prototype based on type of object we have passed. This will look like below:

package com.bettterjavacode.designpatterns.prototypeexample;

import java.util.HashMap;
import java.util.Map;

public class CarFactory
{

   private static final Map<String, Car> prototypes = new HashMap<String, Car>();

   static 
   {
     prototypes.put("toyota", new Toyota());
     prototypes.put("lexus", new Lexus());
     prototypes.put("bmw", new BMW());
   }

   public static Car getPrototype(String type) 
   {
      return prototypes.get(type).clone();
   }
}

 

Therefore, our demo class will pass the type of car as an argument to print the carname. That will look like below:

package com.betterjavacode.designpatterns;

import com.bettterjavacode.designpatterns.prototypeexample.Car;
import com.bettterjavacode.designpatterns.prototypeexample.CarFactory;

public class PrototypeDemo 
{
   public static void main(String[] args) 
   {
      if (args.length > 0) 
      {
         for (String type : args) 
         {
            Car prototype = CarFactory.getPrototype(type);
            if (prototype != null) 
            {
               System.out.println(prototype);
            }
         }
      } 
      else 
      {
         System.out.println(" Run again with arguments");
      }
   }
}

Conclusion

In conclusion, we showed how to use prototype design pattern. The code for this is available here

References

  1. Design Patterns – Prototype
  2. Prototype Pattern