美文网首页
高内聚低耦合

高内聚低耦合

作者: 哈布福禄克 | 来源:发表于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、删除一个模块,应当只影响有依赖关系的其他模块,而不应该影响其他无关部分。

相关文章

  • [JAVA] Java面向对象三大特征:封装、继承、多态

    高内聚和低耦合 面向对象的最终目的是要构建强健、安全、高效的项目,也就是要实现项目的高内聚和低耦合: 高内聚:把该...

  • 测试开发学习Day03

    面向对象原则:高内聚,低耦合。多聚合,少继承。 【高内聚、低耦合】内聚:每个模块尽可能独立完成自己的功能,不依赖于...

  • diango创建的一个简单的基本流程

    高内聚,低耦合 高内聚:一个模块内部一个功能。 低耦合:每个模块各司其职,尽量零关联。 创建django项目 1、...

  • 2021-08-31 IO流(装饰和继承的区别)

    继承结构组合结构高内聚,低耦合

  • 高内聚,低耦合

    概念 耦合性: 也称块间联系。指的软件系统结构中各模块间相互凉席紧密程度的一种度量。模块之间联系越紧密,其耦合性就...

  • 高内聚低耦合

    内聚是从功能角度来度量模块内的联系,一个好的内聚模块应当恰好做一件事。它描述的是模块内的功能联系; 耦合是软件结构...

  • 高内聚,低耦合

    面向对象思想无疑是目前最为流行的软件设计思想,它的强大之处在于可以将现实比较直观的投射到代码中,这个特点非常符合人...

  • 高内聚,低耦合

    "高内聚,低耦合",听过很多次,但是没有切身体会是不会真正明白的。我的深刻体会是在做一个公司的项目的时候,例如:a...

  • 高内聚、低耦合

    文章转自:http://www.cnblogs.com/robnetcn/archive/2012/04/15/2...

  • 高内聚低耦合

    高内聚: 高内聚是另外一个评判软件设计质量的标准。内聚更为专业的说法叫做功能内聚,是对系统中元素职责的相关性和集中...

网友评论

      本文标题:高内聚低耦合

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