iOS设计模式 - 策略

效果

说明

1. 把解决相同问题的算法抽象成策略(相同问题指的是输入参数相同,但根据算法不同输出参数会有差异)

2. 策略被封装在对象之中(是对象内容的一部分),策略改变的是对象的内容.如果从外部扩展了对象的行为,就不叫策略模式,而是装饰模式.

3. 策略模式可以简化复杂的判断逻辑(if - else)

4. 如果对面向对象基本原理以及设计模式基本原理不熟悉,本教程会变得难以理解.

源码

https://github.com/YouXianMing/iOS-Design-Patterns

//
// InputValidator.h
// StrategyPattern
//
// Created by YouXianMing on 15/7/26.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> #define EMPTY_INPUT @"输入为空" @interface InputValidator : NSObject /**
* 抽象策略
*
* @param input 当前输入textField
*
* @return 输入验证是否合法
*/
- (BOOL)validateInput:(UITextField *)input; /**
* 错误信息
*/
@property (nonatomic, strong) NSString *errorMessage; @end
//
// InputValidator.m
// StrategyPattern
//
// Created by YouXianMing on 15/7/26.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import "InputValidator.h" @implementation InputValidator - (BOOL)validateInput:(UITextField *)input { return NO;
} @end
//
// CustomField.h
// StrategyPattern
//
// Created by YouXianMing on 15/7/26.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h>
#import "InputValidator.h" @interface CustomField : UITextField /**
* 抽象策略
*/
@property (nonatomic, strong, readonly) InputValidator *inputValidator; /**
* 初始化textField
*
* @param frame
* @param inputValidator 验证策略
*
* @return 实例对象
*/
- (instancetype)initWithFrame:(CGRect)frame withInputValidator:(InputValidator *)inputValidator; /**
* 对应于具体策略的返回值
*
* @return 是否合格
*/
- (BOOL)validate; @end
//
// CustomField.m
// StrategyPattern
//
// Created by YouXianMing on 15/7/26.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import "CustomField.h" @interface CustomField () /**
* 抽象策略
*/
@property (nonatomic, strong) InputValidator *inputValidator; @end @implementation CustomField #pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame withInputValidator:(InputValidator *)inputValidator { self = [super initWithFrame:frame];
if (self) { [self setup]; // 持有inputValidator
self.inputValidator = inputValidator;
} return self;
} - (void)setup { UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(, , , self.frame.size.height)];
self.leftView = leftView;
self.leftViewMode = UITextFieldViewModeAlways; self.font = [UIFont fontWithName:@"Avenir-Book" size:.f]; self.layer.borderWidth = 0.5f;
} - (BOOL)validate { return [self.inputValidator validateInput:self];
} @end

分析

策略模式对比示意图(抽象类与具体实现的详细对比)

策略被封装在对象之中(是对象内容的一部分),策略改变的是对象的内容的

简化了 if - else 操作(按照以前的写法,有几个TextField判断,就得写几个if - else,还有,实现细节暴露出来,维护困难 -_-!!)

iOS设计模式 - 策略的更多相关文章

  1. iOS书摘之Objective-C编程之道 iOS设计模式解析

    来自<Objective-C编程之道iOS设计模式解析>一书的摘要总结 一.Prototype 原型模式 定义:使用原型实例指定创建对象的种类,并通过复制这个原型创建新的对象.(<设 ...

  2. iOS设计模式 - (1)概述

    近期可自由安排的时间比較多, iOS应用方面, 没什么好点子, 就先放下, 不写了.花点时间学学设计模式. 之后将会写一系列博文, 记录设计模式学习过程. 当然, 由于我自己是搞iOS的, 所以之后设 ...

  3. 15. 星际争霸之php设计模式--策略模式

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

  4. IOS设计模式之一(MVC模式,单例模式)

    iOS 设计模式-你可能已经听说过这个词,但是你真正理解它意味着什么吗?虽然大多数的开发者可能都会认为设计模式是非常重要的,然而关于设计模式这一主题的文章却不多,并且有时候我们开发者在写代码的时候也不 ...

  5. iOS 设计模式之工厂模式

    iOS 设计模式之工厂模式 分类: 设计模式2014-02-10 18:05 11020人阅读 评论(2) 收藏 举报 ios设计模式 工厂模式我的理解是:他就是为了创建对象的 创建对象的时候,我们一 ...

  6. iOS设计模式之生成器

    iOS设计模式之生成器 1.生成器模式的定义 (1): 将一个复杂的对象的构件与它的表示分离,使得相同的构建过程能够创建不同的表示 (2): 生成器模式除了客户之外还包括一个Director(指导者) ...

  7. IOS设计模式之三:MVC模式

    IOS设计模式之三:MVC模式   模型-视图-控制器 这个模式其实应该叫做MCV,用控制器把model与view隔开才对,也就是model与view互相不知道对方的存在,没有任何瓜葛,他们就像一个团 ...

  8. [.net 面向对象程序设计深入](24)实战设计模式——策略模式(行为型)

    [.net 面向对象程序设计深入](24)实战设计模式——策略模式(行为型) 1,策略模式定义 策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换.策略模式让算法独立于使用它 ...

  9. linkin大话设计模式--策略模式

    linkin大话设计模式--策略模式 Strategy [ˈstrætədʒi]  策略 策略模式用于封装系列的算法,这些算法通常被封装在一个称为Context的类中,客户端程序可以自由的选择任何一种 ...

随机推荐

  1. cookies session filter 自动登录

    webxml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=" ...

  2. encodeURI、encodeURIComponent、btoa及其应用场景

    escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z encodeURI不编码字符有82个:!,#,$,&,’,(,),*,+,,,-,.,/,:,;,=,?,@ ...

  3. python2和python3中列表推导式的变量泄露问题

    Python 2.x 中,在列表推导中 for 关键词之后的赋值操作可能会影响列表推导上下文中的同名变量.像下面这个 Python 2.7 控制台对话: Python 2.7.15 (default, ...

  4. scrapyd远程连接配置

    安装scrapyd: pip install scrapyd 默认scrapyd启动是通过scrapyd就可以直接启动,bind绑定的ip地址是127.0.0.1端口是:6800,这里为了其他主机可以 ...

  5. 2-6 js基础-ajax

    1.var oAjax=new XmlHttpRequest()//创建一个ajax对象,兼容非ie6 var oAjax=new ActiveXObject('Microsoft.XMLHTTP') ...

  6. springboot-22-自定义starter

    先说下springboot的运行原理 springboot最主要的配置 是 @SpringBootApplication 然后这里面 @EnableAutoCOnfiguration 最为重要, 继续 ...

  7. leetcode2:线性表

    /********************************************** Function:input two array and find the kth value the ...

  8. Caffe入门随笔

    Caffe入门随笔   分享一下自己入门机器学习的一些资料:(1)课程,最推荐Coursera上的Andrew NG的Machine Learning,最好注册课程,然后跟下来.其次是华盛顿大学的Ma ...

  9. OpenStack Object Storage(Swift)概述

    概述 OpenStack Object Storage(Swift)是OpenStack开源云计算项目的子项目之一,被称为对象存储,提供了强大的扩展性.冗余和持久性. Swift并不是文件系统或者实时 ...

  10. C# 之多线程(二)

    一.确定多线程的结束时间,thread的IsAlive属性 在多个线程运行的背景下,了解线程什么时候结束,什么时候停止是很有必要的. 案例:老和尚念经计时,2本经书,2个和尚念,一人一本,不能撕破,最 ...