设计模式:组合与迭代器模式解析
1. 组合模式概述
组合模式的核心目标是让树结构中的每个节点共享相同的抽象接口,这样整个结构就能作为一个统一的抽象结构使用,而无需暴露其内部表示。对每个节点(无论是叶子节点还是组合节点)的操作都可以通过协议或抽象基类中定义的相同接口进行。
1.1 绘图实现示例
在绘图操作中,不同对象的绘图实现有所不同。
- Vertex 的绘图实现 :
- (void) drawWithContext:(CGContextRef)context
{
CGFloat x = self.location.x;
CGFloat y = self.location.y;
CGContextAddLineToPoint(context, x, y);
}
- Stroke 的绘图实现 :
- (void) drawWithContext:(CGContextRef)context
{
CGContextMoveToPoint(context, self.location.x, self.location.y);
for (id <Mark> mark in children_)
{
[mark drawWithContext:context];
}
CGContextSetStrokeCol
超级会员免费看
订阅专栏 解锁全文

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



