Re: Decoupling musing



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
.



Relevant Pages

  • Re: Dynamic inheritance
    ... >I've been reading the Factory and Abstract Factory patterns - and finding ... It seems the problem boils down to where/when to instantiate ... We have an interface named DeviceFactory. ... >Since most devices would be Cisco devices the 'cisco device' subclass would ...
    (comp.object)
  • Re: Dynamic inheritance
    ... >I've been reading the Factory and Abstract Factory patterns - and finding ... It seems the problem boils down to where/when to instantiate ... We have an interface named DeviceFactory. ... >Since most devices would be Cisco devices the 'cisco device' subclass would ...
    (comp.programming)
  • Re: Redefining __call__ in an instance
    ... Return the requested class but don't instantiate it, ... the point of doing this is to make getting a factory class more ... do stuff with target here ... ...
    (comp.lang.python)
  • Re: Design question: Multiplying singletons
    ... Since relationship implementation is orthogonal to class semantics and it is usually easy to implement when originally defining objects, it is usually a good ideal to implement the *-side of such relationships and instantiate them properly even if there is no immediate need to navigate them. ... but not as much to keep the reference to the whole in the ... through the AController object. ... One could use a factory like AController to instantiate the relationships dynamically, ...
    (comp.object)
  • Re: Making constructor visible only to a certain class
    ... My reason for having protected internal constructors is to prevent ... Only the factory should do so, ... decision of which class to instantiate belongs to the factory. ... Work-around 1. ...
    (microsoft.public.dotnet.languages.csharp)