Design Patterns PHP - Mediator Java

1) What have at the beginning?

Let's consider three components:

ComponentA
ComponentB
ComponentC

class ComponentA
{
    /**
     * @var ComponentB
     */
    protected $componentB;

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

    function __construct($componentB, $componentC)
    {
        $this->componentB = $componentB;
        $this->componentC = $componentC;
    }

    public function doOne()
    {
        echo 'A doing task one' . PHP_EOL;
        $this->componentB->doOne();
        $this->componentC->doOne();
    }

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

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

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

    public function doTwo()
    {
        echo 'B doing task two' . PHP_EOL;
        $this->componentA->doTwo();
        $this->componentC->doTwo();
    }

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

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

        return $this;
    }

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

        return $this;
    }
}

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

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




All components are related to each other. Injecting relations we can see in the following code:

php main.php --type=A
php main.php --type=B

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

if ($type == 'A')
    $componentA->doOne();
else
    $componentB->doTwo();




Dependent of parameter "type" we call call "doOne" from "ComponentA" or "doTwo" from "ComponentB"

2) What we can observe?

We have quite an unclear flow control between components.
We can see that it's not easy to catch whats going on. Both Components "A" and "B" depends of ourselves.

4) How we can improve this?

We can apply Mediator pattern.

This pattern is about to create addition object "Mediator" which will be responsible for relations between components and flow control between them.

Components will be related only with Mediator, not with each other.

Let's see whats going on in next step.