IOS之Objective-C學習 策略模式

來源:互聯網
上載者:User

標籤:

對於策略模式,我個人理解策略模式就是對各種規則的一種封裝的方法,而不僅僅是對演算法的封裝與調用而已。與原廠模式中簡單工廠有點類似,但是比簡單工廠更有耦合度,因為策略模式以相同的方法調用所有的規則,減少了規則類和規則類之間的耦合度。

接下來我用策略模式編輯代碼來計算鬥地主中地主根據坐莊成功失敗翻倍情況的得分情況。

建立一個分數類

Score.h

#import <Foundation/Foundation.h>

#define BASIC_POINT 10

@interface Score : NSObject

@property(nonatomic,assign)double Score;

-(double)getScore;

@end

 

Score.m

 

#import "Score.h"

@implementation Score

-(instancetype)init{

    self=[super  init];

    if (self) {

        self.Score=BASIC_POINT;

    }

    return self;

}

-(double)getScore{

    return NO;

}

@end

 

winScore.h

 

#import "Score.h"

@interface winScore : Score

@end

 

winScore.m

 

#import "winScore.h"

@implementation winScore

/*

 *勝利得分

 */

-(double)getScore{

    return self.Score;

}

@end

 

loseScore.h

 

#import "Score.h"

@interface loseScore : Score

@end

 

loseScore.m

 

#import "loseScore.h"

 

@implementation loseScore

/*

 *失敗得分

 */

-(double)getScore{

    return self.Score*-1;

}

@end

 

BombScore.h

 

#import "Score.h"

@interface BombScore : Score

@property(nonatomic,assign)int bombCount;

-(instancetype)initWithbombCount:(int)counts;

@end

 

BombScore.m

 

#import "BombScore.h"

@implementation BombScore

/*

 *翻倍得分

 */

-(instancetype)initWithbombCount:(int)bombCount{

    self=[super init];

    if (self) {

        _bombCount=bombCount;

    }

    return self;

}

-(double)getScore{

    double d=self.bombCount+1;

    return d*self.Score;

}

@end

 

ScoreResult.h

 

#import <Foundation/Foundation.h>

#import "Score.h"

#import "loseScore.h"

#import "winScore.h"

#import "BombScore.h"

@interface ScoreResult : NSObject

@property(nonatomic,strong) Score* score;

-(instancetype)initWithSituation:(NSString *)str; 

@end

 

ScoreResult.m

 

#import "ScoreResult.h"

@implementation ScoreResult

-(instancetype)initWithSituation:(NSString *)str{

    self=[super init];

    if (self) {

        [self settleScore:str];

    }

    return self;

}

-(void)settleScore:(NSString *)param{

    if ([param isEqualToString:@"成功"]) {

        self.score=[[winScore alloc]init];

    }

    else if([param isEqualToString:@"翻倍"]){

        self.score=[[BombScore alloc]initWithbombCount:1];//炸彈數暫時設為1

    }

    else{

        self.score=[[loseScore alloc]init];

    }

}

@end

執行代碼:

#import "ViewController.h"

#import "ScoreResult.h"

@interface ViewController ()

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    ScoreResult *sr1=[[ScoreResult alloc]initWithSituation:@"翻倍"];

    double score1= [sr1.score getScore];

    NSLog(@"score1=%f",score1);

    ScoreResult *sr2=[[ScoreResult alloc]initWithSituation:@"成功"];

    double score2= [sr2.score getScore];

    NSLog(@"score2=%f",score2);

    ScoreResult *sr3=[[ScoreResult alloc]initWithSituation:@"失敗"];

    double score3= [sr3.score getScore];

    NSLog(@"score3=%f",score3);

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

}

 

@end

 運行結果: 

score1=20.000000

score2=10.000000 

score3=-10.000000

 

對策略模式做一個總結,策略模式是將一些規律封裝策略的context類中,然後通過調用策略context類中的方法就可以得出想要的結果。上述代碼中ScoreResult類就是策略的Context類。

 

IOS之Objective-C學習 策略模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.