您的位置:首页 > 其它

Office 2016 For Mac 安装及破解方法

2015-10-26 11:13 495 查看


MVC中的设计模式

一个以MVC为架构的系统包含了很多的设计模式,但是与MVC最为密切相关的是下面三种模式:Observer, Composite和Strategy。

1. Observer模式

MVC通过使用定购/通知的方式分离了Model和View。View要保证自己显示能正确地反映出Model的内容和状态。一旦Model的内容发生变化,必须有一个机制来使得Model能够通知相关的View,使得相关的View可以在适当的时机刷新数据。这个设计还可以解决更一般的问题,将对象分离,使得一个对象的改变能够影响到另一些对象,而这个对象并不知道那些被影响的对象的细节。这就是被描述为Observer的设计模式。

2. Composite模式

MVC的一个重要特征就是View可以嵌套。嵌套的组合视图可用于任何视图可用的地方,而且可以管理嵌套视图。这种思想反映出将组合的视图与其组件平等对待的设计。这种设计思想在面向对象领域内被描述成为Composite的设计模式。

模式类型:Composite模式是对象型模式,同时它也是结构型模式。

3. Strategy模式

MVC的另一重要特征是可以在不改变View的情况下改变View对用户输入的响应方式。这对一个经常需要变更响应逻辑的系统来说是非常重要的。MVC把响应逻辑封装在Controller中。有一个Controller的类层次结构,可以方便地对原有Controller做适当改变,创建新的 Controller。View使用Controller子类的实例来实现一个特定的响应策略。要实现不同的响应策略,只要用不同种类的 Controller实例替换即可。还可以在运行时刻通过改变View的Controller来改变View对用户输入的响应策略。这种View- Controller的关系是被描述为Strategy的设计模式的一个例子。

 

其中说到Compoite模式,View可以嵌套,但是在Pivot中的应用Composite模式的却是Component,而这个Component却是Controller.下面是Pivot贡献者 Greg Brown在邮件列表中的说明:

 

"Components represent the "controller" in Pivot's MVC implementation, and skins represent the "view". A skin defines the overall L&F of a component, but it doesn't define how the component's content is painted. That's where renderers come in. However, based on my understanding of your app's requirements, I'm not sure you would need to support a renderer (not all skins do).

Decorators are used to augment a component's default paint behavior. They allow a caller to hook into the paint process before and after a component is painted. Skins often take advantage of them, but they don't necessarily have to. In other words, there's no need to use a decorator if you can implement the behavior you need within the skin's paint() met

hod."

 

Pivot中的View是Skin,不是嵌套的,Component的子类Container才是。

还有一个细节,上面提到说 skin负责 L&F ,但事实上在Pivot中真正负责L&F的是Visual接口,包括Component,也包括Skin.

说Skin负责L&F,是因为Component的paint方法都交给Skin代理了,那么就会问一个问题,是否Container所有的children components也是在Container的Skin中做的?这样未免太麻烦了,Container的Skin还得到Container中去找children components, component的paint又由comp的Skin负责。。。

 

所以Pivot的处理时,ContainerSkin只负责Container自己的,不负责Container的子,Container的子都在它的paint方法中做。所以Container的paint方法覆盖了Component的paint,两者有区别。

 

总结之, Container和Component的机制稍微不一样,普通Comp由Skin代理,容器Container由Skin+遍历子(skin+layout)实现。Pivot用户不需要关心这个区别,也不用注意Composite,只需要在Skin中定义L&F就行了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: