一个IOS中的自定义控件,往往是一个UIView的子类,能够独立自主的完成一个特定功能。
能够称为控件,意味着它是由多个基本控件和控制方法组合在一起,我们默认为它的内部复杂且精密,经不起任何意外的变动。
把它看成是一个黑盒子,我们只考虑它的输入是什么,输出是什么。
我们来看一个最简单的封装例子
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
#import <UIKit/UIKit.h>
@protocol MyControlViewDelegate <NSObject>
@optional
- (void)myButtonTouched;
@end
@interface MyControlView : UIView
@property (nonatomic, weak) id<MyControlViewDelegate> delegate;
@end
#import "MyControlView.h"
@interface MyControlView()
@property (nonatomic, strong) UIButton *myButton;
@end
@implementation MyControlView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setupInterface];
}
return self;
}
- (void)setupInterface
{
self.myButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.myButton.frame = CGRectMake(50, 50, 200, 50);
[self.myButton setTitle:@"testButton" forState:UIControlStateNormal];
[self.myButton addTarget:self action:@selector(buttonTouched) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.myButton];
}
- (void)buttonTouched
{
if ([self.delegate respondsToSelector:@selector(myButtonTouched)]) {
[self.delegate myButtonTouched];
}
}
@end
|
这个例子封装了一个View,这个View里包含了一个按钮,当点击按钮的时候,会给它的delegate对象发送一个myButtonTouched消息。
从封装的角度来看,这个例子没有输入,只有一个delegate输出。我们修改需求,现在我们需要能够定制按钮的title。
有一种做法是把myButton这个属性设置为public属性,然后外部程序直接修改它的title。
还有一种做法是不开放myButton,而是设置一个工厂方法,在工厂方法里去定制myButton的title。
我们来分析一下这两种方法的通用性和安全性:
安全性:
采用第一种方法,当有多个外部类都引用了同一个例子对象,其中某个外部类将self.myButton设置为nil,很明显,其他外部类会出错。
第二种方法则不会。
通用性:
采用第一种方法,当有多个外部类使用了例子类,当例子类进行版本升级,修改了按钮的变量名,其他外部类会出错,必须逐个找出调用的具体代码行进行修改。
第二种方法不需要这样。
上面的例子告诉我们封装的第一个原则:
不要让任何外部类能够看到封装类的内部变量或内部方法。
如果将某个内部方法开放出来作为对外的接口,则必须在任意版本中保持方法的输入,输出及方法名称不变。
刚才例子的变化版本:
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
#import <UIKit/UIKit.h>
@protocol MyControlViewDelegate <NSObject>
@optional
- (void)myButtonTouched;
@end
@interface MyControlView : UIView
@property (nonatomic, weak) id<MyControlViewDelegate> delegate;
- (id)initWithFrame:(CGRect)frame ButtonTitle:(NSString *)buttonTitle;
@end
#import "MyControlView.h"
@interface MyControlView()
@property (nonatomic, strong) NSString *buttonTitle;
@property (nonatomic, strong) UIButton *myButton;
@end
@implementation MyControlView
- (id)initWithFrame:(CGRect)frame ButtonTitle:(NSString *)buttonTitle
{
self = [super initWithFrame:frame];
if (self) {
self.buttonTitle = buttonTitle;
[self setupInterface];
}
return self;
}
- (void)setupInterface
{
self.myButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.myButton.frame = CGRectMake(50, 50, 200, 50);
[self.myButton setTitle:self.buttonTitle forState:UIControlStateNormal];
[self.myButton addTarget:self action:@selector(buttonTouched) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.myButton];
}
- (void)buttonTouched
{
if ([self.delegate respondsToSelector:@selector(myButtonTouched)]) {
[self.delegate myButtonTouched];
}
}
@end
|