Friday, August 22, 2014

Programming Design Pattern - Mediator Pattern Applied - Best Practise

Hi there!

Today i'm gonna show the Mediator design pattern in action. The Mediator design pattern is a very useful programming design pattern whenever you want to centralize logics making it maintainable when a lot of objects has to communicate together.

I'm assuming here you already know the concepts and i'll be focusing on practise. The example i will provide is a nice way to show it how it could looks like. You can always come back here, take it, adapt it and use it in your applictions as you may need. So be sure you bookmark it or join the group here on the right side of this post subscribing it.

First of all, let's take a look at the UML diagram of it. After that we will take the analogy for our example.

The UML Diagram of the Mediator Pattern


Pay close attention, because once you understand that, everything will become clear and simple to understand. That's the reason I'm putting always the UML first. That way you'll get an eye for it with the time.





The example

In our example we will see, how we could centralize the logic from a bunch of devices in a smart house making it better maintainable and decoupling the objects from each other.

The Events

In our example, we will be dealing with a lot of devices interacting with each other in a smart house. Those devices fire events. Let's define some events for each of them. After that we will define the devices itself.

public enum Evento {
    TERMOMETRO, METEOROLOGIA, LIXO, DISPERTADOR, 
    AGENDA, MAQUINA_CAFE, AR_CONDICIONADO,BORRIFADOR_PLANTAS;
}
public interface Occurrence {
    void ocorrencia(Evento evento, Object origin);
}

The Devices to mediate

In our example, we have a lot of devices in our house to mediate. Let's define some.

public abstract class Device{
    private Occurrence occurrence;
    public Device(Occurrence centralControl) {
        this.occurrence = centralControl;
    }
    public void inform(Evento evento) {
        occurrence.ocorrencia(evento, this);
    }
    public abstract void update();
}
public class Agenda extends Device{

    public Agenda(Occurrence ocorrencia) {
        super(ocorrencia);
    }
    public void exameMedico(){
        inform(Evento.AGENDA);
    }
    @Override
    public void update() {
        System.out.println("Visitando médico...!");
    }

}
public class ArCondicionado extends Device{
    public ArCondicionado(Occurrence ocorrencia) {
        super(ocorrencia);
    }
    public void fazerManutencao(){
        inform(Evento.AR_CONDICIONADO);
    }
    @Override
    public void update() {
        System.out.println("Manutenção feita com sucesso!");
    }
}
public class BorrifadorPlantas extends Device{
    public BorrifadorPlantas(Occurrence ocorrencia) {
        super(ocorrencia);
    }
    public void faltandoAgua(){
        inform(Evento.BORRIFADOR_PLANTAS);
    }
    @Override
    public void update() {
        System.out.println("Registro aberto. Pode borrifar!");
    }
}
public class Dispertador extends Device {

    public Dispertador(Occurrence ocorrencia) {
        super(ocorrencia);
    }

    public void horaDeTrabalhar() {
        inform(Evento.DISPERTADOR);
    }

    @Override
    public void update() {
        System.out.println("Acorrrrrdaaaaaa! Vai trabalhar menino!!!");
    }
}
public class MaquinaCafe extends Device{

    public MaquinaCafe(Occurrence ocorrencia) {
        super(ocorrencia);
    }
    public void cofreMoedasCheio(){
        inform(Evento.MAQUINA_CAFE);
    }
    @Override
    public void update() {
        System.out.println("Cofre esvaziado! Maquina is ready!");
    }

}
public class Meteorologia extends Device{

    public Meteorologia(Occurrence ocorrencia) {
        super(ocorrencia);
    }
    
    public void alertaDeTempestade(){
        inform(Evento.METEOROLOGIA);
    }
    @Override
    public void update() {
        System.out.println("Fique em casa! Tornado se aproximando!");
    }

}
public class RecolhaLixo extends Device{

    public RecolhaLixo(Occurrence ocorrencia) {
        super(ocorrencia);
    }
    public void colocarLixoPraFora(){
        inform(Evento.LIXO);
    }
    @Override
    public void update() {
        System.out.println("O caminhão de lixo irá passar daqui a pouco!");
    }

}

The Mediator

This class will be mediating between the devices in the house. It is like the CPU in the computer if you like the analogy. It centralizes the logic in one place.

public class CentralControle implements Occurrence {
    @Override
    public void ocorrencia(Evento evento, Object origin) {
        switch (evento) {
        case AGENDA:((Agenda)origin).update();break;
        case AR_CONDICIONADO:
            // FAZER MANUTENÇÃO...
            // ...ATUALIZAR APARELHO
            ((ArCondicionado)origin).update();
            break;
        case BORRIFADOR_PLANTAS:((BorrifadorPlantas)origin).update();break;
        case DISPERTADOR:((Dispertador)origin).update();break;
        case LIXO:((RecolhaLixo)origin).update();break;
        case MAQUINA_CAFE:((MaquinaCafe)origin).update();break;
        case METEOROLOGIA:((Meteorologia)origin).update();break;
        case TERMOMETRO:((Termometro)origin).update();break;
        default:break;}
    }
}

That's all! Hope you like it!

😱👇 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO 😱👇

Be sure to read, it will change your life!
Show your work by Austin Kleonhttps://amzn.to/34NVmwx

This book is a must read - it will put you in another level! (Expert)
Agile Software Development, Principles, Patterns, and Practiceshttps://amzn.to/30WQSm2

Write cleaner code and stand out!
Clean Code - A Handbook of Agile Software Craftsmanship: https://amzn.to/33RvaSv

This book is very practical, straightforward and to the point! Worth every penny!
Kotlin for Android App Development (Developer's Library): https://amzn.to/33VZ6gp

Needless to say, these are top right?
Apple AirPods Pro: https://amzn.to/2GOICxy

😱👆 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO 😱👆