SlideShare una empresa de Scribd logo
1 de 32
Strategy Pattern
Michael Pan
13年9月1⽇日星期⽇日
System
• Game
• 有多個不同的職業
• 每個職業可以有不同的武器
13年9月1⽇日星期⽇日
Classes
戰⼠士
魔法師
⼑刀
劍
法杖
⼸弓箭⼿手
⼸弓箭
13年9月1⽇日星期⽇日
Intuitive thinking
Warrior
sword:Sword*
How about
Staff
Bow
13年9月1⽇日星期⽇日
Abstraction - Generalization
Warrior
weapon:Weapon*
attack()
behavior()
<<interface>>
Weapon
Staff SwordBow
13年9月1⽇日星期⽇日
Application
Warrior * michael = [Warrior new];
michael.weapon = [Sword new];
[michael attack];
michael.weapon = [Staff new];
[michael attack];
13年9月1⽇日星期⽇日
Codes-Warrior
@interface Warrior : NSObject
@property Weapon * weapon;
-(void) attack;
@end
@implementation Warrior
-(void) attack{
[self.weapon behavior];
}
@end
13年9月1⽇日星期⽇日
Codes-Weapon
@interface Weapon : NSObject
-(void) behavior;
@end
@implementation Weapon
@end
@interface Sword : Weapon
@end
@implementation Sword
-(void) behavior{
NSLog(@"Slash...");
}
@end
13年9月1⽇日星期⽇日
Codes-Weapon
@interface Weapon : NSObject
-(void) behavior;
@end
@implementation Weapon
@end
@interface Staff : Weapon
@end
@implementation Staff
-(void) behavior{
NSLog(@"Fire Ball");
}
@end
13年9月1⽇日星期⽇日
Avoid warning..
13年9月1⽇日星期⽇日
Purpose
• Let the subclass must implement this method
• Avoid warning
13年9月1⽇日星期⽇日
Solution
13年9月1⽇日星期⽇日
Test
Weapon * weapon = [Weapon new];
[weapon behavior];
13年9月1⽇日星期⽇日
Another Example - Play Card
http://bit.ly/14zjJfC
13年9月1⽇日星期⽇日
ExchageEffect
• http://bit.ly/1aQW6Rm
13年9月1⽇日星期⽇日
CardView
@interface CardView : UIImageView
@property UIImage * frontImage;
@property UIImage * backImage;
-(void) changeImage;
-(void) showFront;
-(void) showBack;
-(void) lock;
-(void) unlock;
@end
13年9月1⽇日星期⽇日
Add effect in the class
- (void)flipOver:(NSString *) direction {
CATransition* transition = [CATransition animation];
transition.timingFunction = [CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseIn];
transition.duration = 0.5f;
transition.type = @"flip";
transition.subtype = direction;
[self.layer removeAllAnimations];
[self.layer addAnimation:transition forKey:kCATransition];
}
-(void) showFront{
if (self.isLocked) {
return;
}
[self flipOver:@"fromLeft"];
self.image = self.frontImage;
self.isFront = YES;
}
Core Animation
13年9月1⽇日星期⽇日
Extract what into a class ?
- (void)flipOver:(NSString *) direction {
CATransition* transition = [CATransition animation];
transition.timingFunction = [CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseIn];
transition.duration = 0.5f;
transition.type = @"flip";
transition.subtype = direction;
[self.layer removeAllAnimations];
[self.layer addAnimation:transition forKey:kCATransition];
}
should be related to the view object
13年9月1⽇日星期⽇日
FlipEffect - Class
@interface FlipEffect : NSObject
@property (weak) UIView * view;
-(void) flipToRight;
-(void) flipToLeft;
@end
@implementation FlipEffect
- (void)flipOver:(NSString *) direction {
// ignore
[self.view.layer removeAllAnimations];
[self.view.layer addAnimation:transition forKey:kCATransition];
}
-(void) flipToRight{
[self flipOver:@"fromLeft"];
}
-(void) flipToLeft{
[self flipOver:@"fromRight"];
}
@end
13年9月1⽇日星期⽇日
CardView - Modification
@class FlipEffect;
@interface CardView : UIImageView
@property UIImage * frontImage;
@property UIImage * backImage;
@property FlipEffect * effect;
-(void) changeImage;
-(void) showFront;
-(void) showBack;
-(void) lock;
-(void) unlock;
@end
13年9月1⽇日星期⽇日
CardView.m
-(void) showFront{
if (self.isLocked) {
return;
}
[self.effect flipToRight];
// [self flipOver:@"fromLeft"];
self.image = self.frontImage;
self.isFront = YES;
}
-(void) showBack{
if (self.isLocked) {
return;
}
[self.effect flipToLeft];
self.image = self.backImage;
self.isFront = NO;
}
13年9月1⽇日星期⽇日
Create objects
[self.cards enumerateObjectsUsingBlock:^(id obj,
NSUInteger idx, BOOL *stop) {
CardView * cardView = obj;
FlipEffect * effect =[FlipEffect new];
cardView.effect = effect;
effect.view = cardView;
}];
13年9月1⽇日星期⽇日
Demo
• PlayCards_strategy_middle
13年9月1⽇日星期⽇日
Class diagram
CardView
FlipEffect
OtherEffect
???
13年9月1⽇日星期⽇日
Abstract
FlipEffect
view
flipToRight
flipToLeft
ExchangeEffect
view
action
reverseAction
13年9月1⽇日星期⽇日
Class Diagram
action()
reverseAction()
view
<<interface>>
ExchangeEffect
FlipEffect
13年9月1⽇日星期⽇日
Create ExchangeEffect - Interface
@interface ExchangeEffect : NSObject
@property (weak) UIView * view;
-(id) initWithView:(UIView *) targetView;
-(void) action;
-(void) reverseAction;
@end
@implementation ExchangeEffect
-(void) action{
[self doesNotRecognizeSelector:_cmd];
}
-(void) reverseAction{
[self doesNotRecognizeSelector:_cmd];
}
@end
13年9月1⽇日星期⽇日
FlipEffect
@interface FlipEffect : ExchangeEffect
@end
@implementation FlipEffect
-(void) action{
[self flipToRight];
}
-(void) reverseAction{
[self flipToLeft];
}
- (void)flipOver:(NSString *) direction {
//....
}
-(void) flipToRight{
[self flipOver:@"fromLeft"];
}
-(void) flipToLeft{
[self flipOver:@"fromRight"];
}
@end
13年9月1⽇日星期⽇日
CardView.h
@class ExchangeEffect;
@interface CardView : UIImageView
@property UIImage * frontImage;
@property UIImage * backImage;
@property ExchangeEffect * effect;
-(void) changeImage;
-(void) showFront;
-(void) showBack;
-(void) lock;
-(void) unlock;
@end
13年9月1⽇日星期⽇日
CardView.m
-(void) showFront{
if (self.isLocked) {
return;
}
[self.effect action];
self.image = self.frontImage;
self.isFront = YES;
}
-(void) showBack{
if (self.isLocked) {
return;
}
[self.effect reverseAction];
self.image = self.backImage;
self.isFront = NO;
}
13年9月1⽇日星期⽇日
Use object
[self.cards enumerateObjectsUsingBlock:^(id obj,
NSUInteger idx, BOOL *stop) {
CardView * cardView = obj;
ExchangeEffect * effect = nil;
if (idx %2 == 0) {
effect =[FlipEffect new];
cardView.effect = effect;
effect.view = cardView;
}else{
effect = [WaveEffect new];
cardView.effect = effect;
effect.view = cardView;
}
}];
13年9月1⽇日星期⽇日
Question
13年9月1⽇日星期⽇日

