Gof 笔记 - 2

四、Structural Patterns

  • Adapter

1、意图

Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

将一个类的接口转换成客户希望的另一个接口,Adapter让因为接口不兼容而不能一起工作的类,一起工作。

2、下图说的是一个绘图编辑器,对于文本比较特殊,假设已有TextView,我们通过TextShap来适配TextView和Shap

3、structure

class adapter:(通常 public 继承 Target,private 继承 Adaptee)

 

object adapter:

变种,双向适配,既有数据从Unidraw到QOCA,又有数据从QOCA到Unidraw

  • Bridge

1、意图

 

Decouple an abstraction from its implementation so that the two can vary independently.

 

将抽象和实现去耦合,使得他们可以独立变化

2、一个界面工具箱,要能在不同系统(X Window System、IBM's Presentation Manager)上工作

3、structure

4、补充

Client为灰色表示其不是该模式的参与者,只是为了说明其与参与者之间的交互关系

  • composite

1、意图

 

 

Compose objects into tree structures to represent part-whole hierarchies.Composite lets clients treat individual objects and compositions of objects uniformly.

 

将对象组合成树形结构,来表示“部分-整体”的层次。Composite 让客户一致的对待单个和组合对象。

2、绘图编辑器内容可能是简单图元,也可能是简单图元组成的容器

组合起来如下:

3、structure

4、补充

有时componet和composite可能合并成一个,但最好不要那么做

通常和Decorator一起使用,继承一个共有的父类Component

  • decorator

1、意图

 

Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

给一个对象动态的添加额外的职能。扩展功能方面,Decorators相对于子类化,提供了一个灵活的选择。

 

2、给TextView添加边框或者滚动条等

客户端关键代码:

 

3、structure

4、补充

 

A decorator is different from an adapter in that a decorator only changes an object's responsibilities, not its interface; an adapter will give an object a completely new interface.

Decorator改变对象的职责(responsibilities),而Adapter给一个对象提供一个全新的接口(复用为主)。

 

Strategy改变对象的内核,Decorator改变对象的外壳

  • Facade

1、意图

 

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.

提供一个统一的接口,为子系统的一组接口。Facade为了子系统更容易使用,定义了一个高级接口

2、

3、structure

  • Flyweight

1、意图

Use sharing to support large numbers of fine-grained objects efficiently.

使用共享技术有效支持大量细粒度的对象。

2、这里列举了一个文档编辑器,字符就那么几种,没必要为每一个字符都建立一个对象,相同类型的字符可以共享一个对象,这个图跟组合模式结合了一下

3、structure

4、补充

 

Intrinsic state is stored in the flyweight; it consists of information that's independent of the flyweight's context, thereby making it sharable. Extrinsic state depends on and varies with the flyweight's context and therefore can't be shared.

内部状态存储在flyweight中,它包含了不依赖 flyweight 场景的信息,从而可以共享;外部状态依赖且随 flyweight 场景而变化,它不能共享。

 

Sharability also implies some form of reference counting or garbage collection to reclaim a flyweight's storage when it's no longer needed.

共享也提供了引用计数或垃圾回收的一些形式,当 flyweight 不使用时可以回收其存储空间。

 

 

  • Proxy

1、意图

Provide a surrogate or placeholder for another object to control access to it.

为对象提供一个代理来控制对其访问。

2、一个文档编辑器,可以嵌入图形,打开文件的时候,图形加载比较慢,所以通过一个图形代理,当真正显示图形(Draw)的时候才加载它。现在网页技术中很多应该用到了这个技术,页面可以很快打开,当你往下来页面时候,图形才慢慢显示出来。

3、structure

4、补充

代理通常有4种:远程代理(Rmote Proxy)、虚代理(Virtual Proxy)、保护代理(Protection Proxy)、智能指引(Smart Reference)

copy-on-write技术

  • 补充说明

 

a facade defines a new interface, whereas an adapter reuses an old interface.

facade定义了一个新的接口,而adapter复用旧的接口。

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值