Head First设计模式(8):外观模式

更多的可以参考我的博客,也在陆续更新ing
http://www.hspweb.cn/

外观模式提供了一个统一的接口,用来访问子系统中的一群接口。外观定义了一个高层接口,让子系统更容易使用。

装饰者模式---不改变接口,但加入责任。
适配者模式---将一个接口转成另一个接口
外观模式---让接口更简单。

下面用书本例子:甜蜜的家庭影院说明。
观看电影流程:
1. 打开爆米花机
2. 开始爆米花
3. 将灯光调暗
4. 放下屏幕
5. 打开投影机
6. 将投影机的输入切换到DVD
7. 将投影机设置在宽屏模式
8. 打开功放
9. 将功放的输入设置为DVD
10. 将功放设置为环绕立体声
11. 将功放音量调中(5)
12. 打开DVD播放器
13. 开始播放DVD

1、目录
Head First设计模式(8):外观模式_第1张图片
image
2、package cd

①CdPlayer.java

package cd;
import sounds.Amplifier;

public class CdPlayer {
    String description;
    int currentTrack;
    Amplifier amplifier;
    String title;

    public CdPlayer(String description, Amplifier amplifier) {
        this.description = description;
        this.amplifier = amplifier;
    }

    public void on() {
        System.out.println(description + " 开");
    }

    public void off() {
        System.out.println(description + " 关");
    }

    public void eject() {
        title = null;
        System.out.println(description + " 弹出");
    }

    public void play(String title) {
        this.title = title;
        currentTrack = 0;
        System.out.println(description + " 播放 \"" + title + "\"");
    }

    public void play(int track) {
        if (title == null) {
            System.out.println(description + " 不能播放轨道 " + currentTrack + 
                    ", 没有放入CD");
        } else {
            currentTrack = track;
            System.out.println(description + " 播放轨道 " + currentTrack);
        }
    }

    public void stop() {
        currentTrack = 0;
        System.out.println(description + " 停止");
    }

    public void pause() {
        System.out.println(description + " 暂停 \"" + title + "\"");
    }

    public String toString() {
        return description;
    }
}

3、package dvd

①DvdPlayer.java

package dvd;
import sounds.Amplifier;

public class DvdPlayer {
    String description;
    int currentTrack;
    Amplifier amplifier;
    String movie;

    public DvdPlayer(String description, Amplifier amplifier) {
        this.description = description;
        this.amplifier = amplifier;
    }

    public void on() {
        System.out.println(description + " 开");
    }

    public void off() {
        System.out.println(description + " 关");
    }

        public void eject() {
        movie = null;
                System.out.println(description + " 弹出");
        }

    public void play(String movie) {
        this.movie = movie;
        currentTrack = 0;
        System.out.println(description + " 播放 \"" + movie + "\"");
    }

    public void play(int track) {
        if (movie == null) {
            System.out.println(description + " 不能播放轨道 " + track + " 没有放入DVD");
        } else {
            currentTrack = track;
            System.out.println(description + " 播放轨道 " + currentTrack + " of \"" + movie + "\"");
        }
    }

    public void stop() {
        currentTrack = 0;
        System.out.println(description + " 停止 \"" + movie + "\"");
    }

    public void pause() {
        System.out.println(description + " 暂停 \"" + movie + "\"");
    }

    public void setTwoChannelAudio() {
        System.out.println(description + " 设置两个通道的音频");
    }

    public void setSurroundAudio() {
        System.out.println(description + " 设置环绕音频");
    }

    public String toString() {
        return description;
    }
}

4、package facade

①HomeTheaterFacade.java

package facade;
import cd.CdPlayer;
import dvd.DvdPlayer;
import light.TheaterLights;
import popcorn.PopcornPopper;
import projector.Projector;
import projector.Screen;
import sounds.Amplifier;
import sounds.Tuner;

public class HomeTheaterFacade {
    Amplifier amp;
    Tuner tuner;
    DvdPlayer dvd;
    CdPlayer cd;
    Projector projector;
    TheaterLights lights;
    Screen screen;
    PopcornPopper popper;

    public HomeTheaterFacade(Amplifier amp, 
                 Tuner tuner, 
                 DvdPlayer dvd, 
                 CdPlayer cd, 
                 Projector projector, 
                 Screen screen,
                 TheaterLights lights,
                 PopcornPopper popper) {

        this.amp = amp;
        this.tuner = tuner;
        this.dvd = dvd;
        this.cd = cd;
        this.projector = projector;
        this.screen = screen;
        this.lights = lights;
        this.popper = popper;
    }

    public void watchMovie(String movie) {
        System.out.println("准备开始看电影...");
        popper.on();
        popper.pop();
        lights.dim(10);
        screen.down();
        projector.on();
        projector.wideScreenMode();
        amp.on();
        amp.setDvd(dvd);
        amp.setSurroundSound();
        amp.setVolume(5);
        dvd.on();
        dvd.play(movie);
    }

    public void endMovie() {
        System.out.println("关闭电影...");
        popper.off();
        lights.on();
        screen.up();
        projector.off();
        amp.off();
        dvd.stop();
        dvd.eject();
        dvd.off();
    }

    public void listenToCd(String cdTitle) {
        System.out.println("准备享受听觉盛宴...");
        lights.on();
        amp.on();
        amp.setVolume(5);
        amp.setCd(cd);
        amp.setStereoSound();
        cd.on();
        cd.play(cdTitle);
    }

