一、概念
外观模式,为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
二、类图及基本代码
//四个子系统类
class SubSystemOne{
public void MethodOne(){
System.out.println("子系统方法一");
}
}
class SubSystemTwo{
public void MethodTwo(){
System.out.println("子系统方法二");
}
}
class SubSystemThree{
public void MethodThree(){
System.out.println("子系统方法三");
}
}
class SubSystemFour{
public void MethodFour(){
System.out.println("子系统方法四");
}
}
//外观类
public class Facade {
SubSystemOne one;
SubSystemTwo two;
SubSystemThree three;
SubSystemFour four;
public Facade(){
one=new SubSystemOne();
two=new SubSystemTwo();
three=new SubSystemThree();
four=new SubSystemFour();
}
public void MethodA(){
System.out.println("方法组a");
one.MethodOne();
two.MethodTwo();
three.MethodThree();
}
public void MethodB(){
System.out.println("方法组b");
three.MethodThree();
four.MethodFour();
}
}
//客户端
public static void main(String[] args) {
Facade facade=new Facade();//客户端只和外观类进行交互,不知道四个子系统
facade.MethodA();
}
三、实例之股民炒股
(1)、未使用外观模式
//股票,房产,国债类
/*
* 股票类
*/
public class Stock1 {
public void Sell() {
System.out.println("股票1卖出");
}
public void Buy() {
System.out.println("股票1买入");
}
}
class Stock2 {
public void Sell() {
System.out.println("股票2卖出");
}
public void Buy() {
System.out.println("股票2买入");
}
}
class Stock3 {
public void Sell() {
System.out.println("股票3卖出");
}
public void Buy() {
System.out.println("股票3买入");
}
}
class NationalDebt1{
public void Sell() {
System.out.println("国债1卖出");
}
public void Buy() {
System.out.println("国债1买入");
}
}
class Realty1{
public void Sell() {
System.out.println("房地产1卖出");
}
public void Buy() {
System.out.println("房地产1买入");
}
}
//客户端
public class Show {
public static void main(String[] args) {
Stock1 stock1=new Stock1();
Stock2 stock2=new Stock2();
Stock3 stock3=new Stock3();
NationalDebt1 nationalDebt1=new NationalDebt1();
Realty1 realty1=new Realty1();
stock1.Buy();
stock2.Buy();
stock3.Buy();
nationalDebt1.Buy();
realty1.Buy();
stock1.Sell();
stock2.Sell();
stock3.Sell();
nationalDebt1.Sell();
realty1.Sell();
}
}
(2)、使用外观模式
//基金类
/*
* 基金类
*/
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();
}
}
//股票,房地产,国债类
public class Stock1 {
public void Sell() {
System.out.println("股票1卖出");
}
public void Buy() {
System.out.println("股票1买入");
}
}
class Stock2 {
public void Sell() {
System.out.println("股票2卖出");
}
public void Buy() {
System.out.println("股票2买入");
}
}
class Stock3 {
public void Sell() {
System.out.println("股票3卖出");
}
public void Buy() {
System.out.println("股票3买入");
}
}
class NationalDebt1{
public void Sell() {
System.out.println("国债1卖出");
}
public void Buy() {
System.out.println("国债1买入");
}
}
class Realty1{
public void Sell() {
System.out.println("房地产1卖出");
}
public void Buy() {
System.out.println("房地产1买入");
}
}
//客户端----降低了股民与股票,房地产,国债之间的耦合
public class Show {
public static void main(String[] args) {
Fund fund=new Fund();
fund.BuyFund();
fund.SellFund();
}
}
四、总结
通过外观模式可以提供一个简单的接口,从而降低客户类与子系统类之间的耦合。
1.设计初期,将系统分层,通过外观模式为复杂的子系统提供一个简单的接口。
2.开始阶段,减少外部调用子系统的使用困难。
3.对于复杂的老系统,直接改或者拓展都有可能出现问题,此时需要一个外观类,这个类负责与老系统的交互,提供较为简单的接口,在开发新系统时直接调用该接口就可以了。
No matter how familiar we used to be with each other,as long as we are apart then we become strangers