代码实战
package com.atguigu.facade;
public class DVDPlayer {
private static DVDPlayer instance = new DVDPlayer();
public static DVDPlayer getInstance() {
return instance;
}
public void on() {
System.out.println("DVD ON");
}
public void off() {
System.out.println("DVD off");
}
public void play() {
System.out.println("DVD play");
}
public void pause() {
System.out.println("DVD pause");
}
}
package com.atguigu.facade;
public class PopCorn {
private static PopCorn instance = new PopCorn();
public static PopCorn getInstance() {
return instance;
}
public void on() {
System.out.println("PopCorn ON");
}
public void off() {
System.out.println("PopCorn off");
}
public void play() {
System.out.println("PopCorn play");
}
public void pause() {
System.out.println("PopCorn pause");
}
}
package com.atguigu.facade;
public class Projector {
private static Projector instance = new Projector();
public static Projector getInstance() {
return instance;
}
public void on() {
System.out.println("Projector ON");
}
public void off() {
System.out.println("Projector off");
}
public void play() {
System.out.println("Projector play");
}
public void pause() {
System.out.println("Projector pause");
}
public void focus() {
System.out.println("聚焦 pause");
}
}
package com.atguigu.facade;
public class Screen {
private static Screen instance = new Screen();
public static Screen getInstance() {
return instance;
}
public void up() {
System.out.println("Screen up");
}
public void down() {
System.out.println("Screen down");
}
}
package com.atguigu.facade;
public class Stereo {
private static Stereo instance = new Stereo();
public static Stereo getInstance() {
return instance;
}
public void on() {
System.out.println("Stereo on");
}
public void off() {
System.out.println("Stereo off");
}
public void up() {
System.out.println("Stereo up");
}
}
package com.atguigu.facade;
public class TheaterLigth {
private static TheaterLigth instance = new TheaterLigth();
public static TheaterLigth getInstance() {
return instance;
}
public void on() {
System.out.println("TheaterLigth on");
}
public void dim() {
System.out.println("TheaterLigth dim");
}
public void bright() {
System.out.println("TheaterLigth bright");
}
}
关键的部分你来了
package com.atguigu.facade;
public class HomeTheaterFacade {
private TheaterLigth theaterLigth;
private PopCorn popCorn;
private Stereo stereo;
private Projector projector;
private Screen screen;
private DVDPlayer dvdPlayer;
public HomeTheaterFacade() {
super();
this.theaterLigth = TheaterLigth.getInstance();
this.popCorn = PopCorn.getInstance();
this.stereo = Stereo.getInstance();
this.projector = Projector.getInstance();
this.screen = Screen.getInstance();
this.dvdPlayer = DVDPlayer.getInstance();
}
public void ready() {
popCorn.on();
popCorn.play();
screen.down();
projector.on();
stereo.on();
dvdPlayer.on();
theaterLigth.dim();
}
public void play() {
dvdPlayer.play();
}
public void pause() {
dvdPlayer.pause();
}
public void end() {
popCorn.off();
theaterLigth.bright();
screen.up();
projector.off();
stereo.off();
dvdPlayer.off();
}
}
package com.atguigu.facade;
public class Client {
public static void main(String[] args) {
HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade();
homeTheaterFacade.ready();
homeTheaterFacade.play();
homeTheaterFacade.pause();
homeTheaterFacade.end();
}
}