• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Strategy Pattern for Objective-C
 

Strategy Pattern for Objective-C

on

  • 1,378 views

The material of Michael's Lesson. ...

The material of Michael's Lesson.
More classes please go to http://www.zencher.com/
FB Page : https://www.facebook.com/pages/Developers-note/226724001803
Blogger : http://iosdevelopersnote.blogspot.tw/

Statistics

Views

Total Views
1,378
Views on SlideShare
1,378
Embed Views
0

Actions

Likes
2
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Strategy Pattern for Objective-C Strategy Pattern for Objective-C Presentation Transcript

    • 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⽇日星期⽇日