Decoupling musing



Not seen this done but it stikes me as a approach that may have some merit.

Often a class needs to know nothing concrete about the object it is calling
methods on.
However sometimes such a class needs to instantiate this very object and yet
not care so long as it implements interface X.
So why not pass in a Class object that implements X, and do
class.newInstance()

public X createAndProvideAUsefulX(Class<? extends X> myClassOfX) {

X x = myClassOfX.newInstance();

x.doWork(blah);

return x;
}

--
Mike W


.



Relevant Pages