Design Patterns PHP - Mediator Java

1) What have we done?

We created Mediator object, in Client we can see all relations between objects.

class Mediator
{
    /**
     * @var ComponentA
     */
    protected $componentA;

    /**
     * @var ComponentB
     */
    protected $componentB;

    /**
     * @var ComponentC
     */
    protected $componentC;

    public function doOne()
    {
        $this->componentA->doOne();
        $this->componentB->doOne();
        $this->componentC->doOne();
    }

    public function doTwo()
    {
        $this->componentB->doTwo();
        $this->componentA->doTwo();
        $this->componentC->doTwo();
    }

    /**
     * @param \ComponentA $componentA
     * @return \ComponentA
     */
    public function setComponentA($componentA)
    {
        $this->componentA = $componentA;

        return $this;
    }

    /**
     * @param \ComponentB $componentB
     * @return \ComponentB
     */
    public function setComponentB($componentB)
    {
        $this->componentB = $componentB;

        return $this;
    }

    /**
     * @param \ComponentC $componentC
     * @return \ComponentC
     */
    public function setComponentC($componentC)
    {
        $this->componentC = $componentC;

        return $this;
    }
}

$mediator = new Mediator();
$componentA = new ComponentA($mediator);
$componentB = new ComponentB($mediator);
$componentC = new ComponentC();
$mediator->setComponentA($componentA);
$mediator->setComponentB($componentB);
$mediator->setComponentC($componentC);




We set to Mediator all objects which will cooperate with each other

We inject "Mediator" to "ComponentA" and "ComponentB" in constructor(ComponentC doesn't have to Mediator, because clients don't call method from this object)

For "ComponentA" and "ComponentB" we create special methods: "handleOne" and "handleTwo" which will be called from clients.


class ComponentA
{
    /**
     * @var Mediator
     */
    protected $mediator;

    function __construct($mediator)
    {
        $this->mediator = $mediator;
    }

    public function handleOne()
    {
        $this->mediator->doOne();
    }

    public function doOne()
    {
        echo 'A doing task one' . PHP_EOL;
    }

    public function doTwo()
    {
        echo 'A doing task two' . PHP_EOL;
    }
}

class ComponentB
{
    /**
     * @var Mediator
     */
    protected $mediator;

    function __construct($mediator)
    {
        $this->mediator = $mediator;
    }

    public function handleTwo()
    {
        $this->mediator->doTwo();
    }

    public function doTwo()
    {
        echo 'B doing task two' . PHP_EOL;
    }

    public function doOne()
    {
        echo 'B doing task one' . PHP_EOL;
    }
}

class ComponentC
{
    public function doOne()
    {
        echo 'C doing task one' . PHP_EOL;
    }

    public function doTwo()
    {
        echo 'C doing task two' . PHP_EOL;
    }
}




And clients:

$mediator = new Mediator();
$componentA = new ComponentA($mediator);
$componentB = new ComponentB($mediator);
$componentC = new ComponentC();
$mediator->setComponentA($componentA);
$mediator->setComponentB($componentB);
$mediator->setComponentC($componentC);

if ($type == 'A')
    $componentA->handleOne();
else
    $componentB->handleTwo();




3) What are prons?

- Simplifying relations
- Much more simpler Components, method "doOne" and "doTwo" just do what required, nothing more calls from other components