我从Cocoa一书中了解到,许多Cocoa类都使用装饰器模式,包括NSAttributedString (它不是从NSString继承的)。我的NSAttributedString.m和它超出了我的头,但我想知道是否有人已经成功地实现了这个模式,他们愿意分享。
这些需求来自于这个装饰图案引用,而且由于在Objective中没有抽象类,所以Component和Decorator应该与抽象类相类似,以满足它们最初的目的(也就是说,我不认为它们可以是协议,因为您必须能够执行[super operation]。
看到你的一些装饰器的实现,我会非常兴奋的。
发布于 2012-06-08 15:12:07
我在我的一个应用程序中使用了它,其中我有一个单元格的多重表示,一个单元格有一个边框,一个单元格有附加的按钮和一个有纹理图像的单元格,我也需要按一下按钮就改变它们。
下面是我使用的一些代码
//CustomCell.h
@interface CustomCell : UIView
//CustomCell.m
@implementation CustomCell
- (void)drawRect:(CGRect)rect
{
//Draw the normal images on the cell
}
@end以及具有边界的自定义单元
//CellWithBorder.h
@interface CellWithBorder : CustomCell
{
CustomCell *aCell;
}
//CellWithBorder.m
@implementation CellWithBorder
- (void)drawRect:(CGRect)rect
{
//Draw the border
//inset the rect to draw the original cell
CGRect insetRect = CGRectInset(rect, 10, 10);
[aCell drawRect:insetRect];
}现在,在我的视图控制器中,我将执行以下操作
CustomCell *cell = [[CustomCell alloc] init];
CellWithBorder *cellWithBorder = [[CellWithBorder alloc] initWithCell:cell];如果以后我想换另一个牢房的话
CellWithTexture *cellWithBorder = [[CellWithTexture alloc] initWithCell:cellWithBorder.cell];https://stackoverflow.com/questions/10951080
复制相似问题