前言
`
一、外观模式基本介绍
完整代码
DVD类
package tanchishell.SJMS.facade;
//DVD类
public class DVDPlayer {
//使用单例模式, 使用饿汉式
private static DVDPlayer instance = new DVDPlayer();
public static DVDPlayer getInstanc() {
return instance;
}
//打开DVD
public void on() {
System.out.println(" 打开DVD ");
}
//关闭DVD
public void off() {
System.out.println(" 关闭DVD ");
}
//播放DVD
public void play() {
System.out.println(" 播放DVD ");
}
//暂停DVD
public void pause() {
System.out.println(" 暂停DVD ..");
}
}
爆米花类
package tanchishell.SJMS.facade;
//爆米花机
public class Popcorn {
private static Popcorn instance = new Popcorn();
public static Popcorn getInstance() {
return instance;
}
//打开爆米花机
public void on() {
System.out.println(" 打开爆米花机 ");
}
//关闭爆米花机
public void off() {
System.out.println(" 关闭爆米花机 ");
}
//爆米花机正在工作
public void pop() {
System.out.println(" 爆米花机正在工作 ");
}
}
投影仪类
package tanchishell.SJMS.facade;
//投影仪
public class Projector {
private static Projector instance = new Projector();
public static Projector getInstance() {
return instance;
}
//打开投影仪
public void on() {
System.out.println(" 打开投影仪 ");
}
//关闭投影仪
public void off() {
System.out.println(" 关闭投影仪 ");
}
//投影仪正在工作
public void focus() {
System.out.println(" 投影仪正在工作 ");
}
}
屏幕类
package tanchishell.SJMS.facade;
//屏幕类
public class Screen {
private static Screen instance = new Screen();
public static Screen getInstance() {
return instance;
}
//升起屏幕
public void up() {
System.out.println(" 升起屏幕 ");
}
//下降屏幕
public void down() {
System.out.println(" 下降屏幕 ");
}
}
立体声类
package tanchishell.SJMS.facade;
//立体声
public class Stereo {
private static Stereo instance = new Stereo();
public static Stereo getInstance() {
return instance;
}
//打开立体声
public void on() {
System.out.println(" 打开立体声 ");
}
//关闭立体声
public void off() {
System.out.println(" 关闭立体声 ");
}
}
灯光类
package tanchishell.SJMS.facade;
//灯光类
public class TheaterLight {
private static TheaterLight instance = new TheaterLight();
public static TheaterLight getInstance() {
return instance;
}
//打开灯光
public void on() {
System.out.println(" 打开灯光 ");
}
//关闭灯光
public void off() {
System.out.println(" 关闭灯光 ");
}
//调暗灯光
public void dim() {
System.out.println(" 调暗灯光.. ");
}
//调亮灯光
public void bright() {
System.out.println(" 调亮灯光.. ");
}
}
家庭影院类进行聚合
package tanchishell.SJMS.facade;
//家庭影院类进行聚合
public class HomeTheaterFacade {
//定义各个子系统对象
private TheaterLight theaterLight;
private Popcorn popcorn;
private Stereo stereo;
private Projector projector;
private Screen screen;
private DVDPlayer dVDPlayer;
//构造器获取子系统单例对象
public HomeTheaterFacade() {
super();
this.theaterLight = TheaterLight.getInstance();
this.popcorn = Popcorn.getInstance();
this.stereo = Stereo.getInstance();
this.projector = Projector.getInstance();
this.screen = Screen.getInstance();
this.dVDPlayer = DVDPlayer.getInstanc();
}
/**操作分成 4 步 准备 开始 暂停 结束
*
*/
//准备
public void ready() {
//打开爆米花机
popcorn.on();
//爆米花机正在工作
popcorn.pop();
//下降屏幕
screen.down();
//打开投影仪
projector.on();
//打开立体声
stereo.on();
//打开DVD
dVDPlayer.on();
//调暗灯光
theaterLight.dim();
}
//开始
public void play() {
//播放DVD
dVDPlayer.play();
}
//暂停
public void pause() {
//暂停DVD
dVDPlayer.pause();
}
//结束
public void end() {
//关闭爆米花机
popcorn.off();
//调亮灯光
theaterLight.bright();
//升起屏幕
screen.up();
//关闭投影仪
projector.off();
//关闭立体声
stereo.off();
//关闭DVD
dVDPlayer.off();
}
}
Client测试类
public class Client {
public static void main(String[] args) {
// TODO Auto-generated method stub
//这里直接调用。。 很麻烦
HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade();
homeTheaterFacade.ready();
System.out.println("-------------------------");
homeTheaterFacade.play();
System.out.println("-------------------------");
homeTheaterFacade.pause();
System.out.println("-------------------------");
homeTheaterFacade.end();
}
}
输出
打开爆米花机
爆米花机正在工作
下降屏幕
打开投影仪
打开立体声
打开DVD
调暗灯光..
-------------------------
播放DVD
-------------------------
暂停DVD ..
-------------------------
关闭爆米花机
调亮灯光..
升起屏幕
关闭投影仪
关闭立体声
关闭DVD
二、 外观模式在MyBatis框架应用的源码分析
文章来源:https://www.toymoban.com/news/detail-424292.html
三、外观模式的注意事项和细节
文章来源地址https://www.toymoban.com/news/detail-424292.html
到了这里,关于第十二章 外观模式的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!