1

4 つの文字列フィールドと 3 つのブール フィールドを持つ Item オブジェクトがあります。3 つのブール変数に基づいてこのオブジェクトを構築する必要があります。目標は、ブール変数のいずれかが真である場合はいつでも、その/それらのブール変数が設定されたオブジェクトを作成する必要があることです。いずれのブール変数も true でない場合は、オブジェクトを作成しません。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

0

あなたの説明から私が理解していることから:

a)特定のオブジェクトを作成するかどうかを決定するブール値がいくつかあります。

b)「チェックプロトコル」にブール値をさらに含める必要がある場合があります

c)ループでこのチェックを行う必要があります

i/ bool 変数をチェックします

ii/ オブジェクトが以前に作成されたかどうかを確認します

私はまだそれをよく理解していませんが..それは私にはかなり簡単に見えます. boolean[] boolsブール値がブール配列に格納され、文字列が文字列配列に格納されているとしましょうString[] strings(ところで、それらが何に使用されるかはわかりません)。すべての bool が true かどうかを確認し、その結果に基づいてオブジェクトを作成すると言っています。

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 に答える