Thursday, 15 March 2018

Protoype Design Pattern in Java

Design patterns are programming language independent solution providers for specific problems. Design patterns will have some specific rules to provide solutions and we need to follow some rules to implement design pattern. We have different kind of design patterns for different types of problems. Suppose if we want to create class and we want to create only object for that class,  we can go for Singleton design pattern.

Suppose if we want to just clone the object instead of creating new one ,we can choose Prototype Design Pattern. We can also customize the cloned object as per our requirement if we use prototype design pattern. This pattern will follow java cloning feature.

Advantages:

1. With this design pattern, we can reduce the sub class mechanism.
2. It can add objects at runtime.
3. It avoids complexity to create objects.

Based on above advantages, we can go for this design pattern, if we come across any of above mentioned advantages like if we need objects which are slimier to already existing objects or if we need object creation dynamically at run time and if we want to perform some DB CRUD operations instead of creating new object every time to get data, we can clone existing one and can modify the data.

Example:

package com.sample.java.testing;

import java.util.ArrayList;
import java.util.List;

public class CoursePrototype implements Cloneable {

    List<String> courses;
  
    public CoursePrototype(){
        courses = new ArrayList<>();
    }
  
    public CoursePrototype(List<String> courses){
        this.courses = courses;
    }
  
    public void addCourse(){
        courses.add("J2SE");
        courses.add("J2EE");
        courses.add("Spring");
        courses.add("Hibernate");
    }
  
    public Object clone(){
        //if we do this temp list here, just we can clone object with list
        //other wise we need to call addcoures()method on each of cloned object
        List<String> tempCcourses = new ArrayList<>();
        for (String string : this.getCourseList()) {
            tempCcourses.add(string);
        }
        return new CoursePrototype(tempCcourses);
    }
  
    public List<String> getCourseList(){
        return courses;
    }
  

}


package com.sample.java.testing;

import java.util.List;

public class ProtoTypeDemoTest {

    public static void main(String[] args) {

        CoursePrototype coursePrototype = new CoursePrototype();
        coursePrototype.addCourse();
        System.out.println(coursePrototype.getCourseList());
        CoursePrototype cloneCoursePrototype = (CoursePrototype) coursePrototype.clone();
        //cloneCoursePrototype.addCourse();//as we already add dummy list inside clone method
        System.out.println(cloneCoursePrototype.getCourseList());//same as new object becoz we did not do any chnages
        List<String> list = cloneCoursePrototype.getCourseList();
        list.add("Design Pattern");//can modify as per our requirement
        System.out.println(cloneCoursePrototype.getCourseList());//it is different than original object
        System.out.println(coursePrototype.getCourseList());//no changes in original object
      
        CoursePrototype cloneCoursePrototype1 = (CoursePrototype) coursePrototype.clone();
        //cloneCoursePrototype1.addCourse();//as we already add dummy list inside clone method
        List<String> list1= cloneCoursePrototype1.getCourseList();
        list1.remove("Hibernate");//can modify as per our requirement
        System.out.println(cloneCoursePrototype1.getCourseList());//it is different than original object
    }

}


Output:
[J2SE, J2EE, Spring, Hibernate]
[J2SE, J2EE, Spring, Hibernate]
[J2SE, J2EE, Spring, Hibernate, Design Pattern]
[J2SE, J2EE, Spring, Hibernate]

No comments:

Post a Comment