File tree
Expand file treeCollapse file tree3 files changed
+140
-0
lines changed patternBonus/src/me/premaseem/facade
Expand file treeCollapse file tree3 files changed
+140
-0
lines changed Original file line number | Diff line number | Diff line change |
---|
|
| 1 | +package me.premaseem.facade; |
| 2 | + |
| 3 | +import java.util.Scanner; |
| 4 | + |
| 5 | +public class ClientForEntertainmentUnitFacade { |
| 6 | + |
| 7 | +public static void main (String[] args) { |
| 8 | +Scanner scan = new Scanner(System.in); |
| 9 | +int repeatRunFlag = 1; |
| 10 | + |
| 11 | + |
| 12 | +System.out.println("This is Facade Pattern example which provides a simple interface " + |
| 13 | +"for the client to play a movie me entertainment unit " + |
| 14 | +"(simplifies the setup process of entertainment unit) "); |
| 15 | + |
| 16 | +EntertainmentFacade entertainmentFacade = new EntertainmentFacade(); |
| 17 | +while (repeatRunFlag == 1) { |
| 18 | +System.out.println("What would you like to do with your entertainment unit today "); |
| 19 | +System.out.println(" Press 1 for movie"); |
| 20 | +System.out.println(" Press 2 for music"); |
| 21 | +System.out.println(" Press 3 for game "); |
| 22 | +int entType = scan.nextInt(); |
| 23 | +System.out.println("Please enter the name "); |
| 24 | +String name = scan.next(); |
| 25 | + |
| 26 | + |
| 27 | +switch (entType) { |
| 28 | +case 1: |
| 29 | +entertainmentFacade.playMovie(name); |
| 30 | +break; |
| 31 | +case 2: |
| 32 | +entertainmentFacade.playMusic(name); |
| 33 | +break; |
| 34 | +case 3: |
| 35 | +entertainmentFacade.playGame(name); |
| 36 | +break; |
| 37 | +} |
| 38 | + |
| 39 | +System.out.println("Press 1 for more entertainment and 0 for EXIT .... "); |
| 40 | +try { |
| 41 | +repeatRunFlag = scan.nextInt(); |
| 42 | +} catch (Exception e) { |
| 43 | +repeatRunFlag = 0; |
| 44 | +} finally { |
| 45 | +entertainmentFacade.masterPowerOff(); |
| 46 | +} |
| 47 | + |
| 48 | +} |
| 49 | +} |
| 50 | +} |
Original file line number | Diff line number | Diff line change |
---|
|
| 1 | +package me.premaseem.facade; |
| 2 | + |
| 3 | +public class EntertainmentDevice { |
| 4 | + |
| 5 | +} |
| 6 | + |
| 7 | +class Nexus { |
| 8 | +public void downloadmedia(String name) { |
| 9 | +System.out.println("Searching the media "); |
| 10 | +System.out.println("Making online payment "); |
| 11 | +System.out.println("downloaded from Nexus " + name); |
| 12 | +} |
| 13 | +} |
| 14 | + |
| 15 | +class Amplifier { |
| 16 | + |
| 17 | +void powerOn() { |
| 18 | +System.out.println("Power on Amplifier"); |
| 19 | +} |
| 20 | + |
| 21 | +void powerOff() { |
| 22 | +System.out.println("power Off Amplifer "); |
| 23 | +} |
| 24 | + |
| 25 | +void attachAmplifierForMusic() { |
| 26 | +System.out.println("Attaching music amplification"); |
| 27 | +} |
| 28 | + |
| 29 | +void attachAmplifierForHomeTheater() { |
| 30 | +System.out.println("Attaching movie amplification "); |
| 31 | +} |
| 32 | +} |
| 33 | + |
| 34 | +class Projector { |
| 35 | + |
| 36 | +void powerOn() { |
| 37 | +System.out.println("Power on Projector"); |
| 38 | +} |
| 39 | + |
| 40 | +void powerOff() { |
| 41 | +System.out.println("power Off Projector "); |
| 42 | +} |
| 43 | + |
| 44 | +void adjustProjectorForMovie() { |
| 45 | +System.out.println("Attaching home theater mode"); |
| 46 | +} |
| 47 | + |
| 48 | +void adjustProjectorForGame() { |
| 49 | +System.out.println("Attaching game console "); |
| 50 | +} |
| 51 | +} |
Original file line number | Diff line number | Diff line change |
---|
|
| 1 | +package me.premaseem.facade; |
| 2 | + |
| 3 | +public class EntertainmentFacade { |
| 4 | + |
| 5 | +Nexus nexus = new Nexus(); |
| 6 | +Amplifier amplifier = new Amplifier(); |
| 7 | +Projector projector = new Projector(); |
| 8 | + |
| 9 | +public void playMovie(String name) { |
| 10 | +nexus.downloadmedia(name); |
| 11 | +masterPowerOn(); |
| 12 | +amplifier.attachAmplifierForHomeTheater(); |
| 13 | +projector.adjustProjectorForMovie(); |
| 14 | +} |
| 15 | + |
| 16 | +public void playMusic(String name) { |
| 17 | +nexus.downloadmedia(name); |
| 18 | +amplifier.powerOn(); |
| 19 | +amplifier.attachAmplifierForMusic(); |
| 20 | +} |
| 21 | + |
| 22 | +public void playGame(String name) { |
| 23 | +nexus.downloadmedia(name); |
| 24 | +masterPowerOn(); |
| 25 | +amplifier.attachAmplifierForMusic(); |
| 26 | +projector.adjustProjectorForGame(); |
| 27 | +} |
| 28 | + |
| 29 | +public void masterPowerOff() { |
| 30 | +amplifier.powerOff(); |
| 31 | +projector.powerOff(); |
| 32 | +} |
| 33 | + |
| 34 | +public void masterPowerOn() { |
| 35 | +amplifier.powerOn(); |
| 36 | +projector.powerOn(); |
| 37 | +} |
| 38 | + |
| 39 | +} |
You can’t perform that action at this time.
0 commit comments