I have a set of classes that all share the same interface. I'd like to take existing objects of those classes and run the same set of methods whenever they are created in specific contexts. Something like:
function context1()
{
$foo = new FooA();
$foo->addBar();
$foo->addBaz();
// Now do stuff with $foo
}
function context2()
{
$foo = new FooB();
$foo->addBar();
$foo->addBaz();
// Now do stuff with $foo
}
function context3()
{
$foo = new FooC();
// Context 3 doesn't need setup, just do stuff with $foo
}
My first thought was to use a builder, but the examples I've seen of the builder pattern either include it as an inner class or the builder itself instantiates the object it will build off of. Something like:
class FooBuilder
{
private $foo;
public function __construct()
{
$this->foo = new Foo();
}
public function setup()
{
$this->foo->addBar();
$this->foo->addBaz();
}
public function getFoo()
{
return $this->foo;
}
}
But I'd like to use the builder on existing objects:
public function __construct(FooInterface $foo)
{
$this->foo = $foo;
}
Is a builder still appropriate in this case, or should I be using a different pattern?
Aucun commentaire:
Enregistrer un commentaire