Design Patterns Java - Mediator Php

1) What have we done?

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

public class Mediator
{
    ComponentA componentA;
    ComponentB componentB;
    ComponentC componentC;

    public void doOne()
    {
        componentA.doOne();
        componentB.doOne();
        componentC.doOne();
    }

    public void doTwo()
    {
        componentB.doTwo();
        componentA.doTwo();
        componentC.doTwo();
    }

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

    public void setComponentB(ComponentB componentB)
    {
        this.componentB = componentB;
    }

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

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

        //some code and later invoking

        componentA.handleOne();
        componentB.handleTwo();
    }
}




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.


public class ComponentA
{
    private Mediator mediator;

    public ComponentA(Mediator mediator)
    {
        this.mediator = mediator;
    }

    public void handleOne()
    {
        this.mediator.doOne();
    }

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

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

public class ComponentB
{
    private Mediator mediator;

    public ComponentB(Mediator mediator)
    {
        this.mediator = mediator;
    }

    public void handleTwo()
    {
        this.mediator.doTwo();
    }

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

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

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

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




And clients:

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

        //some code and later invoking

        componentA.handleOne();
        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