I am writing a program, where in the beginning of the execution, I am instantiating a number of classifier objects using parameters stored in some files.

I later use those classifiers in multiple objects.

My question is: how should an object which uses a classifier obtain this classifier object?

The objects which use the classifiers do not even exist at the initialization time of the program and they are "far away" from the initialization class, so even if they existed, passing the classifiers through multiple classes is a code smell.


3 Answers 3


It depends on how you want access to your objects (Use as less abstraction as needed):

1) use a Container like a Collection or a Map for simly putting the objects in. Collections are made just for "holding" things.

2) If you need more fine grained access: build another object containing the Container.

I later use those classifiers in multiple objects.

What does later mean in this context? Get the objects when you actually need them: at the latest possible time.

they are "far away" from the initialization class, so even if they existed, passing the classifiers through multiple classes is a code smell.

Why do you think that? From what you are writing, which is very abstract, there is no way of deciding, whether it is or it is not a code smell.

If the needed objects are needed for the life-time of the object, the latest possible time would be the earliest point in the lifetime of the object who needs the other object. Constructor injection would be one way to deal with that.


Make a "classifier holder" object, hand it the classifiers when they are created, and when the classifier-using objects need them, have them ask the holder to give them one.


I take it you have something like this?

public class Main(string[] args)
    string param = args[0]; //load parameter from config
    Classifier c = new Classifier(param)

    SomeClass x = new SomeClass(c);
    var o = x.DoSomething();

public class SomeClass
    OtherClass o;

    //doesnt use the classifier but needs it for OtherClass, 
    //hence required for passing down the chain
    public SomeClass(Classifier c) 
        o = new OtherClass(c);

    public object DoSomething() 
        return o.DoSomthing();

public class OtherClass
    public OtherClass(Classifier c) {} //uses classifier object 'far away'

    public object DoSomething() {}

And you want to avoid either having the Classifier object passed through ever constructor in the chain, or having objects which arent the actual application reading from the config directly?

My Suggestion in these cases it that you can do one of two things

A: Check that your 'higher in the chain classes' are obeying the single responsibility principle. Would it be better just to call OtherObject directly?

B: Can you move the injected parameter to be a parameter of the function call? in this case x.DoSomething(c);

In most cases I think it is a code smell. If you code is too 'deep' in this way its likely that your objects don't just do one thing anymore but orchestrate various other classes to do multiple things. Although one layer of this is good, say you have a Service() which wraps up all the logic and is then implemented in an app, a web site, a windows service etc. When you start wrapping that service in another service, in another service I think its time to take a step back and see if you cant just instantiate the lower level services you need directly and call them in one layer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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