Re: Decoupling musing
- From: "Daniel Dyer" <dan@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sun, 20 Nov 2005 20:46:01 -0000
On Sun, 20 Nov 2005 17:44:51 -0000, VisionSet <spam@xxxxxxxxxxxx> wrote:
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; }
It's probably a useful approach in some situations. The approach that tends to be used for this kind of thing is to give the class a reference to a factory object that can be used to create the instances. The advantages of using a factory being that you can hide the details of exactly which classes are used and how the objects are created. With a factory you could have a create method that used a different class dependent on the parameters passed in, or you could use cloning, deserialisation or shared objects instead of invoking constructors, all without the client class having to know anything about how things work.
Dan.
-- Daniel Dyer http://www.dandyer.co.uk .
- Follow-Ups:
- Re: Decoupling musing
- From: VisionSet
- Re: Decoupling musing
- References:
- Decoupling musing
- From: VisionSet
- Decoupling musing
- Prev by Date: Re: JUG
- Next by Date: java sdk suggestions
- Previous by thread: Decoupling musing
- Next by thread: Re: Decoupling musing
- Index(es):
Relevant Pages
|