了解外观模式

外观模式(Facade pattern),用户操作不同的子系统,要解决用户与子系统的高耦合度,则使用此模式,增加一个用户操作系统,用户可通过此系统,操作到其他子系统,从而降低用户与子系统的耦合。

例如顾客要买肉,水果,用品。顾客一与顾客二买的东西有些一样,有些不一样。

子系统:

public class Fruit {
   public String apple;
   public String pear;
}

 水果

public class Meat {
	public String fish;
	public String chicken;
}

 肉

public class Things {
	public String pot;
	public String bowl;
}

 用品。

如果不使用外观模式

顾客一买水果中的苹果,肉食的鱼

public class ClientA {
	public void buy(){
		Fruit f = new Fruit();
		f.apple="1";
		Meat m = new Meat();
		m.chicken="2";
	}
}

 顾客二买水果中的梨,用品中的盆

public class ClientB {
	public void buy(){
		Fruit f = new Fruit();
		f.pear="3";
		Things t = new Things();
		t.pot="2";
	}
}

 这样可以看出顾客类,直接在操作子系统。

 

如果使用外观模式,可以提供一个商场,顾客可以去商场买。

public class Shop {
	Fruit f = new Fruit();
	Things t = new Things();
	Meat m = new Meat();
}

 

顾客可从这里去买自己想要的。

public class ClientC {
	public void buy(){
		Shop s = new Shop();
		s.f.apple="1";
		s.m.chicken="2";
		s.t.pot="3";
	}
}

 

这里不难看出此模式重要作用也是解耦

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值