iOS Objective-c Learning strategy mode

Source: Internet
Author: User

For the policy model, I personally understand that the policy pattern is a encapsulation of the various rules, not just the encapsulation and invocation of the algorithm. It is somewhat similar to a simple factory in Factory mode, but more coupled than a simple factory, because the policy pattern invokes all the rules in the same way, reducing the coupling between the rule class and the Rule class.

Next I use the strategy mode to edit the code to calculate the number of times the landlord has doubled according to the Republicans success failure.

Create a fractional class

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

/*

* Victory Score

*/

-(double) getscore{

return self. Score;

}

@end

LoseScore.h

#import "Score.h"

@interface Losescore:score

@end

Losescore.m

#import "LoseScore.h"

@implementation Losescore

/*

* Failure Score

*/

-(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

/*

* Double Score

*/

-(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:@ "Success"]) {

Self.score=[[winscore Alloc]init];

}

else if ([param isequaltostring:@ "Double"]) {

Self.score=[[bombscore alloc]initwithbombcount:1];//Bomb number temporarily set to 1

}

else{

Self.score=[[losescore Alloc]init];

}

}

@end

Execute code:

#import "ViewController.h"

#import "ScoreResult.h"

@interface Viewcontroller ()

@end

@implementation Viewcontroller

-(void) Viewdidload {

[Super Viewdidload];

Scoreresult *sr1=[[scoreresult alloc]initwithsituation:@ "Double"];

Double score1= [Sr1.score Getscore];

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

Scoreresult *sr2=[[scoreresult alloc]initwithsituation:@ "Success"];

Double score2= [Sr2.score Getscore];

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

Scoreresult *sr3=[[scoreresult alloc]initwithsituation:@ "failure"];

Double score3= [Sr3.score Getscore];

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

} 

-(void) didreceivememorywarning {

[Super didreceivememorywarning];

}

@end

Operation Result:

score1=20.000000

score2=10.000000

score3=-10.000000

A summary of the policy pattern is that the policy pattern is to encapsulate some rules in the context class of the policy, and then you can derive the desired result by invoking the methods in the policy context class. The Scoreresult class in the code above is the context class for the policy.

iOS Objective-c Learning strategy mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.