Skip to content

Facade Design pattern #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added 4_3 Facade class diagram.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 4_3 facade sequence.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,31 @@
# DesignPatternsJava9
This repo consists Gang of Four Design patterns code on Java 9. Each branch in the repository has code of 1 design pattern. Switch repository to try out different design patterns.
# What is Facade Design Pattern
* Facade pattern adds an interface to existing system or group of sub systems to hide its complexities
* This pattern involves a single class which provides interface with simplified methods required by client and delegates calls to methods of existing system classes.

## Diagram
![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/facade-pattern/diagrams/4_3%20Facade%20class%20diagram.jpeg "Diagram")

![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/facade-pattern/diagrams/4_3%20facade%20sequence.png "Diagram")

### When to use Facade Design Pattern
When application needs a simplified interface to the overall functionality of a complex subsystem.

### Learn Design Patterns with Java by Aseem Jain
This repository contains working project code used in video Course by Packt Publication with title "Learn Design Patterns with Java " authored by "Aseem Jain".

### Course link:
https://www.packtpub.com/application-development/learn-design-patterns-java-9-video

### ![ http://in.linkedin.com/in/premaseem](https://github.com/premaseem/DesignPatternsJava9/blob/master/linkedin.png "http://in.linkedin.com/in/premaseem") Profile: http://in.linkedin.com/in/premaseem

### Authors blog on design patterns:
https://premaseem.wordpress.com/category/computers/design-patterns/

### Software Design pattern community face book page:
https://www.facebook.com/DesignPatternGuru/

### Note:
* This code base will work on Java 9 and above versions.
* `diagrams` folders carry UML diagrams.
* `pattern` folder has code of primary example.
* `patternBonus` folder has code of secondary or bonus example.
Binary file added diagrams/4_3 Facade class diagram.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added diagrams/4_3 facade sequence.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 0 additions & 13 deletions pattern/src/com/premaseem/Client.java

This file was deleted.

34 changes: 34 additions & 0 deletions pattern/src/me/premaseem/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package me.premaseem;

import me.premaseem.remotes.MasterRemoteFacade;
import me.premaseem.remotes.SetTopBoxRemote;
import me.premaseem.remotes.SoundSystemRemote;
import me.premaseem.remotes.TVRemote;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/
public class Client {
public static void main (String[] args) {
System.out.println("Master Remote Facade ");

// decoupled systems from client and moved to facade
MasterRemoteFacade masterRemoteFacade = new MasterRemoteFacade();

// Facade simplified code and reduced complexity
masterRemoteFacade.turnOn();
masterRemoteFacade.turnOFF();

// // Turning ON requires several calls
// tvRemote.trunOn();
// soundSystemRemote.trunOn();
// setTopBoxRemote.trunOn();
//
// // Turning OFF requires several calls
// tvRemote.trunOn();
// soundSystemRemote.trunOn();
// setTopBoxRemote.trunOn();
}
}
38 changes: 38 additions & 0 deletions pattern/src/me/premaseem/remotes/MasterRemoteFacade.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package me.premaseem.remotes;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/
public class MasterRemoteFacade {

// sub system is de coupled from client
private TVRemote tvRemote;
private SoundSystemRemote soundSystemRemote;
private SetTopBoxRemote setTopBoxRemote;

public MasterRemoteFacade(){
tvRemote = new TVRemote();
soundSystemRemote = new SoundSystemRemote();
setTopBoxRemote = new SetTopBoxRemote();
}

// Master turn on takes care of all sub systems
public void turnOn(){
System.out.println();
System.out.println("Turning ON all sub systems");
tvRemote.trunOn();
soundSystemRemote.trunOn();
setTopBoxRemote.trunOn();
}

// Master turn off takes care of all sub systems
public void turnOFF(){
System.out.println();
System.out.println("Turning OFF all sub systems");
tvRemote.trunOff();
soundSystemRemote.trunOff();
setTopBoxRemote.trunOff();
}
}
17 changes: 17 additions & 0 deletions pattern/src/me/premaseem/remotes/SetTopBoxRemote.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package me.premaseem.remotes;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/
public class SetTopBoxRemote {

public void trunOn(){
System.out.println("Set Top box power turn ON ");
}

public void trunOff(){
System.out.println("Set Top box power turn OFF ");
}
}
17 changes: 17 additions & 0 deletions pattern/src/me/premaseem/remotes/SoundSystemRemote.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package me.premaseem.remotes;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/
public class SoundSystemRemote {

public void trunOn(){
System.out.println("Sound System power turn ON ");
}

public void trunOff(){
System.out.println("Sound System power turn OFF ");
}
}
17 changes: 17 additions & 0 deletions pattern/src/me/premaseem/remotes/TVRemote.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package me.premaseem.remotes;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/
public class TVRemote {

public void trunOn(){
System.out.println("TV power turn ON ");
}

public void trunOff(){
System.out.println("TV power turn OFF ");
}
}
13 changes: 0 additions & 13 deletions patternBonus/src/com/premaseem/Client.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package me.premaseem.facade;

