Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 接口-注册继承接口的类以基于该类创建对象_Java_Interface_Abstract Class - Fatal编程技术网

Java 接口-注册继承接口的类以基于该类创建对象

Java 接口-注册继承接口的类以基于该类创建对象,java,interface,abstract-class,Java,Interface,Abstract Class,如何“注册”一个类,该类实现了一个基于“注册”类创建对象的接口?让我解释一下我想做什么: 我为我的线程池服务器编码了一个请求处理器,我希望可以在服务器上“配置”请求处理器,这样我就可以选择服务器应该使用哪个请求处理器来处理它的请求。以下是我认为它应该如何工作: // yes I know the interface can't extend the abstract Thread class ... // see in the Server class what I want to do wit

如何“注册”一个类,该类实现了一个基于“注册”类创建对象的接口?让我解释一下我想做什么:

我为我的线程池服务器编码了一个请求处理器,我希望可以在服务器上“配置”请求处理器,这样我就可以选择服务器应该使用哪个请求处理器来处理它的请求。以下是我认为它应该如何工作:

// yes I know the interface can't extend the abstract Thread class ...
// see in the Server class what I want to do with it
public interface RequestProcessor extends Thread {

public final Socker socket;

RequestProcessor(final Socket paramSocket) {
    socket = paramSocket;
}

// abstract so each request processor implementing this interface
// has to handle the request in his specific way
abstract void run();

}

public class RequestProcessorA implements RequestProcessor {

RequestProcessorA(final Socket paramSocket) {
    super(paramSocket);
}

@Override
public void run() {
    // do something with the request
}

}

public class RequestProcessorB implements RequestProcessor {

RequestProcessorB(final Socket paramSocket) {
    super(paramSocket);
}

@Override
public void run() {
    // do something different with the request
}

}
因此,将其作为接口(或者作为抽象类来扩展Thread类),我想创建一个服务器,并告诉他使用wheater RequestProcessorA或RequestProcessorB,如:

public class Server extends Thread {

private final RequestProcessor processor;
private final ServerSocker server;

Server(final int paramPort, final RequestProcessor paramProcessor) {
    processor = paramProcessor;
    server = new ServerSocket(paramPort);
}

@Override
public void run() {
    while (true) {
        Socket socket = server.accept();
        // how can I create a new RequestProcessorA object if "processor" is type of RequestProcessorA???
        // how can I create a new RequestProcessorB object if "processor" is type of RequestProcessorB???
        // how can I create a new CustomProcessor object if "processor" is type of CustomProcessor???
        RequestProcessor rp = ???;
        // add rp to the thread pool
    }
}

}
那么如何设计这样的需求呢?我不想让它成为一个可扩展的库,这样其他人就可以简单地使用服务器,只需要通过extendimg/实现我的接口/抽象类来编写自己的请求处理器,并简单地“注册”自己的请求处理器

我希望你们能理解我的意思。如果这是一个dup,很抱歉,但我真的不知道它的技术术语:/

您将要使用。为可能要生成的每个类创建一个工厂接口和一个实现:

public interface IRequestProcessorFactory {
    RequestProcessor create(Socket paramSocket);
}
并按如下方式实施:

public class RequestProcessorAFactory implements IRequestProcessorFactory {
    public RequestProcessor create(Socket paramSocket) {
        return new RequestProcessorA(paramSocket);
    }
}
并将其实例传递给
服务器
。然后,打电话

RequestProcessor rp = factory.create(socket);

获取
RequestProcessorA
RequestProcessorB
,具体取决于通过的工厂。

请参阅
java.util.ServiceLoader.
@EJP所以这不是一个简单的接口/抽象类问题?@Pali这是关于实例的创建,所以比这更复杂。@Pali否,这是一个接口/抽象类/工厂/注册问题,这是java提供的答案。你不考虑这个工厂方法模式而不是抽象工厂吗?工作就像一个魅力!非常感谢你,公民!太好了@约翰:我认为这是一个抽象工厂,因为我们有多个工厂,每个类一个,我们可能想要生产。我认为父类型是接口而不是抽象类并不重要。请看维基百科文章中的类图。