Strategy or other pattern?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Strategy or other pattern?

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Strategy or other pattern?

Post by Christopher »

Sounds like Factory. Strategy selects one among several algorithms; Factory one among several classes.
(#10850)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Strategy or other pattern?

Post 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
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Strategy or other pattern?

Post 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
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Strategy or other pattern?

Post 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
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Strategy or other pattern?

Post by josh »

Not sure that has anything to do with decorator FYI. Decorators are basically filters, objects that wrap objects that wrap objects
Post Reply