3

Background:

I have a list of classes (comm objects), which may or may not increase. They all implement the same method in an interface Comms:

public int send(Socket socket, byte[] message);

I get a list of these comm classes by specifying the package name in the method's packageName parameter (excluding the package name itself internally in the function and doing some filtering to get just the names package comm):

public static Class[] getClasses(String packageName); (modified it a bit from the link below)

http://www.dzone.com/snippets/get-all-classes-within-package

Then I have several plugins which are configurable to use one of the comm objects as base communication.

Comm objects:


serial

client

server

etc.


Plugins:


plugin1

plugin2

etc.


The manager class will receive a request for sending a control packet from a plugin, and the manager will just queue the requests and call the send function for each request on a

Question:

How do I access the method send(Socket socket, byte[] message); within manager or the plugin itself? This involves creating a generic class object which may call send, which is cast from one of the comm classes, depending on the plugin configuration, from the string name of the comm object.

The configuration of which comm class is used for each plugin is stored within a database. Converting from string to a Class object works well. I just need to find a way to call the send function which resides in the interface Comm which is implemented by all the comm classes. This has to be generic. More comm classes may be added.

1 Answer 1

1

Give the manager a reference type Comm interface and call its send method.

When you instantiate the manager, inject it with the implementation that you wish.

It sounds like you already have the virtual constructor/factory pattern down for instantiating each type of Comm implementation.

This is a common pattern for all dependency injection engines (e.g. Spring, Guice, etc.)

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

Comments

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.