在家庭影院中,要享受一场电影,需要如下步骤:
【传统方案实现】
创建多个类,分别表示各种机器,然后每个类里面有相应机器的操作方式,最后在主类中分别创建每种机器的对象,并调用其方法来控制机器
缺点:
改进:
比如在高层接口提供四个方法ready, play, pause, end
),用来访问子系统中的一群接口,也就是说 就是通过定义一个一致的接口(界面类),用以屏蔽内部子系统的细节,使得调用端只需跟这个接口发生调用,而无需关心其他子系统的内部细节Facade(窗口)
:是代表构成系统的许多其他角色的“简单窗口”。Facade角色向系统外部提供高层接口(API)构成系统的许多其他角色
:这些角色各自完成自己的工作,它们并不知道Facade角色。Facade角色调用其他角色进行工作,但是其他角色不会调用Facade角色Client(请求者)
:负责调用 Facade角色【子系统:DVD机】
package com.atguigu.facade;
/**
* DVD机
*/
public class DVDPlayer {
/**
* 使用单例模式, 使用饿汉式
*/
private static DVDPlayer instance = new DVDPlayer();
public static DVDPlayer getInstance() {
return instance;
}
/**
* 开机
*/
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 ");
}
/**
* 暂停
*/
public void pause() {
System.out.println(" dvd pause ..");
}
}
【子系统:爆花机】
package com.atguigu.facade;
/**
* 爆米花机,用于制作爆米花
*/
public class Popcorn {
private static Popcorn instance = new Popcorn();
public static Popcorn getInstance() {
return instance;
}
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.atguigu.facade;
/**
* 投影仪
*/
public class Projector {
private static Projector instance = new Projector();
public static Projector getInstance() {
return instance;
}
public void on() {
System.out.println(" Projector on ");
}
public void off() {
System.out.println(" Projector off ");
}
/**
* 聚焦
*/
public void focus() {
System.out.println(" Projector is Projector ");
}
}
【子系统:屏幕】
package com.atguigu.facade;
/**
* 屏幕
*/
public class Screen {
private static Screen instance = new Screen();
public static Screen getInstance() {
return instance;
}
/**
* 屏幕往上升
*/
public void up() {
System.out.println(" Screen up ");
}
/**
* 屏幕下降
*/
public void down() {
System.out.println(" Screen down ");
}
}
【子系统:立体声】
package com.atguigu.facade;
/**
* 立体声
*/
public class Stereo {
private static Stereo instance = new Stereo();
public static Stereo getInstance() {
return instance;
}
public void on() {
System.out.println(" Stereo on ");
}
public void off() {
System.out.println(" Screen off ");
}
public void up() {
System.out.println(" Screen up.. ");
}
}
【子系统:灯光】
package com.atguigu.facade;
/**
* 灯光
*/
public class TheaterLight {
private static TheaterLight instance = new TheaterLight();
public static TheaterLight getInstance() {
return instance;
}
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.atguigu.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.getInstance();
}
/**
* 操作分成 4 步
*/
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.pause();
}
public void end() {
popcorn.off();
theaterLight.bright();
screen.up();
projector.off();
stereo.off();
dVDPlayer.off();
}
}
【客户端】
package com.atguigu.facade;
public class Client {
public static void main(String[] args) {
//这里直接调用外观类
HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade();
System.out.println("-------------------------------准备步骤-------------------------------");
homeTheaterFacade.ready();
System.out.println("-------------------------------电影播放-------------------------------");
homeTheaterFacade.play();
System.out.println("-------------------------------观看结束-------------------------------");
homeTheaterFacade.end();
}
}
【运行】
-------------------------------准备步骤-------------------------------
popcorn on
popcorn is poping
Screen down
Projector on
Stereo on
dvd on
TheaterLight dim..
-------------------------------电影播放-------------------------------
dvd is playing
-------------------------------观看结束-------------------------------
popcorn ff
TheaterLight bright..
Screen up
Projector off
Screen off
dvd off
Process finished with exit code 0
【数据库类】
package com.atguigu.facade.Sample.pagemaker;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class Database {
/**
*防止外部new出Database的实例,所以声明为private方法
*/
private Database() {
}
/**
* 根据数据库名获取Properties
* @param dbname
* @return
*/
public static Properties getProperties(String dbname) {
String filename = dbname + ".txt";
Properties prop = new Properties();
try {
prop.load(new FileInputStream(filename));
} catch (IOException e) {
System.out.println("Warning: " + filename + " is not found.");
}
return prop;
}
}
【数据文件】
[email protected]=Hiroshi Yuki
[email protected]=Hanako Sato
[email protected]=Tomura
[email protected]=Mamoru Takahashi
【Html文件输出】
package com.atguigu.facade.Sample.pagemaker;
import java.io.IOException;
import java.io.Writer;
/**
* 用于编写简单的Web页面
*/
public class HtmlWriter {
private Writer writer;
public HtmlWriter(Writer writer) { // 构造函数
this.writer = writer;
}
/**
* 输出标题
*
* @param title
* @throws IOException
*/
public void title(String title) throws IOException {
writer.write("");
writer.write("");
writer.write("" + title + " ");
writer.write("");
writer.write("\n");
writer.write("" + title + "
\n");
}
/**
* 输出段落
* @param msg
* @throws IOException
*/
public void paragraph(String msg) throws IOException {
writer.write("" + msg + "
\n");
}
/**
* 输出超链接
* @param href
* @param caption
* @throws IOException
*/
public void link(String href, String caption) throws IOException {
paragraph("" + caption + "");
}
/**
* 输出邮件地址
* @param mailaddr
* @param username
* @throws IOException
*/
public void mailto(String mailaddr, String username) throws IOException {
link("mailto:" + mailaddr, username);
}
/**
* 结束输出HTML
* @throws IOException
*/
public void close() throws IOException {
writer.write("");
writer.write("\n");
writer.close();
}
}
【外观类】
package com.atguigu.facade.Sample.pagemaker;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
public class PageMaker {
/**
* 防止外部new出PageMaker的实例,所以声明为private方法
*/
private PageMaker() {
}
/**
* 根据邮件地址和文件名来创建页面
* @param mailaddr
* @param filename
*/
public static void makeWelcomePage(String mailaddr, String filename) {
try {
Properties mailprop = Database.getProperties("maildata");
String username = mailprop.getProperty(mailaddr);
HtmlWriter writer = new HtmlWriter(new FileWriter(filename));
writer.title("Welcome to " + username + "'s page!");
writer.paragraph("欢迎来到" + username + "的主页。");
writer.paragraph("等着你的邮件哦!");
writer.mailto(mailaddr, username);
writer.close();
System.out.println(filename + " is created for " + mailaddr + " (" + username + ")");
} catch (IOException e) {
e.printStackTrace();
}
}
}
【主类】
package com.atguigu.facade.Sample;
import com.atguigu.facade.Sample.pagemaker.PageMaker;
public class Main {
public static void main(String[] args) {
PageMaker.makeWelcomePage("[email protected]", "welcome.html");
}
}