diff --git a/4_3 Facade class diagram.jpeg b/4_3 Facade class diagram.jpeg new file mode 100644 index 0000000..65cfd85 Binary files /dev/null and b/4_3 Facade class diagram.jpeg differ diff --git a/4_3 facade sequence.png b/4_3 facade sequence.png new file mode 100644 index 0000000..4ec4172 Binary files /dev/null and b/4_3 facade sequence.png differ diff --git a/README.md b/README.md index 9e0034c..a6884e2 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/diagrams/4_3 Facade class diagram.jpeg b/diagrams/4_3 Facade class diagram.jpeg new file mode 100644 index 0000000..65cfd85 Binary files /dev/null and b/diagrams/4_3 Facade class diagram.jpeg differ diff --git a/diagrams/4_3 facade sequence.png b/diagrams/4_3 facade sequence.png new file mode 100644 index 0000000..4ec4172 Binary files /dev/null and b/diagrams/4_3 facade sequence.png differ diff --git a/pattern/src/com/premaseem/Client.java b/pattern/src/com/premaseem/Client.java deleted file mode 100644 index 15f05df..0000000 --- a/pattern/src/com/premaseem/Client.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.premaseem; - -/* -@author: Aseem Jain -@title: Design Patterns with Java 9 -@link: https://premaseem.wordpress.com/category/computers/design-patterns/ -@copyright: 2018 Packt Publication -*/ -public class Client { - public static void main (String[] args) { - System.out.println("Singleton cook example "); - } -} diff --git a/pattern/src/me/premaseem/Client.java b/pattern/src/me/premaseem/Client.java new file mode 100644 index 0000000..7252469 --- /dev/null +++ b/pattern/src/me/premaseem/Client.java @@ -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(); + } +} diff --git a/pattern/src/me/premaseem/remotes/MasterRemoteFacade.java b/pattern/src/me/premaseem/remotes/MasterRemoteFacade.java new file mode 100644 index 0000000..e2361a2 --- /dev/null +++ b/pattern/src/me/premaseem/remotes/MasterRemoteFacade.java @@ -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(); + } +} diff --git a/pattern/src/me/premaseem/remotes/SetTopBoxRemote.java b/pattern/src/me/premaseem/remotes/SetTopBoxRemote.java new file mode 100644 index 0000000..0e25497 --- /dev/null +++ b/pattern/src/me/premaseem/remotes/SetTopBoxRemote.java @@ -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 "); + } +} diff --git a/pattern/src/me/premaseem/remotes/SoundSystemRemote.java b/pattern/src/me/premaseem/remotes/SoundSystemRemote.java new file mode 100644 index 0000000..bd40ddd --- /dev/null +++ b/pattern/src/me/premaseem/remotes/SoundSystemRemote.java @@ -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 "); + } +} diff --git a/pattern/src/me/premaseem/remotes/TVRemote.java b/pattern/src/me/premaseem/remotes/TVRemote.java new file mode 100644 index 0000000..19d8850 --- /dev/null +++ b/pattern/src/me/premaseem/remotes/TVRemote.java @@ -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 "); + } +} diff --git a/patternBonus/src/com/premaseem/Client.java b/patternBonus/src/com/premaseem/Client.java deleted file mode 100644 index 15f05df..0000000 --- a/patternBonus/src/com/premaseem/Client.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.premaseem; - -/* -@author: Aseem Jain -@title: Design Patterns with Java 9 -@link: https://premaseem.wordpress.com/category/computers/design-patterns/ -@copyright: 2018 Packt Publication -*/ -public class Client { - public static void main (String[] args) { - System.out.println("Singleton cook example "); - } -} diff --git a/patternBonus/src/me/premaseem/facade/ClientForEntertainmentUnitFacade.java b/patternBonus/src/me/premaseem/facade/ClientForEntertainmentUnitFacade.java new file mode 100644 index 0000000..5557543 --- /dev/null +++ b/patternBonus/src/me/premaseem/facade/ClientForEntertainmentUnitFacade.java @@ -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(); + } + + } + } +} diff --git a/patternBonus/src/me/premaseem/facade/EntertainmentDevice.java b/patternBonus/src/me/premaseem/facade/EntertainmentDevice.java new file mode 100644 index 0000000..e78d56b --- /dev/null +++ b/patternBonus/src/me/premaseem/facade/EntertainmentDevice.java @@ -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 "); + } +} diff --git a/patternBonus/src/me/premaseem/facade/EntertainmentFacade.java b/patternBonus/src/me/premaseem/facade/EntertainmentFacade.java new file mode 100644 index 0000000..9091946 --- /dev/null +++ b/patternBonus/src/me/premaseem/facade/EntertainmentFacade.java @@ -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(); + } + +}