"If you want to construct multiple instances of an expensive object, just create one object and clone it multiple times."
✔ Create one instance of expensive object
✔ Clone it when you need another instance of that type
✔ Modify the properties of new(cloned) instance as required.
☞ Note that this pattern suits only if those instances should have nearly similar properties.
☞ It is not a must to use Object.clone() method to duplicate the object. You can write your own copy method to do this. To keep this lesson short in my Car class I implements the Cloneable interface and use Object.clone() method in CarCreator class.
Assume that you have a class called Car which can be used to build various types of cars. When using Prototype Pattern what you do is create a car initially and duplicate it when you need more cars.
See this example. This is the Car class.
public class Car implements Cloneable {
int doorCount = 0;
public Car() {
// complex construction logic here
}
public int getDoorCount() {
return doorCount;
}
public void setDoorCount(int doorCount) {
this.doorCount = doorCount;
}
protected Object clone() {
try {
return super.clone();
} catch (Exception e) {
return null;
}
}
//..more lines here...
}
int doorCount = 0;
public Car() {
// complex construction logic here
}
public int getDoorCount() {
return doorCount;
}
public void setDoorCount(int doorCount) {
this.doorCount = doorCount;
}
protected Object clone() {
try {
return super.clone();
} catch (Exception e) {
return null;
}
}
//..more lines here...
}
Ok. Now you should have another class(CarCreator) which is used when you need to create a new Car instance. At the very first time you call this class it goes through the expensive logic and creates an instance of Car object. As well this class keeps that instance for later use. When you request a new car instance at later time it clones the previously saved object and returns.
This is your CarCreator class.
public class CarCreator {
private Car car;
public Car createCar() {
if(car == null) {
car = new Car();//This construction is expensive
return car;
} else {
//since we already have a car in hand we return a clone of that
return (Car) car.clone();
}
}
}
private Car car;
public Car createCar() {
if(car == null) {
car = new Car();//This construction is expensive
return car;
} else {
//since we already have a car in hand we return a clone of that
return (Car) car.clone();
}
}
}
Your client class to check above functionality would be as below.
public class Test implements Cloneable {
CarCreator carCreator = new CarCreator();
public static void main(String[] args) {
Test test = new Test();
//create a luxury car
Car luxuryCar = test.createVehicle(4);
//create a sports car
Car sportsCar = test.createVehicle(2);
System.out.println(luxuryCar.getDoorCount());
System.out.println(sportsCar.getDoorCount());
}
private Car createVehicle(int doorCount) {
Car vehicle = carCreator.createCar();
vehicle.setDoorCount(doorCount);
return vehicle;
}
}
CarCreator carCreator = new CarCreator();
public static void main(String[] args) {
Test test = new Test();
//create a luxury car
Car luxuryCar = test.createVehicle(4);
//create a sports car
Car sportsCar = test.createVehicle(2);
System.out.println(luxuryCar.getDoorCount());
System.out.println(sportsCar.getDoorCount());
}
private Car createVehicle(int doorCount) {
Car vehicle = carCreator.createCar();
vehicle.setDoorCount(doorCount);
return vehicle;
}
}
No comments:
Post a Comment