设计模式--外观模式
设计模式--外观模式
外观模式原理
·一个家庭影院项目
组建一个家庭影院:
DVD播放器、投影仪、自动屏幕、环绕立体声、爆米花机
·传统的控制接口设计
直接用遥控器:统筹各设备开关
开爆米花机
放下屏幕
开投影仪
开音响
开DVD,选dvd
去拿爆米花
调暗灯光
播放
观影结束后,关闭各种设备
·外观模式的原理和设计
外观模式:提供一个统一的接口,来访问子系统中一群功能相关接口
外观模式定义了一个高层接口,让子系统更容易使用
代码演示
Popcorn
public class Popcorn {
private static Popcorn instance = null;
private Popcorn() {
}
public static Popcorn getInstance() {
if (instance == null) {
instance = new Popcorn();
}
return instance;
}
public void on() {
System.out.println("Popcorn On");
}
public void off() {
System.out.println("Popcorn Off");
}
public void pop() {
System.out.println("Popcorn is popping");
}
}
Projector
public class Projector {
private int size=5;
private static Projector instance = null;
private Projector() {
}
public static Projector getInstance() {
if (instance == null) {
instance = new Projector();
}
return instance;
}
public void on() {
System.out.println("Projector On");
}
public void off() {
System.out.println("Projector Off");
}
public void focus() {
System.out.println("Popcorn is focus");
}
public void zoom(int size) {
this.size=size;
System.out.println("Popcorn zoom to"+size);
}
}
DVDPlayer
public class DVDPlayer {
private static DVDPlayer instance = null;
private DVDPlayer() {
}
public static DVDPlayer getInstance() {
if (instance == null) {
instance = new DVDPlayer();
}
return instance;
}
public void on() {
System.out.println("DVDPlayer On");
}
public void off() {
System.out.println("DVDPlayer Off");
}
public void play() {
System.out.println("DVDPlayer is playing");
}
public void pause() {
System.out.println("DVDPlayer pause");
}
public void setdvd() {
System.out.println("DVDPlayer is setting dvd");
}
}
Screen
public class Screen {
private static Screen instance = null;
private Screen() {
}
public static Screen getInstance() {
if (instance == null) {
instance = new Screen();
}
return instance;
}
public void up() {
System.out.println("Screen up");
}
public void down() {
System.out.println("Screen down");
}
}
Stereo
public class Stereo {
private static Stereo instance = null;
private int volume = 5;
private Stereo() {
}
public static Stereo getInstance() {
if (instance == null) {
instance = new Stereo();
}
return instance;
}
public void on() {
System.out.println("Stereo On");
}
public void off() {
System.out.println("Stereo Off");
}
public void setVolume(int vol) {
volume = vol;
System.out.println("the volume of Stereo is set to " + volume);
}
public void addVolume() {
if (volume < 11) {
volume++;
setVolume(volume);
}
}
public void subVolume() {
if (volume > 0) {
volume--;
setVolume(volume);
}
}
}
TheaterLights
public class TheaterLights {
private static TheaterLights instance = null;
private TheaterLights() {
}
public static TheaterLights getInstance() {
if (instance == null) {
instance = new TheaterLights();
}
return instance;
}
public void on() {
System.out.println("TheaterLights On");
}
public void off() {
System.out.println("TheaterLights Off");
}
public void dim(int d) {
System.out.println("TheaterLights dim to " + d + "%");
}
public void bright() {
dim(100);
System.out.println("TheaterLights bright");
}
}
控制类HomeTheaterFacade
public class HomeTheaterFacade {
private TheaterLights mTheaterLights;
private Popcorn mPopcorn;
private Stereo mStereo;
private Projector mProjector;
private Screen mScreen;
private DVDPlayer mDVDPlayer;
public HomeTheaterFacade() {
mTheaterLights = TheaterLights.getInstance();
mPopcorn = Popcorn.getInstance();
mStereo = Stereo.getInstance();
mProjector = Projector.getInstance();
mScreen = Screen.getInstance();
mDVDPlayer = DVDPlayer.getInstance();
}
public void ready() {
mPopcorn.on();
mPopcorn.pop();
mScreen.down();
mProjector.on();
mStereo.on();
mDVDPlayer.on();
mDVDPlayer.setdvd();
mTheaterLights.dim(10);
}
public void end() {
mPopcorn.off();
mTheaterLights.bright();
mScreen.up();
mProjector.off();
mStereo.off();
mDVDPlayer.setdvd();
mDVDPlayer.off();
}
public void play() {
mDVDPlayer.play();
}
public void pause() {
mDVDPlayer.pause();
}
}
MainTest
public class MainTest {
public static void main(String[] args) {
HomeTheaterFacade mHomeTheaterFacade=new HomeTheaterFacade();
mHomeTheaterFacade.ready();
mHomeTheaterFacade.play();
}
}
·外观模式与命令模式
外观模式和命令模式各自侧重点
外观模式的侧重点:在一个系统里建立接口,实现什么功能暴露出来,外面调用时不需要知道里面的细节,对外暴露的接口只有一个,外面和里面可以得到解耦;
命令模式的侧重点:把命令包装成对象,实现控制器与命令的解耦。
最少知识原则
·最少知识原则的意义:尽量减少对象之间的交互,只留几个“密友”,项目设计中就是不要让太多的类耦合在一起
·如何遵循最少知识原则
对象的方法调用范围:
该对象本身
作为参数传进来的对象
此方法创建和实例化的对象
对象的组件
看个Car类的例子
public class Car{
Engine engine;
public Car()
{
//初始化发动机
}
public void start(Key mKey)
{
Doors doors=new Doors();
boolean authorized=mKey.turns(); //违背了原则
if(authorized)
{
engine.start();
doors.lock();
}
}
}
·外观模式和最少知识原则