设计模式之门面模式

设计模式之门面模式

    门面模式(Facade Pattern)也叫外观模式,是一种常见的封装模式,定义:Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.(要求一个子系统的外部与其内部的通信必须通过统一的对象进行。门面模式提供一个高层次的接口,使子系统更易于使用)

    门面模式注重“统一的对象”,也就是提供一个访问子系统的接口,除了这个接口不允许有任何访问子系统的行为发生。

通用类图


    Subsystem Classes是子系统所有类的简称,可能代表一个类,也可能代表几十个对象的集合。简单地说,门面对象是外界访问子系统内部的唯一通道,不管系统内部多么复杂,只要门面对象在,就可以做到“金玉其外败絮其中”。

两个角色

1.Facade门面角色,客户端可以调用这个角色的方法,此角色知晓子系统的所有功能和责任。一般情况下,本角色会将所有从客户端发来的请求委派到相应的子系统中,也就是说该角色没有实际的业务逻辑,只是一个委托类;

2.subsystem子系统角色,可以同时拥有一个或多个子系统,每一个子系统不是一个单独的类,而是一个类的集合,子系统并不知道门面的存在。对呀子系统而言,门面仅仅是另外一个客户端而已。

通用源码

子系统

public class ClassA {
	public void doSomethingA(){
		
	}
}
public class ClassB {
	public void doSomethingB(){
		
	}
}

public class ClassC {
	public void doSomethingC(){
		
	}
}

门面对象

public class Facade {
	private ClassA a=new ClassA();
	private ClassB b=new ClassB();
	private ClassC c=new ClassC();
	
	public void methodA(){
		this.a.doSomethingA();
	}
	public void methodB(){
		this.b.doSomethingB();
	}
	public void methodC(){
		this.c.doSomethingC();
	}
}

门面模式的优点:

1.减少系统的相互依赖,所有的依赖都是对门面的依赖,与子系统无关;

2.提高了灵活性,依赖减少了,灵活性自然提高,不管子系统内部如何变化,只要不影响到门面对象,可随意活动;

3.提高安全性,不在门面上开通的方法,客户端不能访问。

门面模式缺点

不符合开闭原则,对修改关闭,对扩展开放。

门面模式使用场景

1.为一个复杂的模块或子系统提供一个供外界访问的接口;

2.子系统相对独立,外界对子系统的访问只要黑箱操作即可;

3,。预防低水平人员带来的风险扩散。

例子:信件投递全过程

写信过程接口

public interface ILetterProcess {
	public void writeContext(String context);
	public void fillEnvelope(String address);
	public void letterInotoEnvelope();
	public void sendLetter();
}

写信过程实现

public class LetterProcessImpl implements ILetterProcess{
	public void writeContext(String context){
		System.out.println("信的内容:"+context);
	}
	public void fillEnvelope(String address){
		System.out.println("信的地址:"+address);
	}
	public void letterInotoEnvelope(){
		System.out.println("装入信封");
	}
	public void sendLetter(){
		System.out.println("投递");
	}
}

现代邮局

public class MordenPostOffice {
	private ILetterProcess letterProcess=new LetterProcessImpl();
	public void sendLetter(String context,String address){
		letterProcess.writeContext(context);
		letterProcess.fillEnvelope(address);
		letterProcess.letterInotoEnvelope();
		letterProcess.sendLetter();
	}
}

场景类

public class Client {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MordenPostOffice postOffice=new MordenPostOffice();
		String adress="nanannananna";
		String context="happy birthday";
		postOffice.sendLetter(context, adress);
	}

}

邮件检查类

public class Police {
	public void checkLetter(ILetterProcess letterProcess){
		System.out.println("检查信件");
	}
}

场景类

public class MordenPostOffice {
	private ILetterProcess letterProcess=new LetterProcessImpl();
	private Police letterPolice=new Police();
	public void sendLetter(String context,String address){
		letterProcess.writeContext(context);
		letterProcess.fillEnvelope(address);
		letterPolice.checkLetter(letterProcess);
		letterProcess.letterInotoEnvelope();
		letterProcess.sendLetter();
	}
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值