Shiro使用到的典型设计模式Facade
1.Facade定义
首先,来看一段关于Facade模式的定义:Facade Pattern says that just “just provide a unified and simplified interface to a set of interfaces in a subsystem, therefore it hides the complexities of the subsystem from the client”.
翻译一下就是,门面模式(又称外观模式,我更喜欢门面模式的翻译)只是为子系统的一组接口提供了统一和简化的接口,因此它对客户端隐藏了子系统的复杂性。
In other words, Facade Pattern describes a higher-level interface that makes the sub-system easier to use.
换句话说,Facade Pattern描述了一个更高级别的接口,它使子系统更易于使用。
Practically, every Abstract Factory is a type of Facade.
实际上,每个抽象工厂都是一种门面。
2.Facade模式典型类图
以下是一个典型的Facade模式类图,Facade类对客户Client类隐藏了具体实现逻辑,同时,也提供了访问子系统类的接口。
3.Shiro的Facade模式类图
上一篇文章Shiro源码解析之认证代码走查中最后章节提供了一张类图,其实已经能够看出来逻辑,现在参照第2点的典型类图做一下简化如下:
SecurityManager就是Facade角色类,Subject的实现类DelegatingSubject中有一个SecurityManager类型的成员变量,而SecurityManager是继承自Authenticator接口和Authorizer接口,因此,也会继承接口的方法,所以,subject对象的所有方法都能够交给SecurityManager这个Facade类来处理,而不用去关心子类的具体细节(虚线框内,登录认证、授权的过程,对subject都是隐藏实现的)。
以上就是我个人关于Shiro典型的Facade模式应用的理解。