Java 如何基于布尔条件创建对象?
我有一个Item对象,它有4个字符串字段和3个布尔字段。 我必须基于3个布尔变量构造这个对象。 目标是只要布尔变量中的任何一个为真,我们就必须创建具有该布尔变量集的对象。 如果在任何情况下布尔变量都不是真的,我们就不会创建对象。 我使用COR来检查是否会根据某些业务逻辑设置任何布尔字段。 我曾在builder中尝试过这一点,但后来我不得不构造这么多对象,然后在布尔变量都不为真时丢弃它们 有谁能想出更好的办法来解决这类问题吗 好的,谢谢你为这个问题设置了2个删除标志。谢谢你对这个问题的思考。 我做了一些事情来实现我想要的。我相信这是相当灵活的。只有部分依赖于if循环,但这是可以接受的,因为报表类可以有额外的布尔值,所以当该类被更改时,应该触摸它的生成器以适应该更改。休息这是灵活的,我想要的。 公开课报告{Java 如何基于布尔条件创建对象?,java,design-patterns,creation-pattern,Java,Design Patterns,Creation Pattern,我有一个Item对象,它有4个字符串字段和3个布尔字段。 我必须基于3个布尔变量构造这个对象。 目标是只要布尔变量中的任何一个为真,我们就必须创建具有该布尔变量集的对象。 如果在任何情况下布尔变量都不是真的,我们就不会创建对象。 我使用COR来检查是否会根据某些业务逻辑设置任何布尔字段。 我曾在builder中尝试过这一点,但后来我不得不构造这么多对象,然后在布尔变量都不为真时丢弃它们 有谁能想出更好的办法来解决这类问题吗 好的,谢谢你为这个问题设置了2个删除标志。谢谢你对这个问题的思考。 我做
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;
}
}
私有字符串acftNo;
私人日期计划日期;
私有字符串计划stn;
私有整数mntncId;
私人设置有能力的电台;
私有字符串路由stn;
私有布尔值IsRoutedNewPlannedStn;//不一致类型1
私有布尔值IsCapableAttPlannedStn;//不一致类型2
私有布尔值isPlannedOrRoutedStationExists;//不一致类型3/5
公共报告(字符串acftNo,整数mntncId){
超级();
this.acftNo=acftNo;
this.mntncId=mntncId;
}
公共报告(字符串acftNo、日期PLANNEDATE、字符串plannedStn、,
整数(mntncId){
超级();
this.acftNo=acftNo;
this.plannedDate=plannedDate;
this.plannedStn=plannedStn;
this.mntncId=mntncId;
}
//设置器和获取器。已删除以留出空间。
公共静态报表buildReport(维护任务,集合枚举集合){
报告温度=新报告(task.getAssignment().getAircraftNumber(),task.getAssignment().getMntncScheduleDate(),
task.getAssignment().getStationCode(),task.getAssignment().getMntncId());
临时可设置工作站(不一致的报告getCapableStations(任务));
for(不一致的YReport ir:enumSet)
{
if(ir.compareTo(不一致的报告路由\u STN\u NEQ\u计划\u STN)==0)
温度设置路由NEQPLANNEDSTN(真);
如果(ir.compareTo(不一致的报告项目在计划时的可用性)==0)
温度可设置为计划的温度(真);
如果(ir.compareTo(不一致的报告没有在日期发送)=0)
温度设置PlannedOrroutedStationExists(真);
}
返回温度;
}
}
CalculateInContencyReport()方法,该方法将决定是否创建对象
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;
}
}
公共类不一致性yreportchain{
public enum unconsistencyReport实现了unconsistencyReportFace{
已路由\u STN\u NEQ\u计划\u STN{
@凌驾
公共布尔查找一致性(维护任务){
如果(!验证(任务))
返回false;
//一些逻辑
返回true;
返回false;
}
},
项目\n有能力\n计划的\n{
@凌驾
公共布尔查找一致性(维护任务){
如果(!验证(任务))
返回false;
//一些逻辑
返回true;
返回false;
}
},
在约会上没有失败{
@凌驾
公共布尔查找一致性(维护任务){
如果(!验证(任务))
返回false;
//一些逻辑
返回真值
返回false;
}
};
@凌驾
公共布尔验证(维护任务){
return!(null==task.getAssignment());
}
静态设置getCapableStations(维护任务)
{
Set capableStations=newHashSet();
if(task.getCapStationList()!=null)
{
addAll(Arrays.asList(task.getCapStationList().split(StringConstants.COMMA\u split\u REGEX));
}
if(task.getCapStationClassList()!=null)
{
Map stationClassMap=CacheManager.get(STN_CLASS.name());
List stationClass=Arrays.asList(task.getCapStationClassList().split(StringConstants.COMMA_split_REGEX));
for(字符串stnClass:stationClass)
{
capableStations.addAll(stationClassMap.get(stnClass));
}
}
返回能力站;
}
}
公共静态报告CalculateInSitenCyReport(维护任务){
Set enumSet=null;
for(unconsistencyReport iReport:unconsistencyReport.values())
{
if(iReport.findInconsistency(任务))
{
if(null==enumSet)
enumSet=enumSet.of(iReport);
其他的
enumSet.add(iReport);
}
}
if(null!=enumSet&&enumSet.size()>0)
return Report.buildReport(任务,枚举集);
返回null;
}
}
助手界面:
public interface InconsistencyReportIface {
public boolean findInconsistency(Maintenance<?> task );
public boolean validate(Maintenance<?> task );
}
公共接口不一致性报告部分{
公共布尔查找一致性(维护任务);
公共布尔验证(维护任务);
}
由于安全原因,类逻辑的细节被删除 有什么问题?只要在其中一个布尔值为真时创建对象即可
if(bool1 || bool2 || bool3) {
item = new Item(str1, str2, str3, str4, bool1, bool2, bool3);
}
根据我对你描述的理解: a) 你会有一些胸部,这将
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