Search code examples
javadesign-patternsprototype-pattern

Is my scenario come under Prototype Design Pattern?


Scenario 1 :

I am generating a report for more department's performance and participation in a institute. When I am display the report in GUI, it can be sort by department performance and participation(No.of student participated).

  1. For this scenario, should i use Prototype Design pattern?

Ex :

  public abstract class Report implements Cloneable {
   private String id;
   protected String type;

   public void setId(String id){
      id=id;
   }
   public String getId(){
      return id;
   }
   public String getType(){
      return type;
   }

  abstract void getReportData();

  public Object clone() {
      Object clone = null;          
      try {
         clone = super.clone();             
      } catch (CloneNotSupportedException e) {
         e.printStackTrace();
      }          
      return clone;
   }
}

public class PerformanceReport extends Report {
   public PerformanceReport(){
     type = "Performance";
   }

   @Override
   public void getReportData() {
          /* Get report data from database and sort based on performance*/

   }
}


public class ParticipationReport extends Report {

   public ParticipationReport(){
     type = "Participation";
   }

   @Override
   public void getReportData() {
          /* Get report data from database and sort based on participation*/

   }  
}

    public class ReportCache {

   private static Hashtable<String, Report> reportMap  = new Hashtable<String, Report>();

   public static Report getReport(String reportid) {
      Report cachedReport = reportMap.get(reportid);
      return (Report) cachedReport.clone();
   }

   public static void loadCache() {
      ParticipationReport participationReport = new ParticipationReport();
      participationReport.setId("1");
      reportMap.put(report.getId(),report);

      PerformanceReport performanceReport = new PerformanceReport();
      performancenReport.setId("2");
      reportMap.put(report.getId(),report);
   }
}


public class PrototypePatternReport {
   public static void main(String[] args) {
      ReportCache.loadCache();

      Report clonedReport = (Report) ReportCache.getReport("1");
      System.out.println("Report : " + clonedReport.getType());     

      Report clonedReport2 = (Report) ReportCache.getReport("2");
      System.out.println("Report : " + clonedReport2.getType());    
  }
}
  1. Is my above concept is correct ? and this concept is relevant to Prototype-pattern?

Scenario 2 :

I am storing quiz detail (questions and options, answers) in a object, while student request for quiz, I should encrypt the answer and give. For encrypted answer i should keep another object to give. I this scenario can i use prototype? After response come from student I should compare the student answer with existing object.


Solution

  • Prototype pattern is often useful when object initialization is expensive or when you explicitly need an object that is a copy of another.

    Scenario 1: In your case, getting report data from database and sorting it is much more expensive than instantiating an object, and each report will consist on its own data (you will not benefit from copying from another object) so I would not consider using a prototype.

    Scenario 2: In this scenario, the key is

    For encrypted answer i should keep another object to give

    In this case, as you need another object and you need to ensure that the second object is an exact copy of the first, you could use a prototype to create the second object, and then change its properties to ensure that the answers are hidden.