美文网首页
谈谈iOS中的高内聚低耦合

谈谈iOS中的高内聚低耦合

作者: 使徒行者24 | 来源:发表于2018-09-10 10:41 被阅读0次

    前言:

            耦合这个词我记得在高中物理就有电磁耦合、机械设备之间耦合的概念。

            我们拿生活中的冰箱来举个栗子:我们都知道冰箱主要由冷藏室和制冷机两个主要部分组成,制冷机主要靠制冷管保持冷藏室低温在没有其他与其相连的部分,制冷机内部有完成制冷功能的装置,而冷藏室提供空间储存需要保鲜或者冷藏的东西,这两块各自独立仅靠很少的链接来完成各自的使命。各自独立自己的功能封装在自己内部这就是高内聚,仅靠很少的部分完成链接就是低耦合。

    分析:

            按照冰箱的例子我们分析:在我们的代码中各个类自己的功能方法都在各自内部封装实现预留出两个类之间的事件和值传递的接口就可以了。

    代码示例:

            假设有这样一个类来为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来说,

    不推荐写法

    如果不考虑高内聚低耦合我们这样在视图控制器中写代码

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

    }

    推荐写法

    如果考虑高内聚低耦合,在cell中代码应该这样写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];

    }

    总结:

            推荐写法主要是把cell的内容赋值放在了自己内部实现,这样就实现了高内聚,而视图控制器内部只需要给cell的model属性赋值即可这样就实现低耦合。

            其实这也就是MVC架构的真正目的。

    相关文章

      网友评论

          本文标题:谈谈iOS中的高内聚低耦合

          本文链接:https://www.haomeiwen.com/subject/rtajgftx.html