mercredi 18 février 2015

Is my scenario come under Prototype Design Pattern?

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?


Aucun commentaire:

Enregistrer un commentaire