设计思路,消除作为方法参数的布尔值。

huangapple 未分类评论46阅读模式
英文:

Design idea to eliminate booleans as method parameters

问题

我目前正在重写一些 Util 类中的代码。

其中定义了一些静态可配置属性数据的集合,需要根据某些布尔标志来使用。

static Set<String> oneDayProperties = D
static Set<String> oneDayPropertiesWithB = D,B 
static Set<String> oneDayPropertiesForM = D,B
static Set<String> oneDayPropertiesForMWithB = D,B
static Set<String> oneDayPropertiesForL = D,B
static Set<String> oneDayPropertiesForLWithB = D,B
static Set<String> oneDayPropertiesNonDefault = D,B
.
.
static Set<String> twoDayProperties = D,B
static Set<String> twoDayPropertiesWithB = D,B
static Set<String> twoDayPropertiesForM = D,B
static Set<String> twoDayPropertiesForMWithB = D,B
static Set<String> twoDayPropertiesForL = D,B
static Set<String> twoDayPropertiesForLWithB = D,B
static Set<String> twoDayPropertiesNonDefault = D,B
...

根据传递给以下方法的布尔属性参数来决定返回哪个集合。有多个类似的方法。所有方法具有相同的参数集,并返回 Set<String>。问题是,如果我需要添加一个条件,代码将变得更加混乱。简单的布尔参数可以用枚举替代,但是由于存在多维条件,我无法想出任何方法。

public static Set<String> get1DayProperties(boolean isForL, boolean isForM, boolean isBIncluded, String area) {
    if (DataCacheConstants.DEFAULT_AREA.equals(area)) {
        if (!isForL) {
            return !isForM ? (!isBIncluded ? oneDayProperties : oneDayPropertiesWithB)
                            : (!isBIncluded ? oneDayPropertiesForM : oneDayPropertiesForMWithB);
        }
        else {
            return !isBIncluded ? oneDayPropertiesForL : oneDayPropertiesForLWithB;
        }
    }
    else {
        return oneDayPropertiesNonDefault;
    }
}

public static Set<String> get2DayProperties(boolean isForL, boolean isForM, boolean isBIncluded, String area) {
                .
                .
                .
                
public static Set<String> get3DayProperties(boolean isForL, boolean isForM, boolean isBIncluded, String area) {
                .
                .
                .

对于如何处理这种情况,任何想法都会非常感激。

英文:

I am currently working on rewriting some codes in an Util class.

It has static Sets of configurable property data defined, that needs to be used based on certain boolean flags.

    static Set&lt;String&gt; oneDayProperties = D
	static Set&lt;String&gt; oneDayPropertiesWithB = D,B 
	static Set&lt;String&gt; oneDayPropertiesForM = D,B
	static Set&lt;String&gt; oneDayPropertiesForMWithB = D,B
	static Set&lt;String&gt; oneDayPropertiesForL = D,B
	static Set&lt;String&gt; oneDayPropertiesForLWithB = D,B
	static Set&lt;String&gt; oneDayPropertiesNonDefault = D,B
	.
    .
	static Set&lt;String&gt; twoDayProperties = D,B
	static Set&lt;String&gt; twoDayPropertiesWithB = D,B
	static Set&lt;String&gt; twoDayPropertiesForM = D,B
	static Set&lt;String&gt; twoDayPropertiesForMWithB = D,B
	static Set&lt;String&gt; twoDayPropertiesForL = D,B
	static Set&lt;String&gt; twoDayPropertiesForLWithB = D,B
	static Set&lt;String&gt; twoDayPropertiesNonDefault = D,B
	...

Which Set to return is based on the boolean properties sent as parameters to methods like below. There are multiple methods like this. All methods have the same set of parameters and return Set<String>. Problem is if I need to add one more condition, it will make the code further ugly. Simple boolean parameters can be replaced with enums, but can't think of anything in this case because of these multi-dimensional conditions.

public static Set&lt;String&gt; get1DayProperties(boolean isForL, boolean isForM, boolean isBIncluded, String area) {
    if (DataCacheConstants.DEFAULT_AREA.equals(area)) {
        if (!isForL) {
            return !isForM ? (!isBIncluded ? oneDayProperties : oneDayPropertiesWithB)
                            : (!isBIncluded ? oneDayPropertiesForM : oneDayPropertiesForMWithB);
        }
        else {
            return !isBIncluded ? oneDayPropertiesForL : oneDayPropertiesForLWithB;
        }
    }
    else {
        return oneDayPropertiesNonDefault;
    }
}

public static Set&lt;String&gt; get2DayProperties(boolean isForL, boolean isForM, boolean isBIncluded, String area) {
				.
				.
				.
				
public static Set&lt;String&gt; get3DayProperties(boolean isForL, boolean isForM, boolean isBIncluded, String area) {
				.
				.
				.

Any help with ideas is much appreciated.

huangapple
  • 本文由 发表于 2020年7月23日 10:30:08
  • 转载请务必保留本文链接:https://java.coder-hub.com/63045955.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定