一、基本介绍

image.png

二、UML类图

image.png

  1. 外观类(Facade):为调用端提供统一的调用接口,外观类知道哪些子系统负责处理请求,从而将调用端的请求代理给适当子系统对下那个
  2. 调用者(Client):外观接口的调用者
  3. 子系统的集合:指模块或者子系统,处理Facade对象指派的任务,他是功能的提供者

    三、代码

    DVD类

    1. public class DVDPlayer {
    2. //使用单例模式, 使用饿汉式
    3. private static DVDPlayer instance = new DVDPlayer();
    4. public static DVDPlayer getInstanc() {
    5. return instance;
    6. }
    7. public void on() {
    8. System.out.println(" dvd on ");
    9. }
    10. public void off() {
    11. System.out.println(" dvd off ");
    12. }
    13. public void play() {
    14. System.out.println(" dvd is playing ");
    15. }
    16. //....
    17. public void pause() {
    18. System.out.println(" dvd pause ..");
    19. }
    20. }

    爆米花类

    1. public class Popcorn {
    2. private static Popcorn instance = new Popcorn();
    3. public static Popcorn getInstance() {
    4. return instance;
    5. }
    6. public void on() {
    7. System.out.println(" popcorn on ");
    8. }
    9. public void off() {
    10. System.out.println(" popcorn ff ");
    11. }
    12. public void pop() {
    13. System.out.println(" popcorn is poping ");
    14. }
    15. }

    投影仪类

    1. public class Projector {
    2. private static Projector instance = new Projector();
    3. public static Projector getInstance() {
    4. return instance;
    5. }
    6. public void on() {
    7. System.out.println(" Projector on ");
    8. }
    9. public void off() {
    10. System.out.println(" Projector ff ");
    11. }
    12. public void focus() {
    13. System.out.println(" Projector is Projector ");
    14. }
    15. //...
    16. }

    外观类

    ```java public class HomeTheaterFacade {

    //定义各个子系统对象 private TheaterLight theaterLight; private Popcorn popcorn; private Stereo stereo; private Projector projector; private Screen screen; private DVDPlayer dVDPlayer;

  1. //构造器
  2. public HomeTheaterFacade() {
  3. super();
  4. this.theaterLight = TheaterLight.getInstance();
  5. this.popcorn = Popcorn.getInstance();
  6. this.stereo = Stereo.getInstance();
  7. this.projector = Projector.getInstance();
  8. this.screen = Screen.getInstance();
  9. this.dVDPlayer = DVDPlayer.getInstanc();
  10. }
  11. //操作分成 4 步
  12. public void ready() {
  13. popcorn.on();
  14. popcorn.pop();
  15. screen.down();
  16. projector.on();
  17. stereo.on();
  18. dVDPlayer.on();
  19. theaterLight.dim();
  20. }
  21. public void play() {
  22. dVDPlayer.play();
  23. }
  24. public void pause() {
  25. dVDPlayer.pause();
  26. }
  27. public void end() {
  28. popcorn.off();
  29. theaterLight.bright();
  30. screen.up();
  31. projector.off();
  32. stereo.off();
  33. dVDPlayer.off();
  34. }

}

  1. <a name="SJ8Kx"></a>
  2. #### 客户端测试
  3. ```java
  4. public class Client {
  5. public static void main(String[] args) {
  6. // TODO Auto-generated method stub
  7. //这里直接调用。。 很麻烦
  8. HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade();
  9. homeTheaterFacade.ready();
  10. homeTheaterFacade.play();
  11. homeTheaterFacade.end();
  12. }
  13. }

四、注意事项

image.png