java - 我的场景属于原型(prototype)设计模式吗?

标签 java design-patterns prototype-pattern

场景 1:

我正在生成一份报告,以了解更多部门的绩效和对研究所的参与。当我在 GUI 中显示报告时,它可以按部门表现和参与(参与的学生人数)排序。

  1. 对于这种情况,我应该使用原型(prototype)设计模式吗?

例如:

  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());    
  }
}
  • 我的上述概念是否正确?这个概念与原型(prototype)模式相关吗?
  • 场景 2:

    我将测验详细信息(问题和选项、答案)存储在一个对象中,当学生请求测验时,我应该加密答案并给出。对于加密答案,我应该保留另一个对象来给出。在这种情况下我可以使用原型(prototype)吗?在学生做出回应后,我应该将学生的答案与现有对象进行比较。

    最佳答案

    当对象初始化成本高昂或明确需要一个对象是另一个对象的副本时,原型(prototype)模式通常很有用。

    场景 1: 就您而言,从数据库获取报告数据并对其进行排序比实例化对象要昂贵得多,并且每个报告都将包含自己的数据(您不会从另一个对象复制中受益),因此我不会考虑使用原型(prototype)。

    场景 2: 在这种情况下,关键是

    For encrypted answer i should keep another object to give

    在这种情况下,当您需要另一个对象并且需要确保第二个对象是第一个对象的精确副本时,您可以使用原型(prototype)来创建第二个对象,然后更改其属性以确保隐藏答案。

    关于java - 我的场景属于原型(prototype)设计模式吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28580926/

    相关文章:

    java - 使用 JPA 进行 save() 时出现重复行

    java - 将大于号和小于号中的文本传递到后端

    java - 将 JAXB 注释应用于现有数据模型

    原型(prototype)模式的 Java 实现

    Delphi原型(prototype)模式

    JavaFX 结合实用程序和透明舞台风格

    java - 验证日期 - Bean 验证注释 - 具有特定格式

    java - OOP 设计 - 需要准备每个方法调用并在之后清理

    C++ 全局访问类属性时应该使用哪种设计模式

    design-patterns - 原型(prototype)与。享元设计模式