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

前言:

? ? ? ? 耦合這個(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)的真正目的。

?著作權(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),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 前言: 耦合這個(gè)詞我記得在高中物理就有電磁耦合、機(jī)械設(shè)備之間耦合的概念。 我們拿生活中的冰箱來(lái)舉個(gè)...
    2897275c8a00閱讀 903評(píng)論 0 0
  • 一、簡(jiǎn)介 <<UITableView(或簡(jiǎn)單地說(shuō),表視圖)的一個(gè)實(shí)例是用于顯示和編輯分層列出的信息的一種手段 <<...
    無(wú)邪8閱讀 10,962評(píng)論 3 3
  • 1.ios高性能編程 (1).內(nèi)層 最小的內(nèi)層平均值和峰值(2).耗電量 高效的算法和數(shù)據(jù)結(jié)構(gòu)(3).初始化時(shí)...
    歐辰_OSR閱讀 30,262評(píng)論 8 265
  • 上官網(wǎng)注冊(cè)賬號(hào) 首先來(lái)到環(huán)信的官網(wǎng),然后登陸.沒(méi)有賬號(hào)先注冊(cè)一個(gè). 進(jìn)去之后創(chuàng)建應(yīng)用,如圖 創(chuàng)建應(yīng)用界面 點(diǎn)擊確定...
    loneWolf01閱讀 564評(píng)論 0 0
  • 概述在iOS開(kāi)發(fā)中UITableView可以說(shuō)是使用最廣泛的控件,我們平時(shí)使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,300評(píng)論 3 38

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