I have a large list of intercommunicating components; so I decided to have one class create all of them and then subsequently delegate them to other classes to facilitate finer tweaking and wiring together.

By "finer tweaking", I mean that the components are modified to their final state; and by "wiring together" I mean that components are given a way to communicate in a specific manner.

After they are all wired together, I send all the components over to an organizer to be properly organized into a GUI.

I read somewhere that a class should only create objects or use them, but never both; so, I am trying to follow that and also limit each class's responsibilities. This initializer/delegator class does both initializing and delegating, so should I further split it into an initializer and delegator?


Your top level class that does this creating and delegating sounds like a Service Locator, or Dependency Container. A Service Locator receives requests from components for objects providing a particular service (that is, implementing a particular interface). It goes off and creates an object of the relevant type (or gets an instance out of its cache depending on how exactly its been configured to handle requests for such services) and passes it back to the caller.

If your top level class is the first thing that's called in your application, then it may be a Bootstrapper.

In the former case, the delegation is implicit because you're calling into the Service Locator and returning the particular services, so all your class should be doing is creating instances of the relevant types at the appropriate times.

In the latter case, it's OK, because a Bootstrapper's task is to initialise the global data structures and spin up the processes that will operate on them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.