Creational Patterns

 


Creational Patterns - Factory Pattern

Factory of what? Of classes. In simple words, if we have a super class and n sub-classes, and based on data provided, we have to return the object of one of the sub-classes, we use a factory pattern.

Let’s take an example to understand this pattern.

Example: Let’s suppose an application asks for entering the name and sex of a person. If the sex is Male (M), it displays welcome message saying Hello Mr. and if the sex is Female (F), it displays message saying Hello Ms .

The skeleton of the code can be given here.

public class Person {
  // name string
public String name;
// gender : M or F
private String gender;

public String getName() {
return name;
}

public String getGender() {
return gender;
}

}// End of class

 

This is a simple class Person having methods for name and gender. Now, we will have two sub-classes, Male and Female which will print the welcome message on the screen.

public class Male extends Person {
  public Male(String fullName) {
System.out.println("Hello Mr. "+fullName);
}
}// End of class

Also, the class Female

public class Female extends Person {
  public Female(String fullNname) {
System.out.println("Hello Ms. "+fullNname);
}
}// End of class

Now, we have to create a client, or a SalutationFactory which will return the welcome message depending on the data provided.

public class SalutationFactory {
  public static void main(String args[]) {
SalutationFactory factory = new SalutationFactory();
factory.getPerson(args[0], args[1]);
}

public Person getPerson(String name, String gender) {
if (gender.equals("M"))
return new Male(name);
else if(gender.equals("F"))
return new Female(name);
else
return null;
}

}// End of class

This class accepts two arguments from the system at runtime and prints the names.

Running the program:

After compiling and running the code on my computer with the arguments Prashant and M:

java Prashant M

The result returned is: “Hello Mr. Prashant”.

When to use a Factory Pattern?
The Factory patterns can be used in following cases:
1. When a class does not know which class of objects it must create.
2. A class specifies its sub-classes to specify which objects to create.
3. In programmer’s language (very raw form), you can use factory pattern where you have to create an object of any one of sub-classes depending on the data provided.

Creational Patterns - Abstract Factory Pattern

This pattern is one level of abstraction higher than factory pattern. This means that the abstract factory returns the factory of classes. Like Factory pattern returned one of the several sub-classes, this returns such factory which later will return one of the sub-classes.

Let’s understand this pattern with the help of an example.

Suppose we need to get the specification of various parts of a computer based on which work the computer will be used for.

The different parts of computer are, say Monitor, RAM and Processor. The different types of computers are PC, Workstation and Server. 

So, here we have an abstract base class Computer.

package creational.abstractfactory;

public abstract class Computer {

  /**
* Abstract method, returns the Parts ideal for
* Server
* @return Parts
*/
public abstract Parts getRAM();

/**
* Abstract method, returns the Parts ideal for
* Workstation
* @return Parts
*/
public abstract Parts getProcessor();

/**
* Abstract method, returns the Parts ideal for
* PC
* @return Parts
*/
public abstract Parts getMonitor();

}// End of class

This class, as you can see, has three methods all returning different parts of computer. They all return a method called Parts. The specification of Parts will be different for different types of computers. Let’s have a look at the class Parts.

package creational.abstractfactory;

public class Parts {

  /**
* specification of Part of Computer, String
*/
public String specification;

/**
* Constructor sets the name of OS
* @param specification of Part of Computer
*/
public Parts(String specification) {
this.specification = specification;
}

/**
* Returns the name of the part of Computer
*
* @return specification of Part of Computer, String
*/
public String getSpecification() {
return specification;
}

 

}// End of class

And now lets go to the sub-classes of Computer. They are PC, Workstation and Server.

package creational.abstractfactory;

public class PC extends Computer {

  /**
* Method over-ridden from Computer, returns the Parts ideal for
* Server
* @return Parts
*/
public Parts getRAM() {
return new Parts("512 MB");
}

/**
* Method over-ridden from Computer, returns the Parts ideal for
* Workstation
* @return Parts
*/
public Parts getProcessor() {
return new Parts("Celeron");
}

/**
* Method over-ridden from Computer, returns the Parts ideal for
* PC
* @return Parts
*/
public Parts getMonitor() {
return new Parts("15 inches");
}

}// End of class