    public void endCd() {
        System.out.println("关闭 CD...");
        amp.off();
        amp.setCd(cd);
        cd.eject();
        cd.off();
    }

    public void listenToRadio(double frequency) {
        System.out.println("调音量...");
        tuner.on();
        tuner.setFrequency(frequency);
        amp.on();
        amp.setVolume(5);
        amp.setTuner(tuner);
    }

    public void endRadio() {
        System.out.println("关闭音量...");
        tuner.off();
        amp.off();
    }
}

5、package light

①TheaterLights.java

package light;

public class TheaterLights {
    String description;

    public TheaterLights(String description) {
        this.description = description;
    }

    public void on() {
        System.out.println(description + " 开");
    }

    public void off() {
        System.out.println(description + " 关");
    }

    public void dim(int level) {
        System.out.println(description + " 灯光亮度调到 " + level  + "%");
    }

        public String toString() {
                return description;
        }
}

6、package popcorn

①PopcornPopper.java

package popcorn;

public class PopcornPopper {
    String description;

    public PopcornPopper(String description) {
        this.description = description;
    }

    public void on() {
        System.out.println(description + " 开");
    }

    public void off() {
        System.out.println(description + " 关");
    }

    public void pop() {
        System.out.println(description + " 做爆米花!");
    }

        public String toString() {
                return description;
        }
}

7、package projector

①Projector.java

package projector;
import dvd.DvdPlayer;

public class Projector {
    String description;
    DvdPlayer dvdPlayer;

    public Projector(String description, DvdPlayer dvdPlayer) {
        this.description = description;
        this.dvdPlayer = dvdPlayer;
    }

    public void on() {
        System.out.println(description + " 开");
    }

    public void off() {
        System.out.println(description + " 关");
    }

    public void wideScreenMode() {
        System.out.println(description + " 宽屏模式(16x9纵横比)");
    }

    public void tvMode() {
        System.out.println(description + " 在电视模式中(4x3横纵比)");
    }

        public String toString() {
                return description;
        }
}

②Screen.java

package projector;

public class Screen {
    String description;

    public Screen(String description) {
        this.description = description;
    }

    public void up() {
        System.out.println(description + " 上升");
    }

    public void down() {
        System.out.println(description + " 下降");
    }

        public String toString() {
                return description;
        }
}

8、package sounds

①Amplifier.java

package sounds;
import cd.CdPlayer;
import dvd.DvdPlayer;

public class Amplifier {
    String description;
    Tuner tuner;
    DvdPlayer dvd;
    CdPlayer cd;

    public Amplifier(String description) {
        this.description = description;
    }

    public void on() {
        System.out.println(description + " 开");
    }

    public void off() {
        System.out.println(description + " 关");
    }

    public void setStereoSound() {
        System.out.println(description + " 立体模式打开");
    }

    public void setSurroundSound() {
        System.out.println(description + " 环绕立体声(5扬声器,1低音炮)");
    }

    public void setVolume(int level) {
        System.out.println(description + " 设置音量为 " + level);
    }

    public void setTuner(Tuner tuner) {
        System.out.println(description + " 设置Tuner " + dvd);
        this.tuner = tuner;
    }

    public void setDvd(DvdPlayer dvd) {
        System.out.println(description + " 设置DVD播放器 " + dvd);
        this.dvd = dvd;
    }

    public void setCd(CdPlayer cd) {
        System.out.println(description + " 设置CD播放器 " + cd);
        this.cd = cd;
    }

    public String toString() {
        return description;
    }
}

②Tuner.java

package sounds;

public class Tuner {
    String description;
    Amplifier amplifier;
    double frequency;

    public Tuner(String description, Amplifier amplifier) {
        this.description = description;
    }

    public void on() {
        System.out.println(description + " 开");
    }

    public void off() {
        System.out.println(description + " 关");
    }

    public void setFrequency(double frequency) {
        System.out.println(description + " 设置频率为 " + frequency);
        this.frequency = frequency;
    }

    public void setAm() {
        System.out.println(description + " 设置为上午模式");
    }

    public void setFm() {
        System.out.println(description + " 设置为下午模式");
    }

        public String toString() {
                return description;
        }
}

9、package test

①test.java

package test;

import cd.CdPlayer;
import dvd.DvdPlayer;
import facade.HomeTheaterFacade;
import light.TheaterLights;
import popcorn.PopcornPopper;
import projector.Projector;
import projector.Screen;
import sounds.Amplifier;
import sounds.Tuner;

//test
public class test {
    public static void main(String[] args) {
        Amplifier amp = new Amplifier("扩音器");   
        Tuner tuner = new Tuner("调频调谐器", amp);
        DvdPlayer dvd = new DvdPlayer("DVD播放器", amp);
        CdPlayer cd = new CdPlayer("CD播放器", amp);
        Projector projector = new Projector("投影仪", dvd);
        TheaterLights lights = new TheaterLights("家庭影院顶灯");
        Screen screen = new Screen("家庭影院屏幕");
        PopcornPopper popper = new PopcornPopper("爆玉米花机");

        HomeTheaterFacade homeTheater = 
                new HomeTheaterFacade(amp, tuner, dvd, cd, 
                        projector, screen, lights, popper);

        homeTheater.watchMovie("《夺宝奇兵》");
        homeTheater.endMovie();
    }
}

补充类图
Head First设计模式(8):外观模式_第2张图片
image

你可能感兴趣的:(Head First设计模式(8):外观模式)