设计模式中的外观模式的作用是给一些子模块封装方法,它本身不实现额外的功能。通过外观类的封装,使得外界调用接口的时候不必清楚各个子模块的实现细节,只需知道调用哪个方法就可以了。当然,有了外观类,也是可以直接调用子模块中的方法的。
PureMVC中的外观类也是一个单例。一般一个工程中只需你自己继承该Facade类并实现自己的一个外观类就可以了,并不需要实现多个。
下面就看下ooc-lang的代码:
/*
* Facade.ooc
* 外观层
*/
// 相关模块的引入
import interfaces/[IFacade, ICommand, INotifier]
import core/[Controller, Model, View]
import patterns/observer/Notification
import patterns/proxy/Proxy
import patterns/mediator/Mediator
// Facade自身也可以发通知的,所以继承了INotifier,
Facade: class extends INotifier implements IFacade {
// 这三个变量用来保存MVC三层的单例引用,这样就不用每次都调用它们获取单例的函数了
controller: Controller
model: Model
view: View
// 类的唯一实例
instance: static Facade
SINGLETON_MSG := "Facade Singleton already constructed!"
// 构造函数
init: func {
if (instance != null) {
Exception new(SINGLETON_MSG) throw()
}
"[ Facade ] new()" println()
instance = this
initializeFacade