package creational.abstractfactory;

public class Workstation extends Computer {
  /**
* Method over-ridden from Computer, returns the Parts ideal for
* Server
* @return Parts
*/
public Parts getRAM() {
return new Parts("1 GB");
}

/**
* Method over-ridden from Computer, returns the Parts ideal for
* Workstation
* @return Parts
*/
public Parts getProcessor() {
return new Parts("Intel P 3");
}

/**
* Method over-ridden from Computer, returns the Parts ideal for
* PC
* @return Parts
*/
public Parts getMonitor() {
return new Parts("19 inches");
}

}// End of class

package creational.abstractfactory;

public class Server extends Computer{

  /**
* Method over-ridden from Computer, returns the Parts ideal for
* Server
* @return Parts
*/
public Parts getRAM() {
return new Parts("4 GB");
}

/**
* Method over-ridden from Computer, returns the Parts ideal for
* Workstation
* @return Parts
*/
public Parts getProcessor() {
return new Parts("Intel P 4");
}

/**
* Method over-ridden from Computer, returns the Parts ideal for
* PC
* @return Parts
*/
public Parts getMonitor() {
return new Parts("17 inches");
}

}// End of class

Now let’s have a look at the Abstract factory which returns a factory “Computer”. We call the class ComputerType.

package creational.abstractfactory;

/**
* This is the computer abstract factory which returns one
* of the three types of computers.
*
*/
public class ComputerType {

  private Computer comp;

public static void main(String[] args) {

    ComputerType type = new ComputerType();

Computer computer = type.getComputer("Server");
System.out.println("Monitor: "+computer.getMonitor().getSpecification());
System.out.println("RAM: "+computer.getRAM().getSpecification());
System.out.println("Processor: "+computer.getProcessor().getSpecification());

  }   
   

/**
* Returns a computer for a type
*
* @param computerType String, PC / Workstation / Server
* @return Computer
*/

  public Computer getComputer(String computerType) {
    if (computerType.equals("PC"))
comp = new PC();
else if(computerType.equals("Workstation"))
comp = new Workstation();
else if(computerType.equals("Server"))
comp = new Server();

return comp;

  }  
}// End of class

Running this class gives the output as this:

Monitor: 17 inches
RAM: 4 GB
Processor: Intel P 4.

When to use Abstract Factory Pattern?
One of the main advantages of Abstract Factory Pattern is that it isolates the concrete classes that are generated. The names of actual implementing classes are not needed to be known at the client side. Because of the isolation, you can change the implementation from one factory to another.

 

Creational Patterns - Singleton Pattern

This is one of the most commonly used patterns. There are some instances in the application where we have to use just one instance of a particular class. Let’s take up an example to understand this.

A very simple example is say Logger, suppose we need to implement the logger and log it to some file according to date time. In this case, we cannot have more than one instances of Logger in the application otherwise the file in which we need to log will be created with every instance.

We use Singleton pattern for this and instantiate the logger when the first request hits or when the server is started.

package creational.singleton;

import org.apache.log4j.Priority;

import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.Properties;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.IOException;

public class Logger {

