You are on page 1of 13

Creation patterns

Factory pattern
In Factory pattern, we create object without exposing the creation logic to the client and refer to
newly created object using a common interface.

Interface

OS:class

public interface OS {

void spec();
}

Classes implementing interface

IOS.class

public class IOS implements OS {

@Override
public void spec() {
System.out.println("Good, but closed");
}

Windows.class

public class Windows implements OS {

@Override
public void spec() {
System.out.println("Not so good");
}

}
Linux.class

public class Linux implements OS {

@Override
public void spec() {
System.out.println("The BEST");
}

Objects factory

public class OSFactory {


public OS getInstance(String str){

if(str.equals("closed")){
return new IOS();
} else if (str.equals("good")){
return new Linux();
} else {
return new Windows();
}
}
}

Main

public class FactoryMain {


public static void main(String[] args) {
OS obj = new IOS();
obj.spec();
///
OS obj2;
OSFactory osFactory = new OSFactory();
obj2 = osFactory.getInstance("good");
obj2.spec();
}
}
Benefits: the creation of the objects of type OS is abstracted. If we want to change the way in
which the objects are created we need to change just the factory class.
Abstract Factory pattern
It’s a factory of factories

public enum FactoryType {


LAPTOP,
MOBILE
}

Factory Generator

public class FactoryGenerator {

public static OSAbstractFactory getFactory(FactoryType


factoryType){
switch(factoryType){
case LAPTOP:
return new LaptopFactory();
case MOBILE:
return new MobileFactory();
}
return null;
}
}

OSAbstractFactory.class

public abstract class OSAbstractFactory {


abstract Device getDeviceInfo(DeviceType deviceType);
}

LaptopFactory.class

public class LaptopFactory extends OSAbstractFactory {

@Override
Device getDeviceInfo(DeviceType deviceType) {

switch(deviceType){
case HP:
return new HP();
case DELL:
return new Dell();
case ASUS:
return new Asus();
}

return null;
}

MobileFactory.class

public class MobileFactory extends OSAbstractFactory {

@Override
Device getDeviceInfo(DeviceType deviceType) {
switch(deviceType){
case ANDROID:
return new Android();
case DELL:
return new Dell();
case NOKIA:
return new Nokia();
}
return null;
}
}

DeviceType.class

public enum DeviceType {

DELL,
HP,
NOKIA,
ASUS,
ANDROID
}

Device.class

public abstract class Device {

public abstract String getDetails();

Dell.class

public class Dell extends Device {

@Override
public String getDetails() {
String str = "Dell";
System.out.println(str);
return str;
}
}

Singleton

In object-oriented programming, a singleton class is a class that can have only one object (an
instance of the class) at a time.

public class Singleton


{
// static variable single_instance of type Singleton
private static Singleton single_instance = null;

// variable of type String


public String s;
// private constructor restricted to this class itself
private Singleton()
{
s = "Hello I am a string part of Singleton class";
}

// static method to create instance of Singleton class


public static Singleton getInstance()
{
if (single_instance == null)
single_instance = new Singleton();

return single_instance;
}
}

Builder
Builder pattern is used to create instance of very complex object having telescoping
constructor in easiest way. Constructors in Java are used to create object and can take
parameters required to create object. Lets see an example and learn how to implement
builder pattern.

RobotPlan.class

public interface RobotPlan {

void setRobotHead(String head);


void setRobotTorso(String torso);
void setRobotArms(String arms);
void setRobotLegs(String legs);
}

Robot.class

public class Robot implements RobotPlan {


private String robotHead;
private String robotTorso;
private String robotArms;
private String robotLegs;

@Override
public void setRobotHead(String head) {

robotHead = head;
}

@Override
public void setRobotTorso(String torso) {
robotTorso = torso;
}

@Override
public void setRobotArms(String arms) {
robotArms = arms;
}

@Override
public void setRobotLegs(String legs) {
robotLegs = legs;

public String getRobotHead() {


return robotHead;
}

public String getRobotTorso() {


return robotTorso;
}

public String getRobotArms() {


return robotArms;
}
public String getRobotLegs() {
return robotLegs;
}
}

RobotBuilder.class

public interface RobotBuilder {

void buildRobotHead();
void buildRobotTorso();
void buildRobotArms();
void buildRobotLegs();
Robot getRobot();
}

RobotBuilderImpl.class

public class RobotBuilderImpl implements RobotBuilder {

private Robot robot;

public RobotBuilderImpl() {
this.robot = new Robot();
}

@Override
public void buildRobotHead() {
robot.setRobotHead("Head");
}

@Override
public void buildRobotTorso() {
robot.setRobotTorso("Torso");
}

@Override
public void buildRobotArms() {
robot.setRobotArms("Arms");
}
@Override
public void buildRobotLegs() {
robot.setRobotLegs("Legs");
}

@Override
public Robot getRobot() {

return this.robot;
}
}

RobotEngineer.class

public class RobotEngineer {

private RobotBuilder robotBuilder;

public RobotEngineer (RobotBuilder robotBuilder){


this.robotBuilder = robotBuilder;
}

public Robot getRobot(){


return this.robotBuilder.getRobot();
}

public void makeBot(){


this.robotBuilder.buildRobotHead();
this.robotBuilder.buildRobotTorso();
this.robotBuilder.buildRobotArms();
this.robotBuilder.buildRobotLegs();

BuilderMain.class
public class BuilderMain {
public static void main(String[] args) {
RobotBuilder builder = new RobotBuilderImpl();
RobotEngineer engineer = new RobotEngineer(builder);
engineer.makeBot();

Robot firstRobot = engineer.getRobot();


}

Prototype

Prototype is a creational design pattern that allows cloning objects, even complex ones,
without coupling to their specific classes.

Book.class

public class Book {

private int id;


private String name;

public int getId() {


return id;
}

public void setId(int id) {


this.id = id;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

@Override
public String toString() {
return "Book{" + "id=" + id + ", name=" + name + '}';
}
}

BookShop.class

public class BookShop implements Cloneable {

private String shopName;


List<Book> books = new ArrayList();

public String getShopName() {


return shopName;
}

public void setShopName(String shopName) {


this.shopName = shopName;
}

public List<Book> getBooks() {


return books;
}

public void setBooks(List<Book> books) {


this.books = books;
}

public void loadData(){


for (int i = 0; i < 10; i++) {
Book b = new Book();
b.setId(i);
b.setName("Book"+ i);
getBooks().add(b);

}
}
@Override
public String toString() {
return "BookShop{" + "shopName=" + shopName + ",
books=" + books + '}';
}

@Override //DEEP CLONING


protected BookShop clone() throws
CloneNotSupportedException {
BookShop bs = new BookShop();
for(Book b : getBooks()){
bs.getBooks().add(b);
}

return bs;
}

MainPrototype.class

public class MainPrototype {

public static void main(String[] args) {


BookShop bs = new BookShop();
bs.setShopName("Shop 1");
bs.loadData();
System.out.println(bs);

BookShop bs1 = null;


try {
bs1 = bs.clone();
} catch (CloneNotSupportedException ex) {

Logger.getLogger(MainPrototype.class.getName()).log(Level.SEVE
RE, null, ex);
}
bs1.setShopName("Shop 2");
System.out.println(bs1);
System.out.println(bs == bs1);
}

Structure patterns

Adapter
Convert the interface of a class into another interface clients expect. Adapter lets classes
work together that couldn't otherwise because of incompatible interfaces. Wrap an existing
class with a new interface. Impedance match an old component to a new system

You might also like