bmw0128
bmw0128

Reputation: 13698

Composition of Objects

I have a class that acts as a manager and does some work. A servlet that starts up when the application server starts up instantiates this manager. I need to add another class that will do other work, and needs to coordinate with the manager. I was thinking about adding the class to the manager as an instance variable. Should I have the manager instantiate the new class (like in the constructor), or have the servlet instantiate the new class and call manager.setNewClass() after the manager has been instantiated?

Upvotes: 3

Views: 195

Answers (3)

hvgotcodes
hvgotcodes

Reputation: 120138

You should do the latter -- it decouples the manager from its delegate. To do the decoupling correctly, you should create an interface that defines the behavior the manager expects, and then provide an implementation via inversion of control/dependency injection. This will allow you to test the manager and its worker class (I called it a delegate, but it might not be) in isolation.

EDIT -- this answer assumes java because you mentioned servlet.

You have your manager class, in it you expect an interface

class Manager {
    Worker worker;

    Manager(Worker worker) {
        this.worker = workder
    }
}

Worker is an interface. It defines behavour but not implementation

interface Worker {
    public void doesSomething(); //method definition but  no implementation
}

you now need to create an implementation

class WorkerImpl implements Worker {
    // must define a doesSomething() implementation
}

The manager just knows it gets some Worker. You can provide any class that implements the interface. This is decoupling -- the Manager is not bound to any particular implementation, it is bound only to the behavour of a worker.

Upvotes: 0

vz0
vz0

Reputation: 32923

This reminds me of the FFF pattern.

It does not matter where you create the instance. Just create wherever it fits you best, and if you need it somewhere else, just apply some basic refactoring.

If you really need decoupling try using some tool like Guice, but only if you really need it.

Upvotes: 1

ircmaxell
ircmaxell

Reputation: 165193

Well, as a gross-over-generalization, you should instantiate it in the servlet and pass it into the manager (either via a constructor parameter, or via setNewClass())... Inject the dependencies rather than hard-code them.

However, depending on your exact use-case, even that might not be the right answer. You might be better off with a Builder for constructing the manager class. That way, the builder manages the construction of the entire manager (including any dependencies) rather than hard-coding it into the servlet. This would move the dependency out of the servlet and into the builder (which you can better deal with in tests and other code).

The short answer is that there's no silver bullet. Without knowing the hard relationships between all of the classes, and the roles and responsibilities, it's hard to say the best method. But instantiating in a constructor is almost never a good idea and you should inject the dependency in some form or another (but from where is up for debate)...

Upvotes: 2

Related Questions