  private String fileName;
private Properties properties;
private Priority priority;

/**
* Private constructor
*/
private Logger() {
logger = this;
}

/**
* Level of logging, error or information etc
*
* @return level, int
*/
public int getRegisteredLevel() {
int i = 0;
try {
InputStream inputstream = getClass().getResourceAsStream("Logger.properties");
properties.load(inputstream);
inputstream.close();
i = Integer.parseInt(properties.getProperty("**logger.registeredlevel**"));
if(i < 0 || i > 3)
i = 0;
}
catch(Exception exception) {
System.out.println("Logger: Failed in the getRegisteredLevel method");
exception.printStackTrace();
}
return i;
}
/**
* One file will be made daily. So, putting date time in file
* name.
*
* @param gc GregorianCalendar
* @return String, name of file
*/
private String getFileName(GregorianCalendar gc) {
SimpleDateFormat dateFormat1 = new SimpleDateFormat("dd-MMM-yyyy");
String dateString = dateFormat1.format(gc.getTime());
String fileName = "C:\\prashant\\patterns\\log\\PatternsExceptionLog-" + dateString + ".txt";
return fileName;
}

/**
* A mechanism to log message to the file.
*
* @param p Priority
* @param message String
*/
public void logMsg(Priority p, String message) {
try {
GregorianCalendar gc = new GregorianCalendar();
String fileName = getFileName(gc);
FileOutputStream fos = new FileOutputStream(fileName, true);
PrintStream ps = new PrintStream(fos);
SimpleDateFormat dateFormat2 = new SimpleDateFormat("EEE, MMM d, yyyy 'at' hh:mm:ss a");
ps.println("<"+dateFormat2.format(gc.getTime())+">["+message+"]");
ps.close();
}
catch (IOException ie) {
ie.printStackTrace();
}
}
/**
* this method initialises the logger, creates an object
*/
public static void initialize() {
logger = new Logger();
}

// singleton - pattern
private static Logger logger;
public static Logger getLogger() {
return logger;
}

}// End of class

Difference between static class and static method approaches:
One question which a lot of people have been asking me. What’s the difference between a singleton class and a static class? The answer is static class is one approach to make a class “Singleton”.

We can create a class and declare it as “final” with all the methods “static”. In this case, you can’t create any instance of class and can call the static methods directly.

Example:

final class Logger {
//a static class implementation of Singleton pattern
static public void logMessage(String s) {
System.out.println(s);
}
}// End of class

//==============================
public class StaticLogger {
public static void main(String args[]) {
Logger.logMessage("This is SINGLETON");
}
}// End of class

The advantage of this static approach is that it’s easier to use. The disadvantage of course is that if in future you do not want the class to be static anymore, you will have to do a lot of recoding.

 

Creational Patterns - Builder Pattern

Builder, as the name suggests builds complex objects from simple ones step-by-step. It separates the construction of complex objects from their representation.

Let’s take a non-software example for this. Say, we have to plan for a children meal at a fast food restaurant. What is it comprised of? Well, a burger, a cold drink, a medium fries and a toy.

This is common to all the fast food restaurants and all the children meals. Here, what is important? Every time a children’s meal is ordered, the service boy will take a burger, a fries, a cold drink and a toy. Now suppose, there are 3 types of burgers available. Vegetable, Fish and Chicken, 2 types of cold drinks available. Cola and Orange and 2 types of toys available, a car and a doll.

So, the order might be a combination of one of these, but the process will be the same. One burger, one cold drink, one fries and one toy. All these items are placed in a paper bag and is given to the customer.

Now let’s see how we can apply software to this above mentioned example.

The whole thing is called a children meal. Each of the four burger, cold drink, fries and toy are items in the meal. So, we can say, there is an interface Item having two methods, pack() and price().

Let’s take a closer look at the interface Item:

Item.java

package creational.builder;

public interface Item {

  /**
* pack is the method, as every item will be packed
* in a different way.
* E.g.:- The burger will be packed as wrapped in a paper
* The cold drink will be given in a glass
* The medium fries will be packed in a card box and
* The toy will be put in the bag straight.
* The class Packing is an interface for different types of
* for different Items.
*/

public Packing pack();

/**
* price is the method as all the items
* burger, cold drink, fries will have a price.
* The toy will not have any direct price, it will
* be given free with the meal.
*
* The total price of the meal will be the combined
* price of the three items.
*
* @return price, int in rupees.
*/

public int price();
 

}// End of class

So, we must now have a class defined for each of the items, as burger, toy, cold drink and fries. All these will implement the Item interface.

Lets start with Burger:

Burger.java

