美文网首页
iOS设计模式之策略(strategy)模式

iOS设计模式之策略(strategy)模式

作者: 52xpz | 来源:发表于2017-08-02 20:49 被阅读53次

1、定义

定义一系列算法,把它们一个个封装起来,并且使它们可相互替换。本模式使得算法可独立于使用它的客户而变化。

如上UML类图,context类可以通过他持有的Strategy对象来创建不同的ConcreteStrategy对象,从而调用不同的algorithmInterface方法。

2、使用场景

在大量使用if-else或switch-case条件的业务场景下可以使用策略模式。

3、示例

比如今日头条新闻列表根据不同的新闻类型创建不同的cell就可以采用策略模式。如下代码所示

#define kClassKey @"kClassKey"

#define kModelKey @"kModelKey"

#define kReuseKey @"kReuseKey"

#define CellAID @"cellA"

#define CellBID @"cellB"

@interface PCCarOrderDetailViewController ()

@property (nonatomic,strong) NSMutableArray *dataSource;

@end

- (void)viewDidLoad

{

[super viewDidLoad];

[self registerTableViewCell]; //注册CellA和CellB

[self createDataSourceAndReloadData]; //构建数据源

}

- (void)registerTableViewCell

{

//注册cell

[self registerTableViewCellNibWithClass:[PCCellA class] reuseId:CellAID];

[self registerTableViewCellNibWithClass:[PCCellB class] reuseId:CellBID];

}

- (void)registerTableViewCellNibWithClass:(Class)cls reuseId:(NSString *)reuseId

{

if (!cls || !reuseId)

{

return;

}

[self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass(cls) bundle:nil] forCellReuseIdentifier:reuseId];

}

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

{

//通过策略模式避免了大量的if-else-if-else判断

NSDictionary* dic = self.dataSource[indexPath.row];

PCBaseCell *cell = [tableView dequeueReusableCellWithIdentifier:dic[kReuseKey] forIndexPath:indexPath];

if ([cell respondsToSelector:@selector(initWithModel:)])

{

[cell initWithModel:dic[kModelKey]];

}

return cell;

}

- (void)createDataSourceAndReloadData

{

self.dataSource = [NSMutableArray array];

//订单状态

[self.dataSource addObject:[NSDictionary dictionaryWithObjectsAndKeys:[PCCellA class], kClassKey, [[PCModel alloc] init], kModelKey, CellAID, kReuseKey, nil]];

[self.dataSource addObject:[NSDictionary dictionaryWithObjectsAndKeys:[PCCellB class], kClassKey, [PCModel alloc] init], kModelKey, CellBID, kReuseKey, nil]];

[self.tableView reloadData];

}

@interface PCBaseCell : UITableViewCell

//子类可重写该类方法

- (void)initWithModel:(id)model;

@end

@interface PCCellA : PCBaseCell

- (void)initWithModel:(id)model; //重写该方法

@end

@interface PCCellB : PCBaseCell

- (void)initWithModel:(id)model; //重写该方法

@end

相关文章

  • 简说设计模式之策略模式

    前言:对于设计模式基础概念可以去看[简说设计模式之设计模式概述] 一、什么是策略模式 策略(Strategy)模式...

  • 策略模式

    本文参考自: 《JAVA设计模式》之策略模式(Strategy) 1. 作用 策略模式属于对象的行为模式。其用意是...

  • 设计模式-策略模式

    设计模式-策略模式 定义 策略模式(Strategy Pattern)也叫政策模式(Policy Pattern)...

  • 设计模式[13]-策略模式-Strategy Pattern

    1.策略模式简介 策略模式(Strategy Patter)是行为型(Behavioral)设计模式,策略模式封装...

  • Java设计模式之策略模式

    Java设计模式之策略模式 简介 在策略模式(Strategy Pattern)中,一个类的行为或其算法可以在运行...

  • iOS设计模式之Strategy(策略模式)

    策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换。策略模式让算法独立于使用它的客户而独...

  • iOS设计模式之策略(strategy)模式

    1、定义 定义一系列算法,把它们一个个封装起来,并且使它们可相互替换。本模式使得算法可独立于使用它的客户而变化。 ...

  • iOS 设计模式-策略模式

    1.策略模式简介   策略模式(Strategy Pattern),是行为型模式之一(设计模式分类:https:/...

  • 设计模式之策略模式(Strategy)

    1. 什么是策略模式? 策略模式 定义了算法族,分别封装起来, 让它们之间可以互相替换,此模式让算法的变化独立于使...

  • 设计模式之策略模式(Strategy)

    概述 写代码时总会出很多的if…else,或者case。如果在一个条件语句中又包含了多个条件语句就会使得代码变得臃...

网友评论

      本文标题:iOS设计模式之策略(strategy)模式

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