I am fairly new to the SOLID principles and I have found i often come to the following situation.
I have an implementation of a task that is tied to an interface, when i need to use the class i just use DI for such an interface.
My problem is what to do when i need multiple implementations, for example.
List<IDataSource> dataSources = new ArrayList<IDataSource>();
dataSources.add(new DataSourceOne());
dataSources.add(new DataSourceTwo());
.... and so one...
then later i will loop over the array to action the interface method for each implementation.
Is this bad practice? is there a way of populating the list without the new keyword? eg c# reflection. or is the a design pattern that avoids this?
What have you got against the new
keyword when you want to create a new object?
You can simplify the code as:
List<DataSource> dataSources = Array.asList(
new DataSourceOne(),
new DataSourceTwo(),
...
);
Although it has some uses, using reflection is almost always a really bad idea.