外观模式:为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易地使用。
简单来说,该模式就是把一些复杂的流程封装成一个接口供给外部用户更简单的使用。这个模式中,设计到3个角色。
-
门面角色:外观模式的核心。它被客户角色调用,它熟悉子系统的功能。内部根据客户角色的需求预定了几种功能的组合。
-
子系统角色:实现了子系统的功能。它对客户角色和Facade时未知的。它内部可以有系统内的相互交互,也可以由供外界调用的接口。
-
客户角色:通过调用Facede来完成要实现的功能
/**
* “系统”接口,只是标记接口,暂无任何意义
*
* @author administrator
*
*/
public interface SystemInterface {
}
class SubSystemOne implements SystemInterface {
public void methodOne() {
System.out.println("子系统方法一");
}
}
class SubSystemTwo implements SystemInterface {
public void methodTwo() {
System.out.println("子系统方法二");
}
}
class SubSystemThree implements SystemInterface {
public void methodThree() {
System.out.println("子系统方法三");
}
}
class SubSystemFour implements SystemInterface {
public void methodFour() {
System.out.println("子系统方法四");
}
}
/**
* 外观类,它需要了解所有的子系统的方法或属性,进行组合,以备外界调用
*
* @author administrator
*
*/
public class Facade {
SubSystemOne subSystemOne;
SubSystemTwo subSystemTwo;
SubSystemThree subSystemThree;
SubSystemFour subSystemFour;
public Facade() {
subSystemOne = new SubSystemOne();
subSystemTwo = new SubSystemTwo();
subSystemThree = new SubSystemThree();
subSystemFour = new SubSystemFour();
}
public void methodA() {
System.out.println("方法组A:");
subSystemOne.methodOne();
subSystemTwo.methodTwo();
subSystemFour.methodFour();
}
public void methodB() {
System.out.println("方法组B:");
subSystemThree.methodThree();
subSystemFour.methodFour();
}
}
/**
* 外观类客户端
*
* @author administrator
*
*/
public class FacadeClient {
public static void main(String[] args) {
// 由于Facade的作用,客户端可以根本不知道四个子系统的存在
// 启发:维护一个遗留的大型系统时,可能这个系统已经非常难以维护和扩展了,此时可以
// 为新系统开发一个外观Facade类,来提供设计粗糙或高度复杂的遗留代码的比较清晰简单
// 的接口,让新系统与Facade对象交互,Facade与遗留代码交互所有复杂的工作
Facade facade = new Facade();
facade.methodA();
facade.methodB();
}
}
案例 基金
/**
* 外观模式(Facade)
* 股票1
*/
public class Stock1 {
// 卖股票
public void sell() {
System.out.println("股票1卖出");
}
// 买股票
public void buy() {
System.out.println("股票1买入");
}
}
/**
* 外观模式(Facade)
* 股票2
*/
public class Stock2 {
// 卖股票
public void sell() {
System.out.println("股票2卖出");
}
// 买股票
public void buy() {
System.out.println("股票2买入");
}
}
/**
* 外观模式(Facade)
* 股票3
*/
public class Stock3 {
// 卖股票
public void sell() {
System.out.println("股票3卖出");
}
// 买股票
public void buy() {
System.out.println("股票3买入");
}
}
/**
* 外观模式(Facade)
* 房产1
*/
public class Realty1 {
// 卖房产
public void sell() {
System.out.println("房产1卖出");
}
// 买房产
public void buy() {
System.out.println("房产1买入");
}
}
/**
* 外观模式(Facade)
* 国债1
*/
public class NationalDebt1 {
// 卖国债
public void sell() {
System.out.println("国债1卖出");
}
// 买国债
public void buy() {
System.out.println("国债1买入");
}
}
/**
* 外观模式(Facade)
* 基金类
*/
public class Fund {
Stock1 stock1;
Stock2 stock2;
Stock3 stock3;
NationalDebt1 nationalDebt1;
Realty1 realty1;
public Fund() {
stock1 = new Stock1();
stock2 = new Stock2();
stock3 = new Stock3();
nationalDebt1 = new NationalDebt1();
realty1 = new Realty1();
}
public void buyFund() {
stock1.buy();
stock2.buy();
stock3.buy();
nationalDebt1.buy();
realty1.buy();
}
public void sellFund() {
stock1.sell();
stock2.sell();
stock3.sell();
nationalDebt1.sell();
realty1.sell();
}
}
/**
* 外观模式(Facade)
* 客户端main方法
*/
public class Client {
public static void main(String[] args) {
Fund fund = new Fund();
// 基金买入
fund.buyFund();
// 基金赎回
fund.sellFund();
}
}