如何在具有多个参数的构造函数的工厂方法中初始化 类

How to initialize the classes in factory method which have constructors with multiple parameters

假设我有一个计算形状面积的 Shape 接口。我添加了 2 个实现 Rectangle 和 Square。我看到的挑战是两种实现都有自己的多参数构造函数。我如何使用工厂模式初始化它们。 我想用 java.

来解决
public class Rectangle implements Shape {
int length;
int breadth;

public Rectangle(List<String> parameters) {
    this.length = Integer.parseInt(parameters.get(0));
    this.breadth = Integer.parseInt(parameters.get(1));
}

@Override
public int area() {
    return length * breadth;
}

}

public class Square implements Shape {
int edge;

public Square(List<String> parameters) {
    this.edge = Integer.parseInt(parameters.get(0));
}

@Override
public int area() {

    return edge * edge;
}

}

public interface Shape {
int area();

}

public interface ShapeFactory {
public Shape make(String shapeType);
public List<String> getParameters(String shapeType);

}

public class ShapeFactoryImpl implements ShapeFactory {
Map<String, List<String>> shapeInitMap = new HashMap<>();

public void init(){
    shapeInitMap.put("Circle", Arrays.asList(new String[]{"4"}));
    shapeInitMap.put("Rectangle", Arrays.asList(new String[]{"2","3"}));
}

@Override
public Shape make(String shapeType) {
    switch (shapeType) {
    case "Circle":
        return new Square(getParameters(shapeType));
    case "Square":
        return new Rectangle(getParameters(shapeType));
    default:
        break;
    }
    return null;
}

@Override
public List<String> getParameters(String shapeType) {

    return shapeInitMap.get(shapeType);
}

}

您的解决方案不是最优的,因为:1) 您必须为您的具体 Shape 创建专用的构造函数,并且您放弃了参数的类型检查(在编译时)。 2)混凝土工厂的init方法容易出错

这是我会做的。具体工厂应该携带具体 Shapes 构造函数的参数,但不能作为不确定的字符串(如果您从用户输入中获取字符串,请在创建具体工厂之前转换它们):

public interface ShapeFactory {
    public Shape make(String shapeType);
}

public class ShapeFactoryImpl implements ShapeFactory {
    private int circleRadius;
    private int rectangleLength;
    private int rectangleBreadth;

    public ShapeFactoryImpl(int circleRadius, int rectangleLength, int rectangleBreadth){
        this.circleRadius = circleRadius;
        this.rectangleLength = rectangleLength;
        this.rectangleBreadth = rectangleBreadth;
    }

    public Shape make(String shapeType) {
        switch (shapeType) {
            case "Circle": return new Circle(this.circleRadius); 
            case "Rectangle": return new Rectangle(this.rectangleLength, this.rectangleBreadth);
            default: throw new Exception("..."); 
        }
    }
}

客户不需要知道他正在使用的具体ShapeFactory,也不必担心他得到的具体Shape。依赖关系是倒置的:抽象而不是细节起着关键作用。但是如果可能形状的数量增加,你会得到一个带有很多相似参数的构造函数。这是另一个解决方案:

public class ShapeFactoryImpl implements ShapeFactory {
    private Shape circle;
    private Shape rectangle;

    public ShapeFactoryImpl(Circle circle, Rectangle rectangle){
        this.circle = circle;
        this.rectangle = rectangle;
    }

    public Shape make(String shapeType) {
        switch (shapeType) {
            case "Circle": return this.circle.clone(); 
            case "Rectangle": return this.rectangle.clone();
            default: throw new Exception("..."); 
        }
    }
}

这样更好,因为您不会混合参数:每个具体 Shape 包含自己的参数。如果你想让它更灵活,你可以使用一个Map将开关的责任移出混凝土工厂:

public class ShapeFactoryImpl implements ShapeFactory {
    private Map<String, Shape> shapeByType;

    public ShapeFactoryImpl(Map<String, Shape> shapeByType){
        this.shapeByType = shapeByType;
    }

    public Shape make(String shapeType) {
        Shape shape = this.shapeByType.get(Type).clone();
        if (shape == null) {
            throw new Exception("...");
        }
        return shape;
    }
}

我什至会使用 enum 作为形状类型而不是字符串,并使用 EnumMap 来处理开关:

public class ShapeFactoryImpl implements ShapeFactory {
    private EnumMap<ShapeType, Shape> shapeByType;

    public ShapeFactoryImpl(Map<ShapeType, Shape> shapeByType){
        this.shapeByType = shapeByType;
    }

    public Shape make(ShapeType shapeType) {
        return this.shapeByType.get(Type).clone();
    }
}

客户端必须知道 ShapeShapeFactory 接口以及 ShapeType 枚举。 "server" 提供了具体的 ShapeFactoryImpl 实例。