1

I'm developing a research framework that needs to allow users to override core functionality.

I have a core class, called TorSocket, that has a method called createCircuit() which returns a TorCircuit class. I want users of the framework to be able to override TorCircuit functions without modifying the source but because the only way to create such a class is through TorSocket.createCircuit() I'm not sure how to.

I've tried as follows, in TorSocket, creating a member variable:

 public Class circuitClass;

and then users could do:

 sock.circuitClass = class MyCircuitClass extends TorCircuit { ...

and the TorSocket class would use circuitClass to instantiate.

However, I'm getting a syntax error using the above code, at "class." What is the correct syntax for this? I cant find much online although welcome pointers.

Finally, how to I then use TorSocket.circuitClass to instantiate?

Many thanks Gareth

1
  • class doesn't return anything. Means problem with assigning statement Commented Aug 11, 2014 at 11:07

3 Answers 3

1

Use

class MyCircuitClass extends TorCircuit {...}
sock.circuitClass = MyCircuitClass.class;
Sign up to request clarification or add additional context in comments.

2 Comments

This is correct, but I think the OP is more in need of a different design-pattern, wouldn't you agree?
I stop thinking after Finally, how to I then use.
0

You can not have your users extend your base class but still want to control the creation of objects by yourself (how exactly would you do this? The user simply could call new MyCircuitClass()) - if you want to concentrate the creation of objects inside of one factory class and let the users only have to provide extended behavior I would suggest to create a set of interfaces your base class is using for which the users have to provide concrete implementations to create an object through your factory.

Comments

0

It seems a better way to do this is to use Generic methods like so:

public <T extends TorCircuit> T createCircuit(Class<T> torCircClass, boolean blocking) {
    T circ;
    try {
        circ = torCircClass.getDeclaredConstructor(TorSocket.class).newInstance(this);
    } catch (InstantiationException  | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
        throw new RuntimeException(e);
    }
    circ. ...
    return circ;
}

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.