图解Java设计模式之外观模式

2022-10-09,,,,

图解java设计模式外观模式

    • 影院管理项目
    • 传统方式解决影院管理
    • 传统方式解决影院管理问题分析
    • 外观模式基本介绍
    • 外观模式原理类图
    • 外观模式解决影院管理
    • 外观模式的注意事项和细节

 

影院管理项目

组建一个家庭影院 :
dvd 播放器、投影仪、自动屏幕、环绕立体声、爆米花机,要求完成使用家庭影院的功能,其过程为: 直接用遥控器:统筹各设备开关
开爆米花机
放下屏幕
开投影仪
开音响
开 dvd,选 dvd
去拿爆米花
调暗灯光
播放
观影结束后,关闭各种设备

传统方式解决影院管理

传统方式解决影院管理问题分析

1)在clienttest的main方法中,创建各个子系统的对象,并直接去调用子系统(对象)相关方法,会造成调用过程混乱,没有清晰的过程。
2)不利于在clienttest中,去维护对子系统的操作。
3)解决思路 :定义一个高层接口,给子系统中的一组接口提供一个一致的界面(比如在高层接口提供四个方法ready,play,pause,end),用来访问子系统中的一群接口
4)也就是说,通过定义一个一致的接口(界面类),用以屏蔽内部子系统的细节,使得调用端只需跟这个接口发生调用,而无需关系这个系统的内部细节 => 外观模式

外观模式基本介绍

1)外观模式(facade),也叫“过程模式”:外观模式为子系统中的一组接口提供一个一致的界面,此模式定义类一个高层接口,这个接口使得这一子系统更加容易使用
2)外观模式通过定义一个一致的接口,用以屏蔽内部子系统的细节,使得调用端只需跟这个接口发生调用,而无需关心这个子系统的内部细节

外观模式原理类图

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

外观模式解决影院管理

1)外观模式可以理解为转换一群接口,客户只要调用一个接口,而不用调用多个接口才能达到目的。比如 :在pc上安装软件的时候经常有一键安装选项(等等),还有手机重启功能(把关机和启动合为一个操作)。
2)外观模式就是解决多个复杂接口带来的使用困难,起到简化用户操作的作用。

package com.example.demo.facade;

public class theaterlight {
	
	private static theaterlight instancelight = new theaterlight();
	
	public static theaterlight getheaterlight() {
		return instancelight;
	}

	public void on() {
		system.out.println(" theaterlight on ");
	}
	
	public void off() {
		system.out.println(" theaterlight off ");
	}
	
	public void dim() {
		system.out.println(" theaterlight dim ");
	}
	
	public void bright() {
		system.out.println(" theaterlight bright ");
	}
}
package com.example.demo.facade;

public class stereo {
	
	private static stereo instancestereo = new stereo();
	
	public static stereo getstereo() {
		return instancestereo;
	}
	
	public void on() {
		system.out.println(" stereo on ");
	}
	
	public void off() {
		system.out.println(" stereo off ");
	}
	
	public void up() {
		system.out.println(" screen up .. ");
	}

}
package com.example.demo.facade;

public class screen {

	private static screen instancescreen = new screen();
	
	public static screen getinstance() {
		return instancescreen;
	}
	
	public void up() {
		system.out.println(" screen up ");
	}
	
	public void down() {
		system.out.println(" screen down ");
	}
	
}
package com.example.demo.facade;

public class projector {

	private static projector instanceprojector = new projector();
	
	public static projector getinstance() {
		return instanceprojector;
	}
	
	public void on() {
		system.out.println(" projector on ");
	}
	
	public void off() {
		system.out.println(" projector ff ");
	}
	
	public void focus() {
		system.out.println(" projector is focus ");
	}
}
package com.example.demo.facade;

public class popcorn {
	
	private static popcorn instencepopcorn = new popcorn();
	
	public static popcorn getinpopcorn() {
		return instencepopcorn;
	}

	public void on() {
		system.out.println(" popcorn on ");
	}
	
	public void off() {
		system.out.println(" popcorn ff ");
	}
	
	public void pop() {
		system.out.println(" popcorn is poping ");
	}
}
package com.example.demo.facade;

public class dvdplayer {

	private static dvdplayer instancedvdplayer = new dvdplayer();
	
	public static dvdplayer getinstance() {
		return instancedvdplayer;
	}
	
	public void on() {
		system.out.println(" dvd on ");
	}
	
	public void off() {
		system.out.println(" dvd off ");
	}
	
	public void play() {
		system.out.println(" dvd is playing ");
	}
}
package com.example.demo.facade;

public class hometheaterfacade {

	private theaterlight theaterlight;
	private popcorn popcorn;
	private stereo stereo;
	private projector projector;
	private screen screen;
	private dvdplayer dvdplayer;
	
	public hometheaterfacade() {
		this.theaterlight = theaterlight.getheaterlight();
		this.dvdplayer = dvdplayer.getinstance();
		this.popcorn = popcorn.getinpopcorn();
		this.projector = projector.getinstance();
		this.screen = screen.getinstance();
		this.stereo = stereo.getstereo();
	}
	
	// 操作分成四部
	public void ready() {
		popcorn.on();
		popcorn.pop();
		screen.down();
		projector.on();
		stereo.on();
		dvdplayer.on();
		theaterlight.dim();
	}
	
	public void play() {
		dvdplayer.play();
	}
	
	public void pause() {
		dvdplayer.play();
	}
	
	public void end() {
		popcorn.off();
		theaterlight.bright();
		screen.up();
		projector.off();
		stereo.off();
		dvdplayer.off();
	}
	
}
package com.example.demo.facade;

public class client {

	public static void main(string[] args) {
		// todo auto-generated method stub
		hometheaterfacade hometheaterfacade = new hometheaterfacade();
		hometheaterfacade.ready();
		hometheaterfacade.play();
	}

}

外观模式的注意事项和细节

1)外观模式对外屏蔽了子系统的细节,因此外观模式降低了客户端对子系统使用的复杂性
2)外观模式对客户端与子系统的耦合关心 - 解耦,让子系统内部的模块更易维护和扩展
3)通过合理的使用外观模式,可以帮我们更好的划分访问的层次
4)当系统需要进行分层设计时,可以考虑使用facade模式
5)在维护一个遗留的大型系统时,可能这个系统已经变得非常难以维护和扩展,此时可以考虑为新系统开发一个facade类,来提供遗留系统的比较清晰简单的接口,让新系统与facade类交互,提高复用性
6)不能过多的或者不合理的使用外观模式,使用外观模式好,还是直接调用模块好。要以让系统有层次,利于维护为目的。

《图解Java设计模式之外观模式.doc》

下载本文的Word格式文档,以方便收藏与打印。