mardi 12 septembre 2017

Factory that outputs a 3rd party client with modified behavior while preserving original behavior for later use

Here is the situation. I have a 3rd party class

public class Client {
    void kill();
}

I need my factory to return a modified version of this client where kill() does nothing (so that in future people don't accidentally break stuff). But I also need to be able to kill() when needed. Since this is a 3rd party library, I don't have access to modify the client. How can I make this work?

Part a of the problem is simple, I just have to extend Client:

void getClient() {
    return new Client() {
        @Override
        public void kill() {
            //do nothing
        }
    }
}

Part b, ie, saving the actions of the original parent kill() method is tricky and that's where I need help. Thanks





Aucun commentaire:

Enregistrer un commentaire