Page 1 of 1

Strategy or other pattern?

Posted: Tue Oct 27, 2009 2:52 am
by alex.barylski
I have a series of objects whose implementation details can change compiments of via DIP/IoC.

Database has multiple injection types (mysql, postgres, etc)
Router has multiple implementations (simple, etc)
Session has multiple implementations as well (File, Shmop, MySQL, Postgres, etc)

As it currently stands, each is instantiated and injected in a concrete fashion, that is, something like this:

Code: Select all

$database = new Database(new DatabaseMySQL('localhost', etc));
Crude example, not the actual API. Anyways, what I want to achieve is a configurable injection, based on a setting created at install of my application, so I am thinking, implement something like a DataBaseStrategy() which would create the proper driver based on config settings.

Although stratgey is about offering different algorithms, not entire implementations, so I wonder if maybe what I am looking at is a factory or sorts?
http://en.wikipedia.org/wiki/Factory_method_pattern wrote:The essence of the Factory Pattern is to "Define an interface for creating an object, but let the subclasses decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses."
Subclassing is not what I am looking to achieve here, rather just to isolate the creation of one of any number of driver/implementation objects and inject that into the adapter object. The creation of the driver class is based on some algorithm, not explicitly stated by the caller, such as I would suspect is the case with a factory|abstract factory method.

Builder pattern sounds a little overly complex, building a composite and returning that instance as opposed to just creating a single class based on a few settings provided.

Any comments, suggestions?

Cheers,
Alex

Re: Strategy or other pattern?

Posted: Tue Oct 27, 2009 8:27 am
by Christopher
Sounds like Factory. Strategy selects one among several algorithms; Factory one among several classes.

Re: Strategy or other pattern?

Posted: Tue Oct 27, 2009 1:51 pm
by alex.barylski
OK cool, thanks man. I really prefer using the term factory to describe the process but strategy also made a little bit of sense, as a factory seems to be geared towards subclassing and allowing the child class to dictate which class was used, which is not what I'm wanting.

But factory is what I iwll use, thanks again, I hate struggling with what pattern to use, so having a second voice only helps assert or deny my own assumptions.

Anyone else want to chime in and offer input? I'm quite set on factory now but if you can offer convincing argument I will probalby change. :)

Cheers,
Alex

Re: Strategy or other pattern?

Posted: Tue Oct 27, 2009 4:40 pm
by josh
I think you're implementing both patterns together, you're using a factory to instantiate an object being used to implement the strategy pattern, when you instantiate it youre using the factory pattern, then when you use this object's interface youre doing the strategy pattern, IMO

Re: Strategy or other pattern?

Posted: Tue Oct 27, 2009 6:13 pm
by alex.barylski
I think you're implementing both patterns together, you're using a factory to instantiate an object being used to implement the strategy pattern, when you instantiate it youre using the factory pattern, then when you use this object's interface youre doing the strategy pattern, IMO
Yes I also see this as well but primarily, the most outward interface would be strategy then, as that is what the client is using?

I am not passing it any parameters...it's almost like a decorator that uses a strategy that uses a factory. :P

Instead of

Code: Select all

$db = new DB(new DB_MySQL());
I'm doing:

Code: Select all

$db = new DB(new DB_Strategy());
Whihc DB_Strategy() might look somehting like:

Code: Select all

class DB_Strategy{
  function __construct()
  {
    $object = null;
    $config =$config['database']; // Pulled from registry or direclty from file, etc
    switch($config['type']){
      case 'mysql':
        $object = new DB_MySQL($config['host'], $config['user'], $config['pass'], $config['port']);
      case 'mssql':
        $object = new DB_MSSQL($config['dsn']);
      case 'postgres':
        $object = new DB_PostgreSQL($config['connection_string']);
    }
  }
}
I`m going to start a new topic on DI containers as this is off topic now...

Cheers,
ALex

Re: Strategy or other pattern?

Posted: Wed Oct 28, 2009 2:09 pm
by josh
Not sure that has anything to do with decorator FYI. Decorators are basically filters, objects that wrap objects that wrap objects