大话设计模式-外观模式学习总结

一、概念

外观模式,为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。

二、类图及基本代码

这里写图片描述

//四个子系统类
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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值