2012-12-14 28 views
1

我有三种方法。如何从依赖其他数据方法的数据获取数据?

  1. 第一种方法从列表(其包含数据对象特定于我们的我的应用程序),它然后调用第二方法,传递它已经获取作为参数的数据取数据。

  2. 第二种方法然后使用此信息作为参考,从另一个列表中获取特定信息,调用第三个方法并将该信息作为参数传递给它。

  3. 第三种方法与第二种方法相同,使用传入的数据作为参考来查找列表中的特定数据。

现在的问题是,我想用完全不同的方法使用这些信息。

我该如何去调用我的方法来获取数据?

private void getRecords() { 
    List<LabelSet> allSets = labelSetStructureService.getLabelSets(); 
    for (LabelSet labelSet : allSets) { 
     List<LabelSet> groups = labelSetStructureService.getLabelGroupsForSet(labelSet.getId()); 
     for (LabelGroup group : groups) { 
      getValuesForLabel(group.getCustomerMeasure(), labelSetStructureService.getLabelsForLabelGroup(group.getCustomer().getId(), group.getId()), labelSet); 
     } 
    } 
} 

private void getValuesForLabel(CustomerMeasure measure, List<Label> labels, LabelSet labelSet) { 
    for (Label label : labels) { 
     List<LabelSet> usageRecs = labelDataService.getLabelData(label, LabelSetStorage.DAY_BY_MONTH_STORAGE, new DateTime(), new DateTime().minusWeeks(1)); 
     preProcessUsageRecs(usageRecs, labelSet); 
    } 
} 

private List<LabelUsage> preProcessUsageRecs(List<LabelUsage> usageRecs, LabelSet labelSet) { 
    //Format records, use list instead strings where possible 

    List<LabelSet> usageRecords = usageRecs; { 
     for(LabelUsage labelUsage : usageRecs) 
      usageRecords.addAll(usageRecs); 
    } 
    return usageRecords; 
    } 
+0

需要进一步解释 – rptmat57

+2

如果没有太多的代码,那么很高兴看到 –

+0

通过返回语句传回所有内容? – Max

回答

0

Java是一种基于对象的语言,所以用一个成员变量来存储一个调用的结果,就像这样:

public List<LabelUsage> labels; 
... 
private List<LabelUsage> preProcessUsageRecs(List<LabelUsage> usageRecs, LabelSet labelSet) { 
    //Format records, use list instead strings where possible 

    List<LabelSet> usageRecords = usageRecs; { 
     for(LabelUsage labelUsage : usageRecs) 
      usageRecords.addAll(usageRecs); 
    } 
    this.labels = usageRecords; 
    return usageRecords; 
} 

,然后访问this.labels无论你将需要使用这样的结果方法;

+0

实例成员(或类成员 - 静态)有时不适合(或不正确)用于维护数据一致性 - 特别是在像web这样的多用户环境中。 – Lion

+0

谢谢你提醒我!有时简单的解决方案是你不认为的! –

+0

@Lion:你能向我解释一下吗? – Goldentoa11

0

这听起来像你正在写一些可疑的代码。根据您的描述,它听起来就像你的API应该是这个样子,如果我理解这个问题:

public void go(Map<MapKey,Collection<MyObject>> values, Keyword keyword) 
{ 
     for(MapKey key: values.keyset()) 
    { 
       search(values.get(key),keyword); 
    } 
} 

public void search(Collection<MyObject> searchSpace, Keyword key) 
{ 
     for(MyObject currentSearch : searchSpace) 
     { 
      if(currentSearch == key) 
      { 
       //do something 
      } 
     } 
} 

的一点是,你应该能够一概而论搜索到一个功能,如果有什么特殊需要的情况发生,你可以将一些私有(helper)函数应用于此进程并通过switch语句绑定它们。

相关问题