简体   繁体   English

Java如果我具有不同的构造函数但具有相同的接口,则应为对象实例化使用哪种设计模式?

[英]Java what design pattern should I use for object instantiation if I have different constructor but same interface?

I'm learning about the design patterns and I encountered a problem which I cant resolve. 我正在学习设计模式,遇到一个无法解决的问题。 I'm writing a client/server script. 我正在编写一个客户端/服务器脚本。 The administrator client send a task with its task data in json format, and the server should instantiate an object accordingly to the recieved task type, and fill its constructor with correct classes. 管理员客户端发送带有json格式任务数据的任务,服务器应根据接收到的任务类型实例化一个对象,并用正确的类填充其构造函数。 As you can see bellow there are two example class. 如您所见,下面有两个示例类。

public class StartProcessing implements ITask{
    private final IProcessor dataProcessor;

    public StartProcessing(IProcessor dataProcessor){
        this.dataProcessor = dataProcessor;
    }

    @Override
    public void ProcessTask() {
        this.dataProcessor.StartProcess();
    }

}

public class StartQueueFiller implements ITask{

    private IQueueFiller queueFiller;

    public StartQueueFiller(IQueueFiller queueFiller){
        this.queueFiller = queueFiller;
    }

    @Override
    public void ProcessTask() {
        this.queueFiller.Start();
    }

}

interface ITask {
    public void ProcessTask();
}

I've tried something like this, but I'll have like 50 different process and hundreds of tasks, so the constructor will be unmanageable, and I think the factory pattern is not so good in this case, and probably I just miss the point of the pattern. 我已经尝试过类似的方法,但是我将拥有50个不同的流程和数百个任务,因此构造函数将难以管理,并且我认为这种情况下的工厂模式不是很好,可能我只是想念一下重点模式。 So how you would solve this problem? 那么,您将如何解决这个问题呢? What should I use instead of the factory pattern? 我应该使用什么代替工厂模式?

public class TaskFactory(){

    private final IProcessor processor;
    private final IQueueFiller queuefiller;

    public TaskFactory(IProcessor processor, IQueueFiller queuefiller){
        this.processor = processor;
        this.queuefiller = queuefiller;
    }

    public ITask Create(String task){
        switch(task){
            case "startprocessor":
                return new StartProcessing(this.processor);
            case "startqueuefiller":
                return new StartQueueFiller(this.queuefiller);
        }
    }

}

I would just use the Abstract Factory pattern: 我只会使用Abstract Factory模式:

public interface TaskFactory {
    Task create();
}

Then we can store a bunch of factories in a data structure of some kind, eg: 然后,我们可以将一堆工厂存储在某种数据结构中,例如:

private final Map<String, TaskFactory> factoriesMap = new HashMap<>();

void registerFactory(String identifier, TaskFactory factory) {
    factoriesMap.put(identifier, factory);
}

public Task create(String identifier) {
    return factoriesMap.get(identifier).create();
}

Then we can register different kinds of factories using a lambda or something: 然后,我们可以使用lambda或其他方式注册不同类型的工厂:

Processor processor = ...;
registerFactory("startprocessor", () -> new StartProcessingTask(processor));

etc. 等等

At some point you're going to realize that your "factory map" is basically a kind of Service Locator, in which case you either need to double-down on that, or find an alternative solution. 在某个时候,您将意识到您的“工厂地图”基本上是一种服务定位器,在这种情况下,您需要加倍努力,或者找到替代解决方案。 I tend to prefer Dependency Injection as an approach here. 在这里,我倾向于将依赖注入作为一种方法。 Depending on your DI environment, you might make all your TaskFactory instances injectable using qualifiers. 根据您的DI环境,可以使用限定符使所有TaskFactory实例可注入。 You can either bind lazy providers of actual task objects, or bind a factory-like object (eg "assisted inject"). 您可以绑定实际任务对象的惰性提供程序,也可以绑定类似工厂的对象(例如,“辅助注入”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以在外观设计模式中有一个界面吗? - Can I have an interface in facade design pattern? 我应该将哪种设计模式用于简单的报告系统 - What design pattern should I use for a simple reporting system 我应该使用哪种设计模式? 使用Spring框架 - What design pattern should I use ? using spring framework 我应该在Java中的TODO列表中使用哪种设计模式? - Which design pattern should I use for a TODO list in Java? 我可以使用Java泛型在Java中具有相同接口的不同实现吗? - Can I use java generics to have different implementations of same interface in Java? 我可以使用哪种设计模式来创建实现特定接口或可用接口之间的多个或任意组合的对象? - what design pattern can I use to create an object that implements a specific interface or multiple or any combination among the available interfaces? 我可以在JAVA中使用哪种设计模式来满足此要求 - What design pattern can i use in JAVA for this requirement 我应该使用哪种工厂设计模式? - Which factory design pattern i should use? 复合设计模式:是否应该在父接口中插入add()和remove()? - Composite Design Pattern: Should I insert add() and remove() in the Parent interface? 使用哪种设计模式或体系结构来覆盖默认类的实例化或创建 - What Design Pattern or Architecture to use to override default class Instantiation or Creation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM