From the way I look at things
Class C splits
A single principle of responsibility, managing two responsibilities, namely: create instances of other objects (instance B) and show whatever it is. Creating an object must be handled by a separate class, and dependencies must be entered by users.
An open, closed principle, being closely related to B, while it could depend on the interface A, so it was open for extension using another implementation of A. See for more information. In other words, C had to use A, and the actual implementation was to be introduced in C.
Below you can write C for SOLID.
class C { private A a; public C(A a) { super(); this.a = a; } public void show() { a.calculate("test"); } } class Creator { C createC() { A b = new B(); return new C(b); } }
Creator can be replaced by a dependency injection framework, such as Spring.
source share