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.
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() { |
||
}// 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) { |
||
}// 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();
/**
/** |
||
}// 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;
/**
/** |
||
}// 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"); }
/**
/** |
||
}// 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"); }
/**
/** |
||
}// 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"); }
/**
/** |
||
}// 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;
/** |
||
private Computer comp;
public static void main(String[] args) { |
||
ComputerType type = new ComputerType();
Computer computer = type.getComputer("Server"); |
||
} | ||
/** |
||
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; public class Logger { |
||
private String fileName; private Properties properties; private Priority priority;
/**
/**
/**
// singleton - pattern |
||
}// 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();
/**
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(); }
/** |
||
}// 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;
/** |
||
/** * 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(); }
/** |
||
}// 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;
/** |
||
public Packing additems() { Item[] items = {new VegBurger(), new Fries(), new Cola(), new Doll()}
return new MealBox().addItems(items);
public int calculatePrice() {
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;
/** |
|||
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 |
||
}// 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.