package creational.builder;
/**
* The class remains abstract as price method will be implemented
* according to type of burger.
* @see price()

*/
public abstract class Burger implements Item {
  /**
* A burger is packed in a wrapper. Its wrapped
* in the paper and is served. The class Wrapper is 
* sub-class of Packing interface.
* @return new Wrapper for every burger served.
*/
public Packing pack() {
return new Wrapper();
}

/**
* This method remains abstract and cannot be
* given an implementation as the real implementation
* will lie with the type of burger.
*
* E.g.:- Veg Burger will have a different price from
* a fish burger.
*
* @return price, int.
*/
public abstract int price();

}// End of class

The class Burger can be further extended to VegBurger, FishBurger, ChickenBurger etc. These classes will each implement the price() method and return a price for each type of burger. I, in this example have given the implementation for VegBurger class. 

VegBurger.java


package creational.builder;

/**
* The implementation of price method.
*/
public class VegBurger extends Burger {
  /**
* This is the method implementation from
* the super class Burger.
*
* @return price of a veg burger in rupees.
*/
public int price() {
return 39;
}
}// End of class

Let’s concentrate on other items now. I, here for explanation purpose will give another item Fries.

Fries.java 


package creational.builder;

/**
* Implements the Item interface.
*/
public class Fries implements Item {

  /**
* Packing in which fries are served.
*
* @return new Packing for every fries.
* Envelop is a packing in which fries are given
*/
public Packing pack() {
return new Envelop();
}

/**
* Price of the medium fries.
*
* @return int , price of medium fries in rupees
*/
public int price() {
return 25;
}

}// End of class

Now, let’s see the Builder class, MealBuilder. This class is the one which serves the Children’s meal.

MealBuilder.java

package creational.builder;

/**
* Main builder class which builds the entire meal
* for the customers
*/
public class MealBuilder {

  public Packing additems() {
Item[] items = {new VegBurger(), new Fries(), new Cola(), new Doll()}

return new MealBox().addItems(items);
}

public int calculatePrice() {
int totalPrice = new VegBurger().price() + new Cola().price() + new Fries().price() + new Doll().price();

return totalPrice;
}

}// End of class

This class gives the total meal and also presents the total price. Here, we have abstracted the price calculation and meal package building activity from the presentation, which is a meal box. The Builder pattern hides the internal details of how the product is built.

Each builder is independent of others. This improves modularity and makes the building of other builders easy.

Because, each builder builds the final product step by step, we have more control on the final product. 

 

 

Creational Patterns - Prototype Pattern

The prototype means making a clone. This implies cloning of an object to avoid creation. If the cost of creating a new object is large and creation is resource intensive, we clone the object. We use the interface Cloneable and call its method clone() to clone the object.

Again let’s try and understand this with the help of a non-software example. I am stressing on general examples throughout this write-up because I find them easy to understand and easy to accept as we all read and see them in day-to-day activity. The example here we will take will be of a plant cell. This example is a bit different from the actual cloning in a way that cloning involves making a copy of the original one. Here, we break the cell in two and make two copies and the original one does not exist. But, this example will serve the purpose. Let’s say the Mitotic Cell Division in plant cells.
Let’s take a class PlantCell having a method split(). The plant cell will implement the interface Cloneable.

Following is the sample code for class PlantCell.

PlantCell.java

package creational.builder;

/**
* Shows the Mitotic cell division in the plant cell.
*/
public class PlantCell implements Cloneable {

  public Object split() {
    try {
      return super.clone();
}catch(Exception e) {
System.out.println("Exception occured: "+e.getMessage());
return null;
}
    }
}// End of class

Now let’s see, how this split method works for PlantCell class. We will make another class CellDivision and access this method.

CellDivision.java

package creational.prototype;
/**
* Shows how to use the clone.
*/
public class CellDivision {
  public static void main(String[] args) {
PlantCell cell = new PlantCell();

// create a clone
PlantCell newPlantCell = (PlantCell)cell.split();
}

}// End of class

One thing is you cannot use the clone as it is. You need to instantiate the clone before using it. This can be a performance drawback. This also gives sufficient access to the data and methods of the class. This means the data access methods have to be added to the prototype once it has been cloned.

 


Contact

ShareYourKnowledge