设计模式中的组合与迭代器模式详解
1. 组合模式
组合模式旨在让树结构中的每个节点共享相同的抽象接口,使得整个结构可以作为一个统一的抽象结构使用,而无需暴露其内部表示。
1.1 绘图实现示例
在绘图操作中,不同对象有不同的绘制实现。例如,Vertex 的 drawWithContext: 方法实现如下:
- (void) drawWithContext:(CGContextRef)context
{
CGFloat x = self.location.x;
CGFloat y = self.location.y;
CGContextAddLineToPoint(context, x, y);
}
而 Stroke 的 drawWithContext: 方法需要将上下文移动到其子节点的第一个点,然后使用 Quartz 2D 函数完成整个线条绘制操作:
- (void) drawWithContext:(CGContextRef)context
{
CGContextMoveToPoint(context, self.location.x, self.location.y);
for (id <Mark> mark in children_)
{
[mark drawWithContext:context];
}
CGContextSetStroke
超级会员免费看
订阅专栏 解锁全文
1098

被折叠的 条评论
为什么被折叠?



