耦合并非软件开发中的专用语言,在初中和高中物理中就有电磁学方面的耦合,还有印象中机械设备之间也有耦合的概念,因此软件开发中的耦合只是来源于生活,用之于软件工程学。设计模式
就拿机械设备中的耦合来类比说明吧,咱们都知道长途货运车,把车头和车身分别想象成两个不一样功能的模块,车头的发动机负责驱动,车身负责装载货物,两个模块之间应该是较低的耦合度,只要车头和车身之间有一个坚固连接的铁轴,这是它们之间耦合度的一个主要结构,再附加上一些电路线之类的零件用以从车头控制车身的灯光,这基本就是二者之间的低耦合的体现;而车身和车头自身则要高内聚,车身主要提供宽敞的空间来存放货物,车头内部复杂的电路系统和发动机驱动,用来给整个车子提供动力,以及控制电路系统。数组
那么在回到iOS开发过程当中,低耦合和高内聚的体现,如今有一个自定义的UITableViewCell,用于显示一个用户发布的内容,显示的界面包括用户名username、时间time、头像header、发布的内容content、点赞的数量like,因此这个自定义Cell包括的UI元素以下,优化
@interface CustomTableViewCell:UITableViewCellatom
@property (nonatomic, strong) UILabel *timeLabel;spa
@property (nonatomic, strong) UILabel *usernameLabel;.net
@property (nonatomic, strong) UIImageView *headerImageView;设计
@property (nonatomic, strong) UILabel *contentLabel;orm
@property (nonatomic, strong) UILabel *likeLabel;对象
@end 开发
咱们都知道Cell数值内容的获取是在
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{}中实现的,
咱们在{}括号中来写一段比较菜的代码,
{
//.....
//关键代码
NSString *name = [_names objectAtIndex:indexPath.row];
NSString *time = [_times objectAtIndex:indexPath.row];
UIImage *header = [UIImage imageNamed:[_headers objectAtIndex:indexPath.row]];
NSString *content = [_contents objectAtIndex:indexPath.row];
NSNumber *like = [likes objectAtIndex:indexPath.row];
//.....
cell.timeLabel.text = time;
cell.usernameLabel.text = name;
cell.headerImageView.image = header;
cell.contentLabel.text = content;
cell.likeLabel.text = [NSString stringWithFormart:@"%@",like];
}
这样的代码写的是颇有问题的,首先就没有将信息所包含的对象给封装起来,咱们把包含time、username、header、content、like的糗事封装成对象Qiushi(糗事),下面是Qiushi对象的代码,
@interface Qiushi:NSOjbect
@property (nonatomic, strong) NSString *username;
@property (nonatomic, strong) NSString *time;
@property (nonatomic, strong) NSString *content;
@property (nonatomic, strong) NSString *header;
@property (nonatomic, strong) NSNumber *like;
在CustomTableViewCell所在的ViewController中将qiushi加入qiushiArray数组,而后在
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{}
的括号{}中这样写,
{
//.....
//关键代码
Qiushi *qiushi = [qiushiArray objectAtindex:indexPath.row];
cell.timeLabel.text = qiushi.time;
cell.usernameLabel.text = qiushi.username;
cell.headerImageView.image = [UIImage imageNamed:qiushi.header];
cell.contentLabel.text = qiushi.content;
cell.likeLabel.text = [NSString stringWithFormart:@"%@",qiushi.like];
//.....
}
这样咱们将username、time、content、like、header等属性封装在Qiushi这个对象中,这就是初级的MVC使用了。那么看上面的代码,有没有以为不妥的地方,你看上面红色的cell.timeLabel、cell.usernameLabel。。。等等赋值方法,像不像Cell对象与ViewController之间的连线,这种状况就是高耦合了,由于链接的线太多了,为了下降耦合度,咱们给CustomTableViewCell添加Qiushi属性变量,以下,
@interface CustomTalbeViewCell:UITableViewCell
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UILabel *usernameLabel;
@property (nonatomic, strong) UIImageView *headerImageView;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UILabel *likeLabel;
@property (nonatomic, strong) Qiushi *qiushi;//新增长的Qiushi对象属性变量
在- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{}
的括号{}中这样写,
{
//.....
//关键代码
Qiushi *qiushi = [qiushiArray objectAtindex:indexPath.row];
cell.qiushi = qiushi;
//.....
}
这样是否是在Controller中少了不少代码,这时候得到了qiushi数据的cell对象,想怎么在其UIView控件上面显示数据就让它本身去作,好比,能够在其内部这样写代码填充UILabel、UIImageView上面的数据内容,
- (void)setQiushi:(Qiushi)qs
{
self.timeLabel.text = qs.username;
self.contentLabel.text = qs.content;
self.header.image = [UIImage imageNamed:qs.header];
....
}
这样就是使用MVC下降耦合度,提升了代码的可读性,即Controller做为View和Model(此处就是Qiushi)之间的桥梁,View不与Model直接交流,只负责显示Controller传递给它的数据。
这就是为何说MVC是iOS设计模式之王,由于使用MVC能够有效的将项目的模块分工明确,下降了不一样模块之间的耦合度,提升了可读性和可扩展性。
上面是我本身总结的一个使用场景,固然还有更多的场景,须要下降模块之间的耦合度,好比两个Controller页面之间切换,ViewController1 push进入ViewController2,那么第二个ViewController2之间的数据显示什么内容,彻底就是ViewController1决定的,你能够想下面这样写代码,
ViewController1
{
ViewController2 VC2 = [[ViewController2 alloc] init];
VC2.name = @"xxxx";
VC2.content = @"xxxx";
[VC2 setTitle:@"xxx" image:[UIImage imageNamed:@"xxx"]];
[self.navigationController pushToViewContorller:VC2];
}
看看上面的代码,经过三条语句来给VC2赋值,咱们都能看出来哪里很差,优化的方式就是尽可能减小VC2方法的调用和属性的赋值,能经过尽可能少的代码进行VC2中初始化赋值最好。