设计模式(六)---外观模式

外观模式

为子系统中的一组接口提供一个以自的界面,为模式定义了一个高层接口,这个接口使得这椅子系统更加容易适用。在这里插入图片描述

例子 (买卖股票的基金)

在这里插入图片描述
股民可以通过基金更好的理财。
而在背后基金就是利用股民的投资去投资其他股票获得收益。

public class Stock1 {

    public void buy(){
    	System.out.println("Buy...111");
    }

    public void sell(){
    	System.out.println("Sell...111");
    }

}

Fund(基金类)要有基本的字段。
public class Fund {
	Stock1 s1;
	Stock2 s2;
	Stock3 s3;
	Stock4 s4;
	public Fund() {
		s1 = new Stock1();
		s2 = new Stock2();
		s3 = new Stock3();
		s4 = new Stock4();
		
	}
    public void buy(){
    	s1.buy();
    	s2.buy();
    	s3.buy();
    	s4.buy();
    }

    public void returns(){
    	s1.sell();
    	s2.sell();
    	s3.sell();
    	s4.sell();
    }

}

public class Client {
	public static void main(String[] args) {
		Fund fund = new Fund();
		fund.buy();
		fund.returns();
	}
}

总结

  • 优点:
    1、减少系统相互依赖。
    2、提高灵活性。
    3、提高了安全性。

  • 缺点:不符合开闭原则,如果要改东西很麻烦,继承重写都不合适。

  • 使用场景:
    1、为复杂的模块或子系统提供外界访问的模块。
    2、子系统相对独立。
    3、预防低水平人员带来的风险。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值