设计模式应用场景之Model设计中可以用到的设计模式
代码实现如下:
@protocol Mark <NSObject,NSCopying> @property (nonatomic, strong) UIColor *color; @property (nonatomic, assign) CGSize size; @property (nonatomic, assign) CGPoint location; - (void)addMark:(id<Mark>)mark; - (void)removeMark:(id<Mark>)mark; - (void)removeAllMarks; - (id<Mark>)childAtIndex:(int)index; - (id<Mark>)lastChild; - (NSUInteger)count; @end
2.Stroke类实现Mark协议如下:
@interface Stroke() @property (nonatomic, strong) NSMutableArray<id<Mark>> *markArray; @end @implementation Stroke @synthesize color,location,size; #pragma mark - 属性操作 - (void)setLocation:(CGPoint)location {} - (CGPoint)location { if (_markArray.count) { return [[_markArray firstObject] location]; } else { return CGPointZero; } } #pragma mark - Mark操作 - (NSMutableArray<id<Mark>> *)markArray { if (!_markArray) { _markArray = @[].mutableCopy; } return _markArray; } - (void)addMark:(id<Mark>)mark { [self.markArray addObject:mark]; } - (void)removeMark:(id<Mark>)mark { //在当前节点中,就从当前节点删除,不在当前节点,就深度查找子节点 if ([_markArray containsObject:mark]) { [self.markArray removeObject:mark]; } else { [_markArray makeObjectsPerformSelector:@selector(removeMark:) withObject:mark]; } } - (void)removeAllMarks { [self.markArray removeAllObjects]; } - (id<Mark>)childAtIndex:(int)index { if (self.markArray.count == 0) { return nil; } else { return self.markArray[index]; } } - (id<Mark>)lastChild { return self.markArray.lastObject; } - (NSUInteger)count { return self.markArray.count; }
@implementation Vertex @synthesize location; @dynamic color,size; - (instancetype)initWithLocation:(CGPoint)location { self = [super init]; if (self) { self.location = location; } return self; } #pragma mark - 属性操作 - (UIColor *)color{return nil;} - (void)setColor:(UIColor *)color{} - (CGSize)size{return CGSizeZero;} - (void)setSize:(CGSize)size{} #pragma mark - Mark操作 - (void)addMark:(id<Mark>)mark {} - (void)removeMark:(id<Mark>)mark {} - (void)removeAllMarks {} - (id<Mark>)childAtIndex:(int)index {return nil;} - (id<Mark>)lastChild {return nil;} - (NSUInteger)count {return 0;}
4.Dot类实现Mark协议如下:
@interface Dot : Vertex @end @implementation Dot @synthesize color,size;
因为Dot类基础自Vertex,所以对象Mark对象的操作使用父类的就好了
迭代器模式篇
迭代器模式简介:
简单表示,迭代器持有着聚合对象
NSEnumerator *enumerator = [self enumerator]; id <Mark> mark; while (mark = [enumerator nextObject]) { [mark lastChild] }
self.stroke = [Stroke new]; [self.stroke enumerateMarksUsingBlock:^(id<Mark> mark, BOOL *stop) { }]
迭代器模式的实际使用例子:
代码实现如下:
Mark协议接口定义
@protocol Mark <NSObject,NSCopying> @property (nonatomic, strong) UIColor *color; @property (nonatomic, assign) CGSize size; @property (nonatomic, assign) CGPoint location; - (void)addMark:(id<Mark>)mark; - (void)removeMark:(id<Mark>)mark; - (void)removeAllMarks; - (id<Mark>)childAtIndex:(int)index; - (id<Mark>)lastChild; - (NSUInteger)count; - (instancetype)copy; //迭代器 - (NSEnumerator *)enumerator; @end;
Stroke类接口实现
@interface Stroke() @property (nonatomic, strong) NSMutableArray<id<Mark>> *markArray; @end @implementation Stroke @synthesize color,location,size; #pragma mark - 属性操作 - (void)setLocation:(CGPoint)location {} - (CGPoint)location { if (_markArray.count) { return [[_markArray firstObject] location]; } else { return CGPointZero; } } #pragma mark - Mark操作 - (NSMutableArray<id<Mark>> *)markArray { if (!_markArray) { _markArray = @[].mutableCopy; } return _markArray; } - (void)addMark:(id<Mark>)mark { [self.markArray addObject:mark]; } - (void)removeMark:(id<Mark>)mark { //在当前节点中,就从当前节点删除,不在当前节点,就深度查找子节点 if ([_markArray containsObject:mark]) { [self.markArray removeObject:mark]; } else { [_markArray makeObjectsPerformSelector:@selector(removeMark:) withObject:mark]; } } - (void)removeAllMarks { [self.markArray removeAllObjects]; } - (id<Mark>)childAtIndex:(int)index { if (self.markArray.count == 0) { return nil; } else { return self.markArray[index]; } } - (id<Mark>)lastChild { return self.markArray.lastObject; } - (NSUInteger)count { return self.markArray.count; } #pragma mark - 迭代器 - (NSEnumerator *)enumerator { return [[ZHFMarkEnumerator alloc] initWithMark:self]; }
代码实现如下:
@protocol Mark <NSObject,NSCopying> @property (nonatomic, strong) UIColor *color; @property (nonatomic, assign) CGSize size; @property (nonatomic, assign) CGPoint location; - (void)addMark:(id<Mark>)mark; - (void)removeMark:(id<Mark>)mark; - (void)removeAllMarks; - (id<Mark>)childAtIndex:(int)index; - (id<Mark>)lastChild; - (NSUInteger)count; - (instancetype)copy; //迭代器 - (NSEnumerator *)enumerator; //内部迭代器 - (void)enumerateMarksUsingBlock:(void (^) (id <Mark> mark, BOOL *stop))block; @end;
@interface Stroke() @property (nonatomic, strong) NSMutableArray<id<Mark>> *markArray; @end @implementation Stroke @synthesize color,location,size; #pragma mark - 属性操作 - (void)setLocation:(CGPoint)location {} - (CGPoint)location { if (_markArray.count) { return [[_markArray firstObject] location]; } else { return CGPointZero; } } #pragma mark - Mark操作 - (NSMutableArray<id<Mark>> *)markArray { if (!_markArray) { _markArray = @[].mutableCopy; } return _markArray; } - (void)addMark:(id<Mark>)mark { [self.markArray addObject:mark]; } - (void)removeMark:(id<Mark>)mark { //在当前节点中,就从当前节点删除,不在当前节点,就深度查找子节点 if ([_markArray containsObject:mark]) { [self.markArray removeObject:mark]; } else { [_markArray makeObjectsPerformSelector:@selector(removeMark:) withObject:mark]; } } - (void)removeAllMarks { [self.markArray removeAllObjects]; } - (id<Mark>)childAtIndex:(int)index { if (self.markArray.count == 0) { return nil; } else { return self.markArray[index]; } } - (id<Mark>)lastChild { return self.markArray.lastObject; } - (NSUInteger)count { return self.markArray.count; } #pragma mark - 迭代器 - (NSEnumerator *)enumerator { return [[ZHFMarkEnumerator alloc] initWithMark:self]; } - (void)enumerateMarksUsingBlock:(void (^)(id<Mark>, BOOL *stop))block { NSEnumerator *enumerator = [self enumerator]; id <Mark> mark; BOOL *stop = NO; for (id <Mark> mark in enumerator) { block(mark, &stop); if (stop) { break; } } }