具有多个泛型的装饰器模式
Decorator Pattern with multiple generics
我目前正在进行一些代码重构。所以我想到用 decorator
设计替换现有的 inheritance
设计。但我正在与多个泛型作斗争(也许根本不可能)。
我现在有上面的设计。有 IConstraint
,其中 check
是 class 针对实施的约束。这些约束的具体实现是 SimpleConstraintA
和 SimpleConstraintB
它们都在检查 ClassA
中的一些值。 Decorator
增强了约束,例如当指定值不在范围内时,不应检查某些约束。 ClassA
实现接口 IA
和 IB
以便 DecoratorA
和 DecoratorB
可以使用它。
设计的用法如下:
Test classToCheck = new Test("test");
IConstraint<Test> constraint = new DecoratorA<>(new DecoratorB<>(new SimpleConstraint()));
boolean value = constraint.check(classToCheck);
所以我想要的是使用具有不同数量和不同类型的输入参数的代码。喜欢:
Test classToCheckA = new Test("testA");
Test classToCheckB = new Test("testB");
IConstraint<Test> constraint = new DecoratorA<>(new DecoratorB<>(new SimpleConstraint()));
boolean value = constraint.check(classToCheckA, classToCheckB);
或者:
Test classToCheckA = new Test("testA");
// TestB does implement the same interfaces as Test
TestB classToCheckB = new TestB("testB");
IConstraint<Test> constraint = new DecoratorA<>(new DecoratorB<>(new SimpleConstraint()));
boolean value = constraint.check(classToCheckA, classToCheckB);
或者:
Test classToCheckA = new Test("testA");
// TestB does implement the same interfaces as Test
TestB classToCheckB = new TestB("testB");
// TestC does implement the same interfaces as Test
TestC classToCheckC = new TestC("testC");
IConstraint<Test> constraint = new DecoratorA<>(new DecoratorB<>(new SimpleConstraint()));
boolean value = constraint.check(classToCheckA, classToCheckB, classToCheckC);
我尝试使用 varargs
、Lists
或 Object[]
而不是 check(obj:T)
中的 T
但后来我总是需要转换和很多异常处理(比如输入参数的个数要正确),所以不满意。
下面的代码是我尝试过的一个例子。正如您在 SimpleConstraint
中看到的那样,check
方法仅允许类型 (Test
)。
public interface IConstraint<T extends ICheckable> {
public boolean check(T[] checkable);
}
public class SimpleConstraint implements IConstraint<Test> {
@Override
public boolean check(Test[] checkable) {
return true;
}
}
以上代码无法实现:
Test classToCheckA = new Test("testA");
// TestB does implement the same interfaces as Test
TestB classToCheckB = new TestB("testB");
IConstraint<Test> constraint = new DecoratorA<>(new DecoratorB<>(new SimpleConstraint()));
boolean value = constraint.check(classToCheckA, classToCheckB);
有没有设计上的改进,可以支持不同数量和不同类型的输入参数?
在上面的代码中,问题是 Test 和 TestB 没有共同的祖先...
IConstraint<Test> constraint = ...
boolean value = constraint.check(classToCheckA, classToCheckB);
如果 TestB extends Test
或其他方式,您可以做到这一点。
更好的方法是
IConstraint<ICheckable> constraint =
我目前正在进行一些代码重构。所以我想到用 decorator
设计替换现有的 inheritance
设计。但我正在与多个泛型作斗争(也许根本不可能)。
我现在有上面的设计。有 IConstraint
,其中 check
是 class 针对实施的约束。这些约束的具体实现是 SimpleConstraintA
和 SimpleConstraintB
它们都在检查 ClassA
中的一些值。 Decorator
增强了约束,例如当指定值不在范围内时,不应检查某些约束。 ClassA
实现接口 IA
和 IB
以便 DecoratorA
和 DecoratorB
可以使用它。
设计的用法如下:
Test classToCheck = new Test("test");
IConstraint<Test> constraint = new DecoratorA<>(new DecoratorB<>(new SimpleConstraint()));
boolean value = constraint.check(classToCheck);
所以我想要的是使用具有不同数量和不同类型的输入参数的代码。喜欢:
Test classToCheckA = new Test("testA");
Test classToCheckB = new Test("testB");
IConstraint<Test> constraint = new DecoratorA<>(new DecoratorB<>(new SimpleConstraint()));
boolean value = constraint.check(classToCheckA, classToCheckB);
或者:
Test classToCheckA = new Test("testA");
// TestB does implement the same interfaces as Test
TestB classToCheckB = new TestB("testB");
IConstraint<Test> constraint = new DecoratorA<>(new DecoratorB<>(new SimpleConstraint()));
boolean value = constraint.check(classToCheckA, classToCheckB);
或者:
Test classToCheckA = new Test("testA");
// TestB does implement the same interfaces as Test
TestB classToCheckB = new TestB("testB");
// TestC does implement the same interfaces as Test
TestC classToCheckC = new TestC("testC");
IConstraint<Test> constraint = new DecoratorA<>(new DecoratorB<>(new SimpleConstraint()));
boolean value = constraint.check(classToCheckA, classToCheckB, classToCheckC);
我尝试使用 varargs
、Lists
或 Object[]
而不是 check(obj:T)
中的 T
但后来我总是需要转换和很多异常处理(比如输入参数的个数要正确),所以不满意。
下面的代码是我尝试过的一个例子。正如您在 SimpleConstraint
中看到的那样,check
方法仅允许类型 (Test
)。
public interface IConstraint<T extends ICheckable> {
public boolean check(T[] checkable);
}
public class SimpleConstraint implements IConstraint<Test> {
@Override
public boolean check(Test[] checkable) {
return true;
}
}
以上代码无法实现:
Test classToCheckA = new Test("testA");
// TestB does implement the same interfaces as Test
TestB classToCheckB = new TestB("testB");
IConstraint<Test> constraint = new DecoratorA<>(new DecoratorB<>(new SimpleConstraint()));
boolean value = constraint.check(classToCheckA, classToCheckB);
有没有设计上的改进,可以支持不同数量和不同类型的输入参数?
在上面的代码中,问题是 Test 和 TestB 没有共同的祖先...
IConstraint<Test> constraint = ...
boolean value = constraint.check(classToCheckA, classToCheckB);
如果 TestB extends Test
或其他方式,您可以做到这一点。
更好的方法是
IConstraint<ICheckable> constraint =