1

我有一个 Item 对象,它有 4 个字符串字段和 3 个布尔字段。我必须基于 3 个布尔变量来构造这个对象。目标是每当布尔变量中的任何一个为真时,我们都必须创建具有该/那些布尔变量集的对象。如果在任何情况下都没有布尔变量为真,我们将不会创建对象。我正在使用 COR 来检查是否会根据某些业务逻辑设置任何布尔字段。我正在使用构建器尝试此操作,但随后我必须构造这么多对象,然后在没有布尔变量发现为真时丢弃它们。

谁能有更好的主意来解决这种问题?

好吧,感谢这个问题的 2 删除标志。也感谢您对这个问题的想法。我做了一些事情来实现我想要的。我相信这是非常灵活的。只有部分依赖于 If 循环,但这是可以接受的,因为 Report 类可以具有额外的布尔值,因此当该类发生更改时,应该触摸它的构建器以适应该更改。休息这是我想要的灵活。公共类报告{

    private String acftNo;
    private Date plannedDate;
    private String plannedStn;
    private Integer mntncId;
    private Set<String> capableStations;
    private String routedStn;
    private boolean isRoutedNEQPlannedStn;  //Inconsistency     type 1
    private boolean isCapableAtPlannedStn;  //Inconsistency     type 2 
    private boolean isPlannedOrRoutedStationExists;  //Inconsistency     type 3/5   

    public Report(String acftNo, Integer mntncId) {
        super();
        this.acftNo = acftNo;
        this.mntncId = mntncId;
    }

    public Report(String acftNo, Date plannedDate, String plannedStn,
            Integer mntncId) {
        super();
        this.acftNo = acftNo;
        this.plannedDate = plannedDate;
        this.plannedStn = plannedStn;
        this.mntncId = mntncId;
    }

    //setters and getters. Removed for space.

    public static Report buildReport(Maintenance<?> task, Set<InconsistencyReport> enumSet) {
        Report temp = new Report(task.getAssignment().getAircraftNumber(),task.getAssignment().getMntncScheduleDate(),
                task.getAssignment().getStationCode(),task.getAssignment().getMntncId());
        temp.setCapableStations(InconsistencyReport.getCapableStations(task));
        for(InconsistencyReport ir : enumSet)
        {
            if(ir.compareTo(InconsistencyReport.ROUTED_STN_NEQ_PLANNED_STN)==0)
                temp.setRoutedNEQPlannedStn(true);
            if(ir.compareTo(InconsistencyReport.ITEM_NT_CAPABLE_AT_PLANNED_STN)==0)
                temp.setCapableAtPlannedStn(true);
            if(ir.compareTo(InconsistencyReport.NO_ROUTD_STN_ON_A_DATE)==0)
                temp.setPlannedOrRoutedStationExists(true);
        }
        return temp;
    }
}

calculateInconsitencyReport() 方法将决定是否创建对象。

public class InconsistencyReportChain {

    public enum InconsistencyReport implements InconsistencyReportIface {

        ROUTED_STN_NEQ_PLANNED_STN  {
            @Override
            public boolean findInconsistency(Maintenance<?> task ) {
                if(!validate(task))
                    return false;
                //some logic 
                    return true;
                return false;
            }
        },
        ITEM_NT_CAPABLE_AT_PLANNED_STN  {
            @Override
            public boolean findInconsistency(Maintenance<?> task) {
                if(!validate(task))
                    return false;
                //some logic
                    return true;
                return false;
            }
        },
        NO_ROUTD_STN_ON_A_DATE  {
            @Override
            public boolean findInconsistency(Maintenance<?> task) {
                if(!validate(task))
                    return false;
                //some logic 
                    return true
                return false; 
            }
        };

        @Override
        public boolean validate(Maintenance<?> task) {
            return !(null == task.getAssignment());
        }

        static Set<String> getCapableStations(Maintenance<?> task)
        {
            Set<String> capableStations = newHashSet();
            if(task.getCapStationList() != null)
            {
                capableStations.addAll(Arrays.asList(task.getCapStationList().split(StringConstants.COMMA_SPLIT_REGEX)));
            }
            if(task.getCapStationClassList() != null)
            {
                Map<String, List<String>> stationClassMap = CacheManager.get(STN_CLASS.name());
                List<String> stationClass = Arrays.asList(task.getCapStationClassList().split(StringConstants.COMMA_SPLIT_REGEX));
                for(String stnClass : stationClass)
                {
                    capableStations.addAll(stationClassMap.get(stnClass));
                }
            }
            return capableStations;
        }
    }

    public static Report calculateInconsitencyReport(Maintenance<?> task)   {
        Set<InconsistencyReport> enumSet = null;
        for(InconsistencyReport iReport : InconsistencyReport.values())
        {
            if(iReport.findInconsistency(task))
            {
                if(null==enumSet)
                    enumSet = EnumSet.of(iReport);
                else
                    enumSet.add(iReport);
            }
        }
        if(null!= enumSet && enumSet.size() > 0)
            return Report.buildReport(task,enumSet);
        return null;
    }
}

助手接口:

public interface InconsistencyReportIface {

    public boolean findInconsistency(Maintenance<?> task );

    public boolean validate(Maintenance<?> task );

}

由于安全性,类逻辑的细节被撕掉了。

4

2 回答 2

1

问题是什么?当您的布尔值之一为真时,只需创建您的对象。

if(bool1 || bool2 || bool3) {
    item = new Item(str1, str2, str3, str4, bool1, bool2, bool3);
}
于 2015-12-28T12:48:21.613 回答
0

根据我对您的描述的理解:

a)您将拥有一些布尔值,它们将确定您是否创建某个对象。

b)您可能必须在“检查协议”中包含更多布尔值

c)您必须在循环中执行此检查

我/你检查布尔变量

ii/您检查该对象是否先前已创建

我还不太明白,但是..这对我来说很简单。假设您的布尔值存储在一个布尔数组中boolean[] bools,而您的字符串存储在一个字符串数组String[] strings中(顺便说一句,我不知道它们的用途)。您是说检查每个 bool 是否为真,然后根据该结果创建一个对象。

boolean[] bools = new boolean[] { ... };
String[] strings = new String[] { ... };
boolean checks = false;
for(int i = 0; i<bools.length && !checks; i++)
    checks = bools[i];
//so far we will have processed if any of the bools was false, which was your condition
if(checks)
    Object object = new Object(); //create your desired object

不过,我不明白为什么您需要检查该对象是否先前已构建,因此我没有将其包含在我的建议中:P

于 2015-12-28T13:10:39.287 回答