Skip to content

Commit cbb6d41

Browse files
committed
commit
1 parent 7e8fbbe commit cbb6d41

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2330
-0
lines changed

DesignPattern/DesignPattern.xcodeproj/project.pbxproj

Lines changed: 552 additions & 0 deletions
Large diffs are not rendered by default.

DesignPattern/DesignPattern.xcodeproj/project.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
AppDelegate.h
3+
4+
Created by coder4869 on 7/27/16.
5+
Copyright © 2016 coder4869. All rights reserved.
6+
*/
7+
8+
#import <UIKit/UIKit.h>
9+
10+
@interface AppDelegate : UIResponder <UIApplicationDelegate>
11+
12+
@property (strong, nonatomic) UIWindow *window;
13+
14+
@end
15+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
AppDelegate.m
3+
4+
Created by coder4869 on 7/27/16.
5+
Copyright © 2016 coder4869. All rights reserved.
6+
*/
7+
8+
#import "AppDelegate.h"
9+
#import "ViewController.h"
10+
11+
@implementation AppDelegate
12+
13+
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
14+
// Override point for customization after application launch.
15+
16+
ViewController *vc = [[ViewController alloc] init];
17+
[vc.view setBackgroundColor:[UIColor whiteColor]];
18+
19+
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
20+
21+
UITabBarController *tabC = [[UITabBarController alloc] init];
22+
tabC.viewControllers = @[nav, nav, nav];
23+
24+
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
25+
[self.window setRootViewController:tabC];
26+
[self.window makeKeyAndVisible];
27+
28+
return YES;
29+
}
30+
31+
@end
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "iphone",
5+
"size" : "29x29",
6+
"scale" : "2x"
7+
},
8+
{
9+
"idiom" : "iphone",
10+
"size" : "29x29",
11+
"scale" : "3x"
12+
},
13+
{
14+
"idiom" : "iphone",
15+
"size" : "40x40",
16+
"scale" : "2x"
17+
},
18+
{
19+
"idiom" : "iphone",
20+
"size" : "40x40",
21+
"scale" : "3x"
22+
},
23+
{
24+
"idiom" : "iphone",
25+
"size" : "60x60",
26+
"scale" : "2x"
27+
},
28+
{
29+
"idiom" : "iphone",
30+
"size" : "60x60",
31+
"scale" : "3x"
32+
},
33+
{
34+
"idiom" : "ipad",
35+
"size" : "29x29",
36+
"scale" : "1x"
37+
},
38+
{
39+
"idiom" : "ipad",
40+
"size" : "29x29",
41+
"scale" : "2x"
42+
},
43+
{
44+
"idiom" : "ipad",
45+
"size" : "40x40",
46+
"scale" : "1x"
47+
},
48+
{
49+
"idiom" : "ipad",
50+
"size" : "40x40",
51+
"scale" : "2x"
52+
},
53+
{
54+
"idiom" : "ipad",
55+
"size" : "76x76",
56+
"scale" : "1x"
57+
},
58+
{
59+
"idiom" : "ipad",
60+
"size" : "76x76",
61+
"scale" : "2x"
62+
}
63+
],
64+
"info" : {
65+
"version" : 1,
66+
"author" : "xcode"
67+
}
68+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10116" systemVersion="15G31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
3+
<dependencies>
4+
<deployment identifier="iOS"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
6+
</dependencies>
7+
<scenes>
8+
<!--View Controller-->
9+
<scene sceneID="EHf-IW-A2E">
10+
<objects>
11+
<viewController id="01J-lp-oVM" sceneMemberID="viewController">
12+
<layoutGuides>
13+
<viewControllerLayoutGuide type="top" id="Llm-lL-Icb"/>
14+
<viewControllerLayoutGuide type="bottom" id="xb3-aO-Qok"/>
15+
</layoutGuides>
16+
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
17+
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
18+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
19+
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
20+
</view>
21+
</viewController>
22+
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
23+
</objects>
24+
<point key="canvasLocation" x="53" y="375"/>
25+
</scene>
26+
</scenes>
27+
</document>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>en</string>
7+
<key>CFBundleExecutable</key>
8+
<string>$(EXECUTABLE_NAME)</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
11+
<key>CFBundleInfoDictionaryVersion</key>
12+
<string>6.0</string>
13+
<key>CFBundleName</key>
14+
<string>$(PRODUCT_NAME)</string>
15+
<key>CFBundlePackageType</key>
16+
<string>APPL</string>
17+
<key>CFBundleShortVersionString</key>
18+
<string>1.0</string>
19+
<key>CFBundleSignature</key>
20+
<string>????</string>
21+
<key>CFBundleVersion</key>
22+
<string>1</string>
23+
<key>LSRequiresIPhoneOS</key>
24+
<true/>
25+
<key>UILaunchStoryboardName</key>
26+
<string>LaunchScreen</string>
27+
<key>UIRequiredDeviceCapabilities</key>
28+
<array>
29+
<string>armv7</string>
30+
</array>
31+
<key>UISupportedInterfaceOrientations</key>
32+
<array>
33+
<string>UIInterfaceOrientationPortrait</string>
34+
<string>UIInterfaceOrientationLandscapeLeft</string>
35+
<string>UIInterfaceOrientationLandscapeRight</string>
36+
</array>
37+
<key>UISupportedInterfaceOrientations~ipad</key>
38+
<array>
39+
<string>UIInterfaceOrientationPortrait</string>
40+
<string>UIInterfaceOrientationPortraitUpsideDown</string>
41+
<string>UIInterfaceOrientationLandscapeLeft</string>
42+
<string>UIInterfaceOrientationLandscapeRight</string>
43+
</array>
44+
</dict>
45+
</plist>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
EmailValidator.h
3+
4+
Created by coder4869 on 7/27/16.
5+
Copyright © 2016 coder4869. All rights reserved.
6+
*/
7+
8+
#import <Foundation/Foundation.h>
9+
#import "ValidatorInterface.h"
10+
11+
@interface EmailValidator : NSObject <ValidatorInterface>
12+
13+
@end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
EmailValidator.m
3+
4+
Created by coder4869 on 7/27/16.
5+
Copyright © 2016 coder4869. All rights reserved.
6+
*/
7+
8+
#import "EmailValidator.h"
9+
10+
@implementation EmailValidator
11+
12+
-(BOOL) validateInput:(id)input error:(NSError**)error
13+
{
14+
NSLog(@"Email Validator Action!");
15+
return true;
16+
}
17+
18+
@end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
InputValidator.h
3+
4+
Created by coder4869 on 7/27/16.
5+
Copyright © 2016 coder4869. All rights reserved.
6+
*/
7+
8+
#import <Foundation/Foundation.h>
9+
#import "ValidatorInterface.h"
10+
11+
@interface InputValidator : NSObject
12+
{
13+
@public
14+
id< ValidatorInterface> validator;
15+
}
16+
-(void)setValidator:(id<ValidatorInterface >)myValidator;
17+
-(BOOL) validateInput:(id)input error:(NSError**)error;
18+
19+
@end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
InputValidator.m
3+
4+
Created by coder4869 on 7/27/16.
5+
Copyright © 2016 coder4869. All rights reserved.
6+
*/
7+
8+
#import "InputValidator.h"
9+
10+
@implementation InputValidator
11+
12+
-(void)setValidator:(id<ValidatorInterface >)myValidator {
13+
validator = myValidator;
14+
}
15+
-(BOOL) validateInput:(id)input error:(NSError**)error {
16+
NSLog(@"Input Validator callled");
17+
[validator validateInput:input error:nil];
18+
return true;
19+
}
20+
21+
@end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
PhoneValidator.h
3+
4+
Created by coder4869 on 7/27/16.
5+
Copyright © 2016 coder4869. All rights reserved.
6+
*/
7+
8+
#import <Foundation/Foundation.h>
9+
#import "ValidatorInterface.h"
10+
11+
@interface PhoneValidator : NSObject <ValidatorInterface>
12+
13+
@end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
PhoneValidator.m
3+
4+
Created by coder4869 on 7/27/16.
5+
Copyright © 2016 coder4869. All rights reserved.
6+
*/
7+
8+
#import "PhoneValidator.h"
9+
10+
11+
@implementation PhoneValidator
12+
13+
-(BOOL) validateInput:(id)input error:(NSError**)error
14+
{
15+
NSLog(@"Phone Validator Action!");
16+
return true;
17+
}
18+
19+
@end
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
5. 策略模式
3+
策略模式:定义一系列的算法,把每一个算法封装起来,并且使它们可相互替换。本模式使得算法可独立于使用它的客户而变化。也称为政策模式 (Policy)。
4+
策略模式把对象本身和运算规则区分开来,其功能非常强大,因为这个设计模式本身的核心思想就是面向对象编程的多形性的思想。
5+
敏捷原则:接口隔离原则;多用组合,少用继承;针对接口编程,而非实现。
6+
注意事项:
7+
[1]. 剥离类中易于变化的行为,通过组合的方式嵌入抽象基类
8+
[2]. 变化的行为抽象基类为,所有可变变化的父类
9+
[3]. 用户类的最终实例,通过注入行为实例的方式,设定易变行为,防止了继承行为方式,导致无关行为污染子类。完成了策略封装和可替换性。
10+
11+
5.1 适用场景
12+
[1]. 许多相关的类仅仅是行为有异。“策略”提供了一种用多个行为中的一个行为来配置一个类的方法。即一个系统需要动态地在几种算法中选择一种。
13+
[2]. 需要使用一个算法的不同变体。例如,定义一些反映不同的空间/时间权衡的算法。当这些变体实现为一个算法的类层次时,可以使用策略模式。
14+
[3]. 算法使用客户不应该知道的数据。可使用策略模式以避免暴露复杂的、与算法相关的数据结构。
15+
[4]. 一个类定义了多种行为,并且这些行为在这个类的操作中以多个条件语句的形式出现。将相关的条件分支移入它们各自的Strategy类中以代替这些条件语句。
16+
17+
5.2 优缺点分析
18+
5.2.1 优点
19+
[1]. 相关算法系列
20+
Strategy类层次为Context定义了一系列的可供重用的算法或行为。继承有助于析取出这些算法中的公共功能。
21+
[2]. 提供了可以替换继承关系的办法
22+
继承提供了另一种支持多种算法或行为的方法。可以直接生成一个Context类的子类,从而给它以不同的行为。但这会将行为硬行编制到Context中,而将算法的实现与Context的实现混合起来,从而使Context难以理解、难以维护和难以扩展,而且还不能动态地改变算法。最后得到一堆相关的类,它们之间的唯一差别是它们所使用的算法或行为。将算法封装在独立的Strategy类中使得你可以独立于其Context改变它,使它易于切换、易于理解、易于扩展。
23+
[3]. 消除了一些if else条件语句
24+
Strategy模式提供了用条件语句选择所需的行为以外的另一种选择。当不同的行为堆砌在一个类中时,很难避免使用条件语句来选择合适的行为。将行为封装在一个个独立的Strategy类中消除了这些条件语句。含有许多条件语句的代码通常意味着需要使用Strategy模式。
25+
[4]. 实现的选择
26+
Strategy模式可以提供相同行为的不同实现。客户可以根据不同时间/空间权衡取舍要求从不同策略中进行选择。
27+
5.2.2 缺点
28+
[1]. 客户端必须知道所有的策略类,并自行决定使用哪一个策略类
29+
客户要选择一个合适的Strategy就必须知道各Strategy的不同。此时可能不得不向客户暴露具体的实现问题。因此仅当这些不同行为变体与客户相关的行为时,才需要使用Strategy模式。
30+
[2]. Strategy和Context之间的通信开销
31+
无论各Strategy实现的算法是简单还是复杂,它们都共享Strategy定义的接口。有时,某Strategy不会用到接口传入的全部信息,这使得有时Context会创建和初始化一些永远不会用到的参数。如果存在这样问题,那么将需要在Strategy和Context之间更进行紧密的耦合。
32+
[3]. 策略模式将造成产生很多策略类
33+
可以通过使用享元模式在一定程度上减少对象的数量。增加了对象的数目Strategy增加了一个应用中的对象的数目。有时你可以将 Strategy实现为可供各Context共享的无状态的对象来减少这一开销。任何其余的状态都由 Context维护。Context在每一次对Strategy对象的请求中都将这个状态传递过去。共享的 Strategy不应在各次调用之间维护状态。
34+
35+
5.3 案例分析
36+
5.3.1 案例
37+
在验证用户输入的表单的时候,加入包括电话输入框的验证和邮件输入框的验证,这两部分的验证算法是不同的,如果把这个算法看成一个函数,他几乎有相同的输入参数和返回参数。
38+
5.3.2 策略
39+
[1]. 把公共函数抽象为基类(InputValidator)的一个方法(bool validateInput(input,error))
40+
[2]. 抽象出两个具体的策略类:电话验证类(PhoneValidator)和邮件验证类(EmailValidator)。策略类在各自的实现里面去复写父类的验证方法。
41+
42+
5.4 Reference
43+
iOS设计模式 - (4)策略模式:http://blog.csdn.net/hitwhylz/article/details/40583525
44+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
ValidatorInterface.h
3+
4+
Created by coder4869 on 7/27/16.
5+
Copyright © 2016 coder4869. All rights reserved.
6+
*/
7+
8+
#import <Foundation/Foundation.h>
9+
10+
@protocol ValidatorInterface <NSObject>
11+
-(BOOL) validateInput:(NSString*)input error:(NSError**)error;
12+
@end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
Test4DesignPattern.h
3+
4+
Created by coder4869 on 7/27/16.
5+
Copyright © 2016 coder4869. All rights reserved.
6+
*/
7+
8+
#import <Foundation/Foundation.h>
9+
10+
@interface Test4DesignPattern : NSObject
11+
12+
//策略模式调用案例
13+
- (void) test4StrategyDemo;
14+
15+
@end

0 commit comments

Comments
 (0)