import java.util.Scanner;

public class ClientForEntertainmentUnitFacade {

public static void main (String[] args) {
Scanner scan = new Scanner(System.in);
int repeatRunFlag = 1;


System.out.println("This is Facade Pattern example which provides a simple interface " +
"for the client to play a movie me entertainment unit " +
"(simplifies the setup process of entertainment unit) ");

EntertainmentFacade entertainmentFacade = new EntertainmentFacade();
while (repeatRunFlag == 1) {
System.out.println("What would you like to do with your entertainment unit today ");
System.out.println(" Press 1 for movie");
System.out.println(" Press 2 for music");
System.out.println(" Press 3 for game ");
int entType = scan.nextInt();
System.out.println("Please enter the name ");
String name = scan.next();


switch (entType) {
case 1:
entertainmentFacade.playMovie(name);
break;
case 2:
entertainmentFacade.playMusic(name);
break;
case 3:
entertainmentFacade.playGame(name);
break;
}

System.out.println("Press 1 for more entertainment and 0 for EXIT .... ");
try {
repeatRunFlag = scan.nextInt();
} catch (Exception e) {
repeatRunFlag = 0;
} finally {
entertainmentFacade.masterPowerOff();
}

}
}
}
51 changes: 51 additions & 0 deletions patternBonus/src/me/premaseem/facade/EntertainmentDevice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package me.premaseem.facade;

public class EntertainmentDevice {

}

class Nexus {
public void downloadmedia(String name) {
System.out.println("Searching the media ");
System.out.println("Making online payment ");
System.out.println("downloaded from Nexus " + name);
}
}

class Amplifier {

void powerOn() {
System.out.println("Power on Amplifier");
}

void powerOff() {
System.out.println("power Off Amplifer ");
}

void attachAmplifierForMusic() {
System.out.println("Attaching music amplification");
}

void attachAmplifierForHomeTheater() {
System.out.println("Attaching movie amplification ");
}
}

class Projector {

void powerOn() {
System.out.println("Power on Projector");
}

void powerOff() {
System.out.println("power Off Projector ");
}

void adjustProjectorForMovie() {
System.out.println("Attaching home theater mode");
}

void adjustProjectorForGame() {
System.out.println("Attaching game console ");
}
}
39 changes: 39 additions & 0 deletions patternBonus/src/me/premaseem/facade/EntertainmentFacade.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package me.premaseem.facade;

public class EntertainmentFacade {

Nexus nexus = new Nexus();
Amplifier amplifier = new Amplifier();
Projector projector = new Projector();

public void playMovie(String name) {
nexus.downloadmedia(name);
masterPowerOn();
amplifier.attachAmplifierForHomeTheater();
projector.adjustProjectorForMovie();
}

public void playMusic(String name) {
nexus.downloadmedia(name);
amplifier.powerOn();
amplifier.attachAmplifierForMusic();
}

public void playGame(String name) {
nexus.downloadmedia(name);
masterPowerOn();
amplifier.attachAmplifierForMusic();
projector.adjustProjectorForGame();
}

public void masterPowerOff() {
amplifier.powerOff();
projector.powerOff();
}

public void masterPowerOn() {
amplifier.powerOn();
projector.powerOn();
}

}