views:

50

answers:

2

Hi I have a problem where i have to perform similar steps / actions (which differ slightly) on different sets of data (the data can be slightly different also with regards to its strucure). There are a few steps that i need to perform: connect, validate, build error messages if required, change the strcture of the data in to a set format and output results.

I was looking at the template method pattern which looks like it would fit this quite nicely - does this sound correct or would there be a better way to tackle this type of problem?

I am aiming for a design whereby new sets of data can be added to the system easily without disrupting anyything, and also the error reporting can be changed etc without affecting much in the system.

I am using php.

+1  A: 

Template method sounds pretty good here, but also consider Strategy http://stackoverflow.com/questions/672083/when-to-use-template-method-vs-strategy

philfreo
A: 

The strategy method works well with the template method.

The template method specifies requirements of the class. The strategy method allows you to use objects to defined the behaviours, and to define them at run-time using dependency injection.

class CRUDTemplate
{
    protected $updater;
    protected $creator;
    protected $deletor;
    protected $loader;

    public function __construct(IUpdate $updater, ICreator $creator, IDeletor $delete, ILoader $loader)
    {
          $this->updater = $updater;
          //...snipped...
    }

    // sample template function; others are create, delete, update 
    public function load($id)
    {
          $this->content = $this->loader->load($id);
    }
}

While the sample code just shows the template functions calling each of the strategy, more can be done in a specialized overloaded version of the template functions, such as validation of input, initisation and such. And after all, something has to call the strategies.

The pros is that, for the example above, you can use different different set of strategies for different conditions (CRUD may not be the best example here). So if you want to change the way the data is output, but retain how it is saved/created/loaded,you just need to change the output strategy.

The cons is that you have to make sure the strategies are for the right context, as there is no type checking to ensure the strategies you pass in are valid (any will work as long as it is of the expectd base class). Oh yeah, the CRUDTemplate could derive from a base class too.

For data, in PHP I find that the array works best for passing complex parameters. Of course, you have to do your validation of the array

Extrakun