如何根据布尔条件创建对象?

时间:2015-12-28 12:40:48

标签: java design-patterns creation-pattern

我有一个Item对象,有4个String字段和3个boolean字段。 我必须基于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 );

}

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

2 个答案:

答案 0 :(得分:1)

有什么问题?只需在你的一个布尔值为真时创建你的对象。

if(bool1 || bool2 || bool3) {
    item = new Item(str1, str2, str3, str4, bool1, bool2, bool3);
}

答案 1 :(得分:0)

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

a)你会有一些bool会决定你是否创造某个特定对象。

b)你可能需要在&#34;检查协议中加入更多的bool&#34;

c)你必须在循环中进行检查

我/你检查bool变量

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

我还没有完全明白,但是......看起来很顺利。让我们说你的bool存储在一个布尔数组$ionicPlatform.ready(function() { if (window.cordova && window.cordova.plugins.Keyboard) { cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); cordova.plugins.Keyboard.disableScroll(true); } if (window.StatusBar) { if (ionic.Platform.isAndroid()) { //StatusBar.backgroundColorByHexString("#60C7CC"); $cordovaStatusbar.styleHex('#60C7CC'); } else { StatusBar.styleLightContent(); } } }); 中,你的字符串存储在一个字符串数组boolean[] bools中(顺便说一下,我不知道它们用于什么)。您要说的是检查每个bool是否为真,然后根据该结果创建一个对象。

String[] strings

我不明白为什么你需要先检查一下这个物体是否已经建成,所以我没有把它包括在我的建议中:P