关于delegate在解耦合的应用

代理的作用

    1.封装变化点

    2.解决类与类之间的强耦合性

    3.可以在类与类之间进行数据传递

什么强耦合呢?

  对于软件开发来说,就是代码与代码,类与类之间的高度依赖性。如果一个类的文件发生了改变,那么其它的类文件必须做出修改,造成了工程的开发复杂度提高,降低了开发效率。

 那么,在什么时候使用代理呢?

  有一系列变化点,能够让用户清晰地知道到底有哪些变化点被封装起来

例如:在UITableView中(如下)

这些协议方法都是属于变化点,不同代理,带来的也会是不同的反馈

@required

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

@optional

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;       

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView; 

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index; 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;

 

 

posted @ 2015-08-12 11:54  一念寻风  阅读(289)  评论(0)    收藏  举报