前言:
? ? ? ? 耦合這個(gè)詞我記得在高中物理就有電磁耦合、機(jī)械設(shè)備之間耦合的概念。
? ? ? ? 我們拿生活中的冰箱來(lái)舉個(gè)栗子:我們都知道冰箱主要由冷藏室和制冷機(jī)兩個(gè)主要部分組成,制冷機(jī)主要靠制冷管保持冷藏室低溫在沒(méi)有其他與其相連的部分,制冷機(jī)內(nèi)部有完成制冷功能的裝置,而冷藏室提供空間儲(chǔ)存需要保鮮或者冷藏的東西,這兩塊各自獨(dú)立僅靠很少的鏈接來(lái)完成各自的使命。各自獨(dú)立自己的功能封裝在自己內(nèi)部這就是高內(nèi)聚,僅靠很少的部分完成鏈接就是低耦合。
分析:
? ? ? ? 按照冰箱的例子我們分析:在我們的代碼中各個(gè)類自己的功能方法都在各自內(nèi)部封裝實(shí)現(xiàn)預(yù)留出兩個(gè)類之間的事件和值傳遞的接口就可以了。
代碼示例:
? ? ? ? 假設(shè)有這樣一個(gè)類來(lái)為cell賦值
@interface Feeling : NSOjbect
@property (nonatomic, strong) NSString *userName;
@property (nonatomic, strong) NSString *creatTime;
@property (nonatomic, strong) NSString *feelingContent;
@property (nonatomic, strong) NSString *headerImgUrl;
@property (nonatomic, strong) NSNumber *likeStatus;
@end
? ? ? ? 有如下cell
@interface FeelingTableViewCell : UITableViewCell
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UILabel *userNameLabel;
@property (nonatomic, strong) UIImageView *headerImageView;
@property (nonatomic, strong) UILabel *FeelingContentLabel;
@property (nonatomic, strong) UILabel *likeLabel;
@end
我們拿tableView和cell來(lái)說(shuō),
不推薦寫(xiě)法
如果不考慮高內(nèi)聚低耦合我們這樣在視圖控制器中寫(xiě)代碼
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
FeelingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FeelingCellId forIndexPath:indexPath];
Feeling *feeling = [feelingArray objectAtindex:indexPath.row];
cell.timeLabel.text = feeling.creatTime;
cell.usernameLabel.text = feeling.userName;
cell.headerImageView.image = [UIImage imageNamed:feeling.headerImgUrl];
cell.contentLabel.text = feeling.feelingContent;
cell.likeLabel.text = [NSString stringWithFormart:@"%@",feeling.like];
}
推薦寫(xiě)法
如果考慮高內(nèi)聚低耦合,在cell中代碼應(yīng)該這樣寫(xiě)cell.h
@class Feeling;
@interface FeelingTableViewCell : UITableViewCell
@property (nonatomic, strong) Feeling *feelingModel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UILabel *userNameLabel;
@property (nonatomic, strong) UIImageView *headerImageView;
@property (nonatomic, strong) UILabel *FeelingContentLabel;
@property (nonatomic, strong) UILabel *likeLabel;
@end
重寫(xiě)feelingModel的set方法
- (void)setFeelingModel:(Feeling*)feelingModel{
_feelingModel = feelingModel;
self.timeLabel.text = feeling.creatTime;
self.usernameLabel.text = feeling.userName;
self.headerImageView.image = [UIImage imageNamed:feeling.headerImgUrl];
self.contentLabel.text = feeling.feelingContent;
self.likeLabel.text = [NSString stringWithFormart:@"%@",feeling.like];
}
在視圖控制器中
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
FeelingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FeelingCellId forIndexPath:indexPath];
cell.feelingModel = [feelingArray objectAtindex:indexPath.row];
}
總結(jié):
? ? ? ? 推薦寫(xiě)法主要是把cell的內(nèi)容賦值放在了自己內(nèi)部實(shí)現(xiàn),這樣就實(shí)現(xiàn)了高內(nèi)聚,而視圖控制器內(nèi)部只需要給cell的model屬性賦值即可這樣就實(shí)現(xiàn)低耦合。
? ? ? ? 其實(shí)這也就是MVC架構(gòu)的真正目的。