我试图在Java上使用Composite模式以生成报告,但我显然忘记了层次结构和方法重载的工作方式。
假设我有以下模型:
public class Product {
public String get(){
return "Product";
}
}
public class BProduct extends Product {
public String getB() {
return "BBBBB";
}
}
public class CProduct extends Product {
public String getC() {
return "CCCCC";
}
}
和以下转换器:
import java.util.List;
public class Converter {
private List<Converter> converters;
public Converter() {
}
public Converter(List<Converter> converters) {
this.converters = converters;
}
public void execute(Product product) {
for (Converter converter : converters) {
converter.execute(product);
}
}
}
public class BConverter extends Converter {
@Override
public void execute(Product product) {
innerExecute(product);
}
public void innerExecute(Product product) {
System.out.println(product.get() + " done on B normal.");
}
public void innerExecute(BProduct b) {
System.out.println(b.getB() + " done on B special.");
}
}
public class CConverter extends Converter {
@Override
public void execute(Product product) {
System.out.println(product.get() + " done on C normal.");
}
public void execute(CProduct c) {
System.out.println(c.getC() + " done on C special.");
}
}
使用以下测试进行测试:
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
public class ProductConverterTest {
@Test
public void test() {
List<Converter> converters = new ArrayList<>();
converters.add(new BConverter());
converters.add(new CConverter());
Converter converter = new Converter(converters);
List<Product> list = new ArrayList<>();
list.add(new Product());
list.add(new BProduct());
list.add(new CProduct());
for (Product product : list) {
converter.execute(product);
}
}
}
作为输出获取:
Product done on B normal.
Product done on C normal.
Product done on B normal.
Product done on C normal.
Product done on B normal.
Product done on C normal.
当我想要的是:
Product done on B normal.
Product done on C normal.
BBBBB done on B special.
Product done on C normal.
Product done on B normal.
CCCCC done on C special.
注意:我要执行此操作而不使用
instanceof
。我已经知道如何使用它。我想知道的是,没有它,是否可以做到。 最佳答案
此行为的原因是#execute
方法已重载。在编译时选择要调用的重载方法。为了使其按需工作,您必须使用动态选择,这将导致instanceOf
检查或覆盖product
的方法或通过/检索类,并进行比较,例如:
public class Converter {
private List<Converter> converters;
public Converter() {
}
public Converter(List<Converter> converters) {
this.converters = converters;
}
public void execute(Product product) {
for (Converter converter : converters) {
converter.execute(product);
}
}
}
public class BConverter extends Converter {
@Override
public void execute(Product product) {
if (product.getClass() == BProduct.class) {
innerExecute((BProduct)product);
} else {
innerExecute(product);
}
}
public void innerExecute(Product product) {
System.out.println(product.get() + " done on B normal.");
}
public void innerExecute(BProduct b) {
System.out.println(b.getB() + " done on B special.");
}
}
public class CConverter extends Converter {
@Override
public void execute(Product product) {
if (product.getClass() == CProduct.class) {
innerExecute((CProduct)product);
} else {
innerExecute(product);
}
}
public void innerExecute(Product product) {
System.out.println(product.get() + " done on C normal.");
}
public void innerExecute(CProduct b) {
System.out.println(b.getC() + " done on C special.");
}
}