Skip to main content

Posts

Showing posts with the label Creational Pattern

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...

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. Foll...

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...

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 {   privat...

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. <Name> and if the sex is Female (F), it displays message saying Hello Ms <Name>. 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 ...