Follow treslines by email clicking Here!

Wednesday, August 20, 2014

Programming Design Pattern - Facade Pattern Applied - Best Practise

Hi there!

Today i'm gonna show the Facade design pattern in action. The Facade design pattern is a very useful programming design pattern when you are trying to simplify a lot of communication and actions between a bunch of classes or objects.

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 Facade 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 simplify the turn ON and OFF process of a stereo home cinema. Imagine you bought a stereo home cinema. Instead of connecting all your components every time you watch a movie, we will simplify this process by using a facade. 

The components

Typically, the facade coordinates the communication between lots of objects making it simple to use it. For that reason, let´s define a lot of components that communicates together to simulate a situation like that. What we have here are home cinema devices.

public class Light {
    public void on(){System.out.println("light on");}
    public void off(){System.out.println("light off");}
    public void dim(){System.out.println("dimming lights down");}
}
public class WatchScreen {
    public void down(){System.out.println("Sliding screen down");}
    public void high(){System.out.println("Sliding screen high");}
}
public class PopcornMaschine {
    public void on(){System.out.println("popcorn maschine on");}
    public void off(){System.out.println("popcorn maschine off");}
    public void start(){System.out.println("preparing popcorn...");}
}
public class CdPlayer {
    private Amplifier amplifier;
    public void on(){System.out.println("CD Player on!");}
    public void off(){System.out.println("CD Player off!");}
    public void eject(){System.out.println("Eject");}
    public void pause(){System.out.println("Pause");}
    public void play(){System.out.println("play");}
    public void stop(){System.out.println("stop");}
}
public class Tuner {
    private Amplifier amplifier;
    public void on(){System.out.println("Tuner on!");}
    public void off(){System.out.println("Tuner off!");}
    public void setFM(){System.out.println("FM set");}
    public void setAM(){System.out.println("AM set");}
    public void setChannel(double frequency){System.out.println("Channel: "+frequency);}
}
public class DvdPlayer {
    public void on(){System.out.println("DVD Player on!");}
    public void off(){System.out.println("DVD Playerner off!");}
    public void eject(){System.out.println("Eject");}
    public void pause(){System.out.println("Pause");}
    public void play(){System.out.println("play");}
    public void stop(){System.out.println("stop");}
    public void setSurroundSound(){System.out.println("Surround Sound mode");}
    public void setTwoChannelSound(){System.out.println("Two channel sound mode");}
}
public class Amplifier {
    private Tuner tuner;
    private DvdPlayer dvdPlayer;
    private CdPlayer cdPlayer;
    
    public void on(){System.out.println("Amplifier on!");}
    public void off(){System.out.println("Amplifier off!");}
    public void setCD(){System.out.println("setting CD");}
    public void setDVD(){System.out.println("setting DVD");}
    public void setStereoSound(){System.out.println("setting stereo sound");}
    public void setSurroundSound(){System.out.println("settting surround sound");}
    public void setTuner(){System.out.println("setting Tuner");}
    public void setVolume(){System.out.println("setting volume");}
    
}
public class Beamer {
    private DvdPlayer dvdPlayer;
    public void on(){System.out.println("Projetor on");}
    public void off(){System.out.println("Projetor off");}
    public void tvMode(){System.out.println("TV mode");}
    public void wideScreenMode(){System.out.println("Wide screen mode");}
}

The Facade

Instead of switching all devices ON and OFF (in the right sequences) we use the facade. All we have to do now, is to call start and stop watching movie. All needed communication and actions behind the scene will be handled by the facade itself. We don't care about it anymore.

public class HomeCinemaFacade {
    private Amplifier amplifier;
    private Tuner tuner;
    private DvdPlayer dvdPlayer;
    private CdPlayer cdPlayer;
    private Beamer beamer;
    private Light light;
    private PopcornMaschine popcorn;
    private WatchScreen screen;
    
    public HomeCinemaFacade(Amplifier amplifier, Tuner tuner, DvdPlayer dvdPlayer,
            CdPlayer cdPlayer, Beamer beamer, Light light, PopcornMaschine popcorn,
            WatchScreen screen) {
        super();
        this.amplifier = amplifier;
        this.tuner = tuner;
        this.dvdPlayer = dvdPlayer;
        this.cdPlayer = cdPlayer;
        this.beamer = beamer;
        this.light = light;
        this.popcorn = popcorn;
        this.screen = screen;
    }
    
    public void startWatchingMovie(){
        System.out.println("Se prepare! O filme ja vai come├žar!");
        popcorn.on();
        popcorn.start();
        light.dim();
        screen.down();
        beamer.on();
        beamer.wideScreenMode();
        amplifier.on();
        amplifier.setDVD();
        amplifier.setSurroundSound();
        amplifier.setVolume();
        dvdPlayer.on();
        dvdPlayer.play();
    }
    public void stopWatchingMovie(){
        System.out.println("Desligando Home-Cinema! aguarde...");
        popcorn.off();
        light.on();
        screen.high();
        beamer.off();
        amplifier.off();
        dvdPlayer.pause();
        dvdPlayer.eject();
        dvdPlayer.off();
    }
}

The Test

Finally, let´s watch some movies using our new facade. No more complicated turn ON and OFF processes anymore ;)

public class Client {
    public static void main(String[] args) {
        final HomeCinemaFacade homeCinemaFacade = new HomeCinemaFacade(new Amplifier(),
                new Tuner(), new DvdPlayer(), new CdPlayer(), new Beamer(), new Light(),
                new PopcornMaschine(), new WatchScreen());
        // SIMULATING START OF A MOVIE
        homeCinemaFacade.startWatchingMovie();
    }
}

That's all! Hope you like it!

No comments:

Post a Comment