Design Patterns Java - Mediator Php

1) What have at the beginning?

Let's consider three components:

ComponentA
ComponentB
ComponentC

public class ComponentA
{
    private ComponentB componentB;

    private ComponentC componentC;

    public ComponentA(
            ComponentB componentB,
            ComponentC componentC)
    {
        this.componentB = componentB;
        this.componentC = componentC;
    }

    public void doOne()
    {
        System.out.println("A doing task one");
        componentB.doOne();
        componentC.doOne();
    }

    public void doTwo()
    {
        System.out.println("A doing task two");
    }
}

public class ComponentB
{
    ComponentA componentA;

    ComponentC componentC;

    public void doTwo()
    {
        System.out.println("B doing task two");
        componentA.doTwo();
        componentC.doTwo();
    }

    public void doOne()
    {
        System.out.println("B doing task one");
    }

    public void setComponentA(ComponentA componentA)
    {
        this.componentA = componentA;
    }

    public void setComponentC(ComponentC componentC)
    {
        this.componentC = componentC;
    }
}

public class ComponentC
{
    public void doOne()
    {
        System.out.println("C doing task one");
    }

    public void doTwo()
    {
        System.out.println("C doing task two");
    }
}




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

public class Client
{
    public static void main(String[] args)
    {
        // relations
        ComponentB componentB = new ComponentB();
        ComponentC componentC = new ComponentC();
        ComponentA componentA = new ComponentA(componentB, componentC);
        componentB.setComponentA(componentA);
        componentB.setComponentC(componentC);

        //some code and later invoking
        componentA.doOne();
        componentB.doTwo();
    }
}




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 each other.

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.