Más contenido relacionado

Más de Michael Pan

Google maps SDK for iOS 1.4
Google maps SDK for iOS 1.4Google maps SDK for iOS 1.4
Google maps SDK for iOS 1.4
Michael Pan
 
比價撿便宜 Steven
比價撿便宜 Steven比價撿便宜 Steven
比價撿便宜 Steven
Michael Pan
 
Appsgaga - iOS Game Developer
Appsgaga - iOS Game DeveloperAppsgaga - iOS Game Developer
Appsgaga - iOS Game Developer
Michael Pan
 
GZFox Inc. Jacky
GZFox Inc. JackyGZFox Inc. Jacky
GZFox Inc. Jacky
Michael Pan
 
創投公司 hoku_20121017
創投公司 hoku_20121017創投公司 hoku_20121017
創投公司 hoku_20121017
Michael Pan
 

Más de Michael Pan (17)

Activity
ActivityActivity
Activity
 
Shootting Game
Shootting GameShootting Game
Shootting Game
 
Introduction to Android Studio
Introduction to Android StudioIntroduction to Android Studio
Introduction to Android Studio
 
Eclipse and Genymotion
Eclipse and GenymotionEclipse and Genymotion
Eclipse and Genymotion
 
Core data lightweight_migration
Core data lightweight_migrationCore data lightweight_migration
Core data lightweight_migration
 
Google maps SDK for iOS 1.4
Google maps SDK for iOS 1.4Google maps SDK for iOS 1.4
Google maps SDK for iOS 1.4
 
Homework2 play cards
Homework2 play cardsHomework2 play cards
Homework2 play cards
 
Prototype by Xcode
Prototype by XcodePrototype by Xcode
Prototype by Xcode
 
Dropbox sync
Dropbox syncDropbox sync
Dropbox sync
 
Nimbus
NimbusNimbus
Nimbus
 
Superstar dj pdf
Superstar dj pdfSuperstar dj pdf
Superstar dj pdf
 
ADB - Arthur
ADB - ArthurADB - Arthur
ADB - Arthur
 
比價撿便宜 Steven
比價撿便宜 Steven比價撿便宜 Steven
比價撿便宜 Steven
 
Appsgaga - iOS Game Developer
Appsgaga - iOS Game DeveloperAppsgaga - iOS Game Developer
Appsgaga - iOS Game Developer
 
GZFox Inc. Jacky
GZFox Inc. JackyGZFox Inc. Jacky
GZFox Inc. Jacky
 
創投公司 hoku_20121017
創投公司 hoku_20121017創投公司 hoku_20121017
創投公司 hoku_20121017
 
Opening iOS App 開發者交流會
Opening iOS App 開發者交流會Opening iOS App 開發者交流會
Opening iOS App 開發者交流會
 

Strategy Pattern for Objective-C