Menu

default factory spoils TDD benefits of IoC

2009-09-18
2013-05-10
  • Glenn-Meister

    Glenn-Meister - 2009-09-18

    For me, one big benefit of IoC is deferring the selection of actual type that gets instantiated. Currently, the resolver has a default factory that creates an object that is an instance of the return type. This means that I cannot use virtual methods in the return type which pretty much limits my selection of type that actually gets instanced.

     
  • Adam Young

    Adam Young - 2009-09-25

    Doesn't have to.  The default Factory is just the simplest approach.  I'll try to put together an example that shows how you can use a derived type.  You have to register your own factory to do it:

    I've checked code into the simple example on the TRUNK that shows creating an instance of type Base that is backed by the subclass Derived.

    class Base {
    public:
      virtual ~Base(){};
      virtual void do_action();
    };

    class Derived : public Base {
    public:
      virtual ~Derived(){};
      virtual void do_action();
    };

    Create a factory method….

    Base* makeBase(Zone& zone){
      return new Derived();
    }

    and register it:

    supply<Base>::configure(makeBase);

    Then use it:

    Base * base = supply<Base>::fetch(request1);
    base->do_action()

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.