TL;DR — The prototype pattern is one of the creational types of design patterns. It is used when we want to create a copy of another object without going into the specifics of how that object is created.

Let’s say that object where there are fields of different object types. And to create one you have to initialize all that fields to create this object.

public class User{String first_name;String last_name;Account acc_no;Company company;Order order;
}

Here you can see to create a single user object you have to create the Account, Company, and Order object first.

Well for the first time that User object has to be created by going on creating all those other objects.

Account acc = new Account();Company company = new Company();Order order = new Order();User user1 = new User("Clark","Kent",acc_no ,company,order);

Now if we want to have another instance of the same User class with the same data.

We can go on and create a new instance and then set every field of the new object with the value of the old one.

Or

We could just use a method to do all those things for us like:

User user2 = user1.getClone();

Prototype pattern helps us to create clones of objects and avoid the expensive cost of creating them from the ground up.

Implementation:

public class EmployeePrototypePattern implements Cloneable {

private String first_name; //required field
private String last_name; //required field
private String middle_name; //optional_field
private String favourite_hobby; //optional field
private String martial_status; //optional field
private Address address;

EmployeePrototypePattern(String first_name , String last_name , String middle_name , String favourite_hobby , String martial_status , Address address){
this.first_name = first_name;
this.last_name = last_name;
this.middle_name = middle_name;
this.favourite_hobby = favourite_hobby;
this.martial_status = martial_status;
this.address = address;
}

public Address getAddress() {
return address;
}

public void setAddress(Address address) {
this.address = address;
}

@Override
public String toString() {
return "EmployeePrototypePattern{" +
"first_name='" + first_name + '\'' +
", last_name='" + last_name + '\'' +
", middle_name='" + middle_name + '\'' +
", favourite_hobby='" + favourite_hobby + '\'' +
", martial_status='" + martial_status + '\'' +
", address=" + address.getCity() +
'}';
}

public void setFirst_name(String first_name) {
this.first_name = first_name;
}

public void setLast_name(String last_name) {
this.last_name = last_name;
}

public void setMiddle_name(String middle_name) {
this.middle_name = middle_name;
}

public void setFavourite_hobby(String favourite_hobby) {
this.favourite_hobby = favourite_hobby;
}

public void setMartial_status(String martial_status) {
this.martial_status = martial_status;
}

@Override
public Object clone() throws CloneNotSupportedException{
return super.clone();
}
}

Here we have used the Cloneable interface to mark our class. The clone method is overridden, which will result in creating a shallow copy of the object.

Usage:

Address address = new Address("Toronto");
EmployeePrototypePattern emp = new EmployeePrototypePattern("john", "cena", "thomas", "tennis", "single", address);

System.out.println(" emp 1:" + emp.toString());

try {
EmployeePrototypePattern emp2 = (EmployeePrototypePattern) emp.clone();

emp2.getAddress().setCity("Monza");
System.out.println(" emp 2:" +emp2.toString());
System.out.println(" emp 1:" +emp.toString());

} catch (CloneNotSupportedException e) {
e.printStackTrace();
}

Result:

emp 1:{first_name='john',...,address=Toronto}
emp 2:{first_name='john',..., address=Monza}
emp 1:{first_name='john',..., address=Monza}

Here as you can see in the result the first employee was set in Toronto

After cloning, we set the city to Monza for the second employee.

When printing the result of the first employee again after changing the city of the second one we see both the employees are now in Monza.

Because of Shallow copying. This means both instances are referencing the same object of Address.

For this, we need to change the implementation of our cloning method to:

@Override
public Object clone() throws CloneNotSupportedException{
EmployeePrototypePattern emp = new EmployeePrototypePattern (first_name , last_name , middle_name , favourite_hobby , martial_status , address);

emp.setAddress(new Address(address.getCity()));
return emp;
}

Now the results are:

EmployeePrototypePattern{first_name='john',...,address=Toronto}
EmployeePrototypePattern{first_name='john',..., address=Monza}
EmployeePrototypePattern{first_name='john',..., address=Toronto}

For more: www.buffernest.com

--

--

Rana M. Suleman

Business oriented technology enthusiast, sharing ideas from the idea valley inside my immensely curious mind, A boring Software developer and a Gaming Nerd