iOS设计模式 - 模板
原理图
说明
定义一个操作中的算法的骨架,而将步骤延迟到子类中。模板方法使得子类可以不改变一个算法的结构即可重定义算法的某些特定步骤。
源码
https://github.com/YouXianMing/iOS-Design-Patterns
//
// GameProtocol.h
// TemplatePattern
//
// Created by YouXianMing on 15/10/27.
// Copyright © 2015年 ZiPeiYi. All rights reserved.
//
#import <Foundation/Foundation.h>
@protocol GameProtocol <NSObject>
@required
/**
* 设置玩家个数
*
* @param count 数目
*/
- (void)setPlayerCount:(int)count;
/**
* 返回玩家数目
*
* @return 玩家数目
*/
- (int)playerCount;
/**
* 初始化游戏
*/
- (void)initializeGame;
/**
* 开始游戏
*/
- (void)makePlay;
/**
* 结束游戏
*/
- (void)endOfGame;
@end
//
// Monopoly.h
// TemplatePattern
//
// Created by YouXianMing on 15/10/27.
// Copyright © 2015年 ZiPeiYi. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "GameProtocol.h"
@interface Monopoly : NSObject <GameProtocol>
@end
//
// Monopoly.m
// TemplatePattern
//
// Created by YouXianMing on 15/10/27.
// Copyright © 2015年 ZiPeiYi. All rights reserved.
//
#import "Monopoly.h"
@interface Monopoly ()
@property (nonatomic, assign) int gamePlayerCount;
@end
@implementation Monopoly
- (void)setPlayerCount:(int)count {
self.gamePlayerCount = count;
}
- (int)playerCount {
return self.gamePlayerCount;
}
- (void)initializeGame {
NSLog(@"Monopoly initialize");
}
- (void)makePlay {
NSLog(@"Monopoly makePlay");
}
- (void)endOfGame {
NSLog(@"Monopoly endOfGame");
}
@end
//
// Chess.h
// TemplatePattern
//
// Created by YouXianMing on 15/10/27.
// Copyright © 2015年 ZiPeiYi. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "GameProtocol.h"
@interface Chess : NSObject <GameProtocol>
@end
//
// Chess.m
// TemplatePattern
//
// Created by YouXianMing on 15/10/27.
// Copyright © 2015年 ZiPeiYi. All rights reserved.
//
#import "Chess.h"
@interface Chess ()
@property (nonatomic, assign) int gamePlayerCount;
@end
@implementation Chess
- (void)setPlayerCount:(int)count {
self.gamePlayerCount = count;
}
- (int)playerCount {
return self.gamePlayerCount;
}
- (void)initializeGame {
NSLog(@"Chess initialize");
}
- (void)makePlay {
NSLog(@"Chess makePlay");
}
- (void)endOfGame {
NSLog(@"Chess endOfGame");
}
@end
细节