【问题标题】:Is my scenario come under Prototype Design Pattern?我的场景是否属于原型设计模式?
【发布时间】:2015-02-18 10:12:15
【问题描述】:

场景 1:

我正在生成一份报告,以了解更多部门的绩效和参与机构的情况。当我在 GUI 中显示报告时,它可以按部门绩效和参与度(参与的学生人数)排序。

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

例如:

  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. 我的上述概念是否正确?这个概念与原型模式有关吗?

场景 2:

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

【问题讨论】:

    标签: java design-patterns prototype-pattern


    【解决方案1】:

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

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

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

    对于加密的答案,我应该保留另一个对象来提供

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

    【讨论】:

    • 你能举个例子吗?
    • 以游戏开发环境为例,我将使用原型根据给定的原型实例创建一个新的敌人,然后更改其属性(生命、武器伤害……)。这种方法将利用现有实例(3D 模型、纹理等),并避免创建全新的对象(可能非常昂贵)。
    • 谢谢,请参阅我编辑的问题。我添加了另一个场景。
    • 不客气。我已经编辑了我的答案以包含您的新场景
    猜你喜欢
    • 2013-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    相关资源
    最近更新 更多