Tag Info

New answers tagged

0

If I know what a field will be initialized to If you know that, then chances are very high that either your class won't be very reusable or you make the mistake of assuming that you will know how your class will be used now and in future - while actually missing some possible usecases. I can explain this in more detail if you provide more information ...


5

Initializing as a parameter breaks encapsulation in that the caller can then do with the passed in list what it wants (clear at odd times). ArrayList<String> list = new ArrayList<String>(); Initializer init = new Initializer(list); //do various thing list.clear(); //now the list in init is also empty while init may still expect it to be filled ...


2

Let's address the fundamentals of Dependency Injection with a brief review of the dependency injection principle When following this principle, the conventional dependency relationships established from high-level, policy-setting modules to low-level, dependency modules are inverted (i.e. reversed), thus rendering high-level modules independent of the ...


1

Because dependency injection container can and is used for lifetime management. Basically, singleton's main purpose is to maintain lifetime of exactly one instance : it is created once at startup (or first use) and is destroyed when application ends. But if you put both lifetime management and behavior itself in single class (eg. singleton), you break single ...


0

Version 1 is better than version 2. The reason for that is, a car should not know anything about an owner or any tickets. As cars are objects in our real world, think about it: Can a car exist without having an owner or tickets? - Certainly yes. Therefore, remove those from car and make car an independent class that is only dependent of what it is ...


3

If a property meets the following criteria then I make it a constructor parameter: The class is dependent on it for operation It has no reason to change over the lifetime of the object Its value is known at object creation If it meets these criteria, then it's a constructor parameter. If it only meets one or two of the criteria then it's almost ...


5

The decision which parameters a constructor should have is the same decision which parameters an arbitrary function should have - it should have exactly the parameters which are needed to create a specific, ideally easy to understand, abstraction. And if your abstraction of a car encapsulates exactly those three things, version 2 reflects that much better ...


1

IMHO, it depends upon the parameters. I've used DI to inject cross cutting concerns, such as logging and session management. I think this is good use of DI as the magic of DI does not affect the class-specific logic. Injecting instance specific parameters seems to be asking for trouble. Not the least of which is someone else trying to work out how your ...



Top 50 recent answers are included