mercredi 4 janvier 2017

One class to call method from all other classes in directory

Sorry for a bit misleading title but I don't quite know how to ask that better.

I have a directory with classes doing same job but with different implementation. They all look like this:

class someClass {
    private static $variable=12345; 
    public static function someTask($keyword){
            ...
            return array();
    }
    private function pf() {...}

}

The methods are taking same arguments, and returning array of the same structure.

I'd like to create one class to be able to call selected classes form that folder (and maybe each of them) combine their result, sort it by some criteria end return.

I thought of strategy pattern, but as far as I know it goes like :

$obj = new Context();
$obj->setStrategy(new ConcreteStrategy1);
$obj->getStrategy()->task(); // => „Strategy 1”
$obj->setStrategy(new ConcreteStrategy2);
$obj->getStrategy()->task(); // => „Strategy 2”

Each time I want to call another class I have to change it manually. That will leave me using foreach on some array of classes (strategies) names. Is that the right way to go? Also please help me find better title for that question because that one is misleading.

Aucun commentaire:

Enregistrer un commentaire