File tree Expand file tree Collapse file tree 3 files changed +38
-102
lines changed
Expand file tree Collapse file tree 3 files changed +38
-102
lines changed Original file line number Diff line number Diff line change 2222是不一样的。装饰者模式是实实在在的为对象增加新的职责和行为,而代理做的事情还是跟本体
2323一样,最终都是设置 src。但代理可以加入一些“聪明”的功能,比如在图片真正加载好之前,先使用一张占位的 loadin 图片反馈给
2424
25- 状态模式和策略模式的关系
25+ 状态模式和策略模式的关系
26+
27+ 装饰者模式和代理模式也不会改变原有对象的接口,但装饰者模式的作用是为了给对象
28+ 增加功能。装饰者模式常常形成一条长的装饰链,而适配器模式通常只包装一次。代理
29+ 模式是为了控制对对象的访问,通常也只包装一次。
30+ 外观模式的作用倒是和适配器比较相似,有人把外观模式看成一组对象的适配器,但外
31+ 观模式最显著的特点是定义了一个新的接口
32+
Original file line number Diff line number Diff line change 1+ 适配器模式主要用来解决两个已有接口之间不匹配的问题,它不考虑这些接口是怎样实现的,也不考虑它们将来可能会如何演化。
2+ 适配器模式不需要改变已有的接口,就能够使它们协同作用。
3+
4+ < script type ="text/javascript ">
5+ var googleMap = {
6+ show : function ( ) {
7+ console . log ( '开始渲染谷歌地图' ) ;
8+ }
9+ } ;
10+ var baiduMap = {
11+ display : function ( ) {
12+ console . log ( '开始渲染百度地图' ) ;
13+ }
14+ } ;
15+ var renderMap = function ( map ) {
16+ if ( map . show instanceof Function ) {
17+ map . show ( ) ;
18+ }
19+ } ;
20+
21+ // 适配器
22+ var baiduMapAdapter = {
23+ show : function ( ) {
24+ return baiduMap . display ( ) ;
25+ }
26+ } ;
27+
28+ renderMap ( googleMap ) ; // 输出:开始渲染谷歌地图
29+ renderMap ( baiduMapAdapter ) ; // 输出:开始渲染百度地图
30+ </ script >
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments