Contents
  1. 1. 模板模式、提供一些基础的模板方法、步骤性的逻辑代码框架给其他的子类.
  2. 2. 抽象模板接口
  3. 3. 默认实现类作为抽象类
  4. 4. 具体模板

模板模式、提供一些基础的模板方法、步骤性的逻辑代码框架给其他的子类.

之前BaseViewController使用过这个模式.


抽象模板接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//
// Template.h
// demos
//

#import <Foundation/Foundation.h>

@protocol Template <NSObject>


////////////////////////////////////////////////////////////
/////每一个步骤方法可以选择重写实现
////////////////////////////////////////////////////////////
- (void)step1;

- (void)step2;

- (void)step3;

- (void)step4;


////////////////////////////////////////////////////////////
/////业务逻辑步骤不能重写
////////////////////////////////////////////////////////////
- (void)business;

@end

默认实现类作为抽象类

1
2
3
4
5
6
7
8
9
10
11
12
13
//
// AbstractTemplate.h
// demos
//

#import <Foundation/Foundation.h>

//实现模板接口
#import "Template.h"

@interface AbstractTemplate : NSObject <Template>

@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//
// AbstractTemplate.m
// demos
//

#import "AbstractTemplate.h"

@implementation AbstractTemplate

- (void)step1 {}
- (void)step2 {}
- (void)step3 {}
- (void)step4 {}

- (void)business {
[self step1];
[self step2];
[self step3];
[self step4];
}

@end

具体模板

1
2
3
4
5
6
7
8
9
10
11
12
//
// ConcreatTemplate.h
// demos
//

#import <Foundation/Foundation.h>

#import "AbstractTemplate.h"

@interface ConcreatTemplate : AbstractTemplate

@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//
// ConcreatTemplate.m
// demos
//

#import "ConcreatTemplate.h"

@implementation ConcreatTemplate

- (void)step1 {
NSLog(@"步骤1");
}

- (void)step2 {
NSLog(@"步骤2");
}

- (void)step3 {
NSLog(@"步骤3");
}

- (void)step4 {
NSLog(@"步骤4");
}

@end

感觉有点像 建造者 模式.

Contents
  1. 1. 模板模式、提供一些基础的模板方法、步骤性的逻辑代码框架给其他的子类.
  2. 2. 抽象模板接口
  3. 3. 默认实现类作为抽象类
  4. 4. 具体模板