文章目录
  1. 1. 简单介绍
  2. 2. UML图
  3. 3. 实例
  4. 4. 输出
  5. 5. 效果
  6. 6. 应用场景

信息隐藏是指模块隐藏内部的设计决策,下面我就去学习一些尊崇这个原则的设计模式–外观模式(Facade Pattern)

简单介绍

为子系统中的一组接口提供一个统一接口。Facade模式定义了一个高层接口,这个接口使得这子系统更容易使用。

UML图

实例

  1. HomeTheaterFacade类

    public class HomeTheaterFacade {
        private Amplifier amplifier;
        private CDPlayer cdPlayer;
        private DVDPlayer dvdPlayer;
        private Projector projector;
        private Screen screen;
        private Tuner tuner;
    
        public HomeTheaterFacade(Amplifier amplifier, CDPlayer cdPlayer,
                DVDPlayer dvdPlayer, Projector projector, Screen screen, Tuner tuner) {
            super();
            this.amplifier = amplifier;
            this.cdPlayer = cdPlayer;
            this.dvdPlayer = dvdPlayer;
            this.projector = projector;
            this.screen = screen;
            this.tuner = tuner;
        }
    
        public void on(){
            System.out.println("Now,Let's movie!");
            projector.on();
            tuner.on();
            amplifier.on();
            cdPlayer.on();
            dvdPlayer.on();
            screen.on();
            System.out.println("HaHa,所有的设备都打开啦,快点享受你的电影吧!");
        }
    }
    
  2. Amplifier类

    public class Amplifier {
        public void on(){
            System.out.println("Amplifier is on!");
        }
    
    }
    
  3. CDPlayer类

    public class CDPlayer {
        public void on(){
            System.out.println("CDPlayer is on!");
        }
    }
    
  4. DVDPlayer类

    public class DVDPlayer {
        public void on(){
            System.out.println("DVDPlayer is on!");
        }
    }
    
  5. Projector类

    public class Projector {
        public void on(){
            System.out.println("Projector is on!");
        }
    }
    
  6. Screen类

    public class Screen {
        public void on(){
            System.out.println("Screen is on!");
        }
    }
    
  7. Tuner类

    public class Tuner {
        public void on(){
            System.out.println("Tuner is on!");
        }
    }
    
  8. Test类

    public class Test {
        public static void main(String arg[]) {
            HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade(new Amplifier(), new CDPlayer(), 
                    new DVDPlayer(), new Projector(), new Screen(), new Tuner());
            homeTheaterFacade.on();
        }
    }    
    

输出

Now,Let's movie!
Projector is on!
Tuner is on!
Amplifier is on!
CDPlayer is on!
DVDPlayer is on!
Screen is on!
HaHa,所有的设备都打开啦,快点享受你的电影吧!

效果

信息隐藏

应用场景

  1. 为一个复杂子系统提供一个简单接口。
  2. 提高子系统的独立性。
  3. 在层次化结构中,可以使用Facade模式定义系统中每一层的入口。
文章目录
  1. 1. 简单介绍
  2. 2. UML图
  3. 3. 实例
  4. 4. 输出
  5. 5. 效果
  6. 6. 应用场景