談?wù)刬OS中的高內(nèi)聚低耦合

前言:

? ? ? ? 耦合這個(gè)詞我記得在高中物理就有電磁耦合、機(jī)械設(shè)備之間耦合的概念。

? ? ? ? 我們拿生活中的冰箱來舉個(gè)栗子:我們都知道冰箱主要由冷藏室和制冷機(jī)兩個(gè)主要部分組成,制冷機(jī)主要靠制冷管保持冷藏室低溫在沒有其他與其相連的部分,制冷機(jī)內(nèi)部有完成制冷功能的裝置,而冷藏室提供空間儲(chǔ)存需要保鮮或者冷藏的東西,這兩塊各自獨(dú)立僅靠很少的鏈接來完成各自的使命。各自獨(dú)立自己的功能封裝在自己內(nèi)部這就是高內(nèi)聚,僅靠很少的部分完成鏈接就是低耦合。

分析:

? ? ? ? 按照冰箱的例子我們分析:在我們的代碼中各個(gè)類自己的功能方法都在各自內(nèi)部封裝實(shí)現(xiàn)預(yù)留出兩個(gè)類之間的事件和值傳遞的接口就可以了。

代碼示例:

? ? ? ? 假設(shè)有這樣一個(gè)類來為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來說,

不推薦寫法

如果不考慮高內(nèi)聚低耦合我們這樣在視圖控制器中寫代碼

- (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];

}

推薦寫法

如果考慮高內(nèi)聚低耦合,在cell中代碼應(yīng)該這樣寫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

重寫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é):

? ? ? ? 推薦寫法主要是把cell的內(nèi)容賦值放在了自己內(nèi)部實(shí)現(xiàn),這樣就實(shí)現(xiàn)了高內(nèi)聚,而視圖控制器內(nèi)部只需要給cell的model屬性賦值即可這樣就實(shí)現(xiàn)低耦合。

? ? ? ? 其實(shí)這也就是MVC架構(gòu)的真正目的。

這就是個(gè)人對(duì)MVC和高內(nèi)聚低耦合的理解,喜歡的話可以看看我的其他文章

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容