October 23, 2020

Implement Prototype Design Pattern Using Java Base libraries features.

Java provides inbuilt features to implement Prototype design pattern.

But, before using that I will show you how you can create a prototype without any help from any libraries.

So, we have a Person class.

public class Person {
    private String firstName;
    private String lastName;
    private String country;

    public Person(){}
    public Person(final String firstName, final String lastName, final String country){
        this.firstName = firstName;
        this.lastName  = lastName;
        this.country   = country;
    }

    public void setFirstName(final String firstName){ this.firstName = firstName; } 
    public void setLastName(final String lastName){ this.lastName    = lastName; } 
    public void setCountry(final String country){ this.country       = country; } 

    public String getFirstName() { return firstName; } 
    public String getLastName()  { return  lastName;  } 
    public String getCountry()   { return country; } 

    @Override
    public String toString() { 
        return String.format("First Name :- %s, Last Name :- %s, Country :- %s.%n"
                             , this.firstName
                 , this.lastName
                 , this.country);
    }

}

It has three properties, and it has getters and setters for that.

public class PersonClone{

    static Person clone(final Person person){
    final Person clone = new Person();
    clone.setFirstName(person.getFirstName());
    clone.setLastName(person.getLastName());
    clone.setCountry(person.getCountry());
        return clone;
    }
}

And, We have another class, which provides the ability to create prototypes.

Prototyping and cloning are the same thing.

We create a new Person object and copy all the values from the Person object that was passed to it as an argument.

We can do the same thing by using java Cloneable interface.

Now, Person class will implement this interface and provide a clone function to create proptotypes.

The full Person class code is here.

public class Person implements Cloneable{
    private String firstName;
    private String lastName;
    private String country;

    public Person(){}
    public Person(final String firstName, final String lastName, final String country){
        this.firstName = firstName;
        this.lastName  = lastName;
        this.country   = country;
    }

    public void setFirstName(final String firstName){ this.firstName = firstName; } 
    public void setLastName(final String lastName){ this.lastName    = lastName; } 
    public void setCountry(final String country){ this.country       = country; } 

    public String getFirstName() { return firstName; } 
    public String getLastName()  { return  lastName;  } 
    public String getCountry()   { return country; } 

    @Override
    public String toString() { 
        return String.format("First Name :- %s, Last Name :- %s, Country :- %s.%n"
                             , this.firstName
                 , this.lastName
                 , this.country);
    }

    @Override
    public Object clone(){
        try{
        final Person cloneOfPerson = (Person) super.clone();
        cloneOfPerson.firstName = this.firstName;
        cloneOfPerson.lastName  = this.lastName;
        cloneOfPerson.country   = this.country;

        return cloneOfPerson;
    }catch(CloneNotSupportedException ex){
        ex.printStackTrace();
    }
    return null;
    }

}

If you look at the clone function above, you will see the same kind of implementation, that we did in clone function in PersonClone Class.

But, the difference is, each class has a superclass, and to create their prototype we need to call their clone function i.e super.clone();

You can see the full source code at github