Java Patterns
Java Patterns
Creational Patterns
The Factory Method provides a simple decision making class that
returns one of several possible subclasses of an abstract base class depending
on the data that are provided.
public FirstFirst(String s) {
//find sep space
int i = s.lastIndexOf(" ");
if (i > 0) {
//left is first name
first = s.substring(0, i).trim();
//right is last name
last =s.substring(i+1).trim();
} else {
// put all in last name
first = “”;
// if no space
last = s;
}
}
}
public LastFirst(String s) {
//find comma
int i = s.indexOf(",");
if (i > 0) {
//left is last name
last = s.substring(0, i).trim();
//right is first name
first = s.substring(i + 1).trim();
} else {
last = s; // put all in last name
first = ""; // if no comma
}
}
}
public Butterfly() {
}
class Cocoon {
public Butterfly getButterfly(float y) {
if (y !=0) {
//get multiply class
return new trigButterfly(y);
} else {
//get add/sub class
return new addButterfly(y);
}
}
}
· When a class does not know which class of objects it must create.
· A class specifies its sub-classes to specify which objects to create.
· 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.
a) The different parts of computer are, say Monitor, RAM and Processor. The different types of
computers are PC, Workstation and Server.
package creational.abstractfactory;
/**
* Abstract method, returns the Parts ideal for
* PC
* @return Parts
*/
public abstract Parts getMonitor();
}// End of class
package creational.abstractfactory;
this.specification = specification;
}
/**
* Returns the name of the part of Computer
*
* @return specification of Part of Computer, String
*/
public String getSpecification() {
return specification;
}
/**
* 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;
/**
* 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;
/**
* 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
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
import java.util.*;
class WorkShop {
//force the order of building process
public void construct(HouseBuilder hb) {
hb.buildFoundation();
hb.buildFrame();
hb.buildExterior();
hb.buildInterior();
}
}
class House {
private String type = null;
private List features = new ArrayList();
public House() {
class TestBuilder {
b)
package creational.builder;
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
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
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; }
/**
* Main builder class which builds the entire meal
* for the customers
*/
public class MealBuilder {
Want to decouple the process of building a complex object from the parts that make up the
object.
o Abstract Factory, which focuses on the layer over the factory pattern (may be simple
or complex), whereas a builder pattern focuses on building a complex object from
other simple objects.
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.