2

How can I "register" a class which implements an interface to create objects based on the "registered" class? Let me explain what I want to do:

I an coding a request processor for my thread pooled server and I want the request processor can be "configured" on the server so I can choose which request processor the server should use for it's requests. Here is how I think it should have worked:

// 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
}

}

So having this as interface (or as abstract class to be able to extend the Thread class) I want to create a server and tell him to use wheater RequestProcessorA or RequestProcessorB like:

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
    }
}

}

So how to design such requirements? I wan't to make this an extendable library so others can simply use the server and just have to code their own request processors by extendimg/implementing my interface/abstract class and simply "register" their own request processor?

I hope you guys understand what I mean. And sorry if this is a dup but I dont really know the technical terms for this :/

4
  • 1
    See java.util.ServiceLoader. Commented Dec 10, 2015 at 21:29
  • @EJP so this isn't a simple interface/abstract class question? Commented Dec 10, 2015 at 21:32
  • @Pali This is about creation of instances, so it's more complicated than that. Commented Dec 10, 2015 at 21:35
  • 1
    @Pali No, it is an interface/abstract class/factory/registration question, and that is the answer that Java provides. Commented Dec 10, 2015 at 21:39

1 Answer 1

3

You will want to use the Abstract Factory pattern. Create one factory interface, and one implementation for each class you might want to produce:

public interface IRequestProcessorFactory {
    RequestProcessor create(Socket paramSocket);
}

and implement it like this:

public class RequestProcessorAFactory implements IRequestProcessorFactory {
    public RequestProcessor create(Socket paramSocket) {
        return new RequestProcessorA(paramSocket);
    }
}

and pass an instance of it to Server. Then, call

RequestProcessor rp = factory.create(socket);

to obtain what will either be a RequestProcessorA or RequestProcessorB, depending on which factory was passed.

Sign up to request clarification or add additional context in comments.

3 Comments

Would you not consider this Factory method pattern rather than Abstract Factory ?
Working like a charm! Thank you very much random citizen!
Great! @John: I believe that this is Abstract Factory, since we have multiple factories, one per class that we might want to produce. I don't think it matters that the parent type is an interface rather than an abstract class. Look at the class diagram in the Wikipedia article.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.