java - 将属性传递给工厂方法

标签 java design-patterns factory-pattern builder-pattern

我有一个返回接口(interface)实现的工厂方法。问题是 - 实现具有不同的构造函数参数。

我的问题是 - 如何通过工厂方法将参数传递给接口(interface)的不同实现?

我有一个想法,但我不确定它是否有意义 - 将 Properties 对象传递给工厂方法?这样每个接口(interface)实现都可以获得其构造函数所需的属性,而工厂接口(interface)将是统一的。

这是否有意义,或者有更好的解决方案?

我决定加一个例子,这样我就可以更好地阐明问题。

假设我们有接口(interface) SomeAlgorithm 并且我们有具体的算法,其中每个算法可能有不同的参数,例如

SomeAlgorithm algo = new Algo1();
SomeAlgorithm algo = new Algo2(noOfIterations);
SomeAlgorithm algo = new Algo3(precision, boundary);

我希望能够做类似的事情

SomeAlgorithm algo = AlgoFactory.getAlgo("algoName");

我处理不同参数的方法是

SomeAlgorithm algo = AlgoFactory.getAlgo("algoName", properties); 

然后,如果算法完全有参数(例如,Algo1 没有参数),AlgoFactory 可以将适当的属性传递给具体的算法构造函数。如果某些属性不存在,则可以传递默认值(如果算法中需要该值)。

如您所见,我希望能够动态更改算法。用户将在运行时选择算法并传递将放入属性对象的适当参数。

这有意义吗?

最佳答案

我认为您需要实现 Builder模式。

The builder pattern is an object creation software design pattern. Unlike the abstract factory pattern and the factory method pattern whose intention is to enable polymorphism, the intention of the builder pattern is to find a solution to the telescoping constructor anti-pattern[citation needed].

当对象构造函数参数组合的增加导致构造函数的指数列表时,就会出现伸缩构造函数反模式。

Instead of using numerous constructors, the builder pattern uses another object, a builder, that receives each initialization parameter step by step and then returns the resulting constructed object at once.

看看这个示例代码。

class SomeAlgorithm{
    // Make it or class or interface
}
class Algo extends SomeAlgorithm{
    private int noOfIterations;
    private double precision; 
    private double boundary;

    public Algo(Builder builder){
        this.noOfIterations = builder.noOfIterations;
        this.precision= builder.precision;
        this.boundary= builder.boundary;
    }
    public String toString(){
        return new StringBuilder("Algo:Iterations:precision:boundary:").append(noOfIterations).append(":").
        append(precision).append(":").append(boundary).toString();
    }
    static class Builder {
        private int noOfIterations; // Mandatory parameter
        private double precision = 1.0; // Optional parameter
        private double boundary = 2.0; // Optional parameter

        public Builder ( int noOfIterations){
            this.noOfIterations = noOfIterations;
        }
        public Builder precision(double precision){
            this.precision = precision;
            return this;
        }
        public Builder boundary(double boundary){
            this.boundary = boundary;
            return this;
        }
        public Algo build(){
            return new Algo(this);
        }
    }
}
public class BuilderDemo{
    public static void main(String args[]){
        Algo algo = new Algo.Builder(2).precision(3.0).boundary(4.0).build();
        System.out.println(algo);
        algo = new Algo.Builder(10).build();
        System.out.println(algo);
    }
}

输出:

java BuilderDemo 2
Algo:Iterations:precision:boundary:2:3.0:4.0
Algo:Iterations:precision:boundary:10:1.0:2.0

如果您必须为构造函数实现具有相同参数集且没有 if-else 语句的工厂方法,请查看 this alternative

但我更喜欢获得相同的结果:

public static Algo newInstance(String algoClassType) {
    return Class.forName(algoClassType).newInstance();      
}

关于java - 将属性传递给工厂方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34726422/

相关文章:

java - Android base 64 编码字符串不显示实际图像

java - 基于注解的 ServiceLocatorFactoryBean?

java - 这是否使用工厂设计模式? ( java )

java - 装饰模式问题

javascript - 基本的小型 Angular 文件结构

c# - 为什么我不能通过 Type.GetType() 找到我在 app.config 中引用的插件实例的类型?

java - 为什么不允许我使用 Tibco Rendezvous 确认消息?

java - 如何安装STS到eclipseoxy(2018.march) 市场上没有sts?

java - 公共(public)静态变量和私有(private)静态变量之间的区别

design-patterns - 抽象工厂与工厂方法 : Composition vs Inheritance?