美文网首页
高内聚低耦合

高内聚低耦合

作者: 哈布福禄克 | 来源:发表于2019-08-28 11:26 被阅读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架构的真正目的。

    降低耦合度的方法

    1、少使用类的继承,多用接口隐藏实现的细节。

    2、模块的功能化分尽可能的单一,道理也很简单,功能单一的模块供其它模块调用的机会就少。(其实这是高内聚的一种说法,高内聚低耦合一般同时出现)。

    3、遵循一个定义只在一个地方出现。

    4、少使用全局变量。

    5、类属性和方法的声明少用public,多用private关键字。

    6、多用设计模式,比如采用MVC的设计模式就可以降低界面与业务逻辑的耦合度。

    7、尽量不用“硬编码”的方式写程序,同时也尽量避免直接用SQL语句操作数据库。

    8、最后当然就是避免直接操作或调用其它模块或类(内容耦合);如果模块间必须存在耦合,原则上尽量使用数据耦合,少用控制耦合,限制公共耦合的范围,避免使用内容耦合。

    增强内聚度方法

    1、模块只对外暴露最小限度的接口,形成最低的依赖关系。

    2、只要对外接口不变,模块内部的修改,就不得影响其他模块。

    3、删除一个模块,应当只影响有依赖关系的其他模块,而不应该影响其他无关部分。

    相关文章

      网友评论

          本文标题:高内聚低耦合

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