iOS設計模式之備忘錄模式

2021-09-23 21:35:35 字數 2480 閱讀 2760

-(id)initwithcoder:(nscoder *)coder;

-(void)encodewithcoder:(nscoder *)coder;

//

// caretaker.h

// memodemo

//// created by zhanggui on 15/8/8.

//#import

@class

memo; @inte***ce

caretaker : nsobject

@property(nonatomic,strong) memo *memo;

@end

caretaker.m

//

// caretaker.m

// memodemo

//// created by zhanggui on 15/8/8.

//#import "caretaker.h"

@implementation

caretaker

@end

上面這個是看管人。

memo.h

//

// memo.h

// memodemo

//// created by zhanggui on 15/8/8.

//#import

@inte***ce

memo : nsobject

-(memo *)initwithname:(nsstring *)name andwithaddress:(nsstring *)address;

-(nsstring *)getname;

-(nsstring *)getaddress;

@end

memo.m

//

// memo.m

// memodemo

//// created by zhanggui on 15/8/8.

//#import "memo.h"

@implementation

memo

-(memo *)initwithname:(nsstring *)name andwithaddress:(nsstring *)address

-(nsstring *)getaddress

-(nsstring *)getname

@end

上面這個是備忘錄。

student.h

//

// student.h

// memodemo

//// created by zhanggui on 15/8/8.

//#import

@class

memo; @inte***ce

student : nsobject

@property(nonatomic,strong)nsstring *myname;

@property(nonatomic,strong)nsstring *address;

-(memo *)creatememo;

-(void)setmemo:(memo *)memo;

-(void)display;

@end

student.m

//

// student.m

// memodemo

//// created by zhanggui on 15/8/8.

//#import "student.h"

#import "memo.h"

@implementation

student

-(memo *)creatememo

-(void)setmemo:(memo *)memo

-(void)display

@end

上面這個是student。然後看一下具體的呼叫:

//

// viewcontroller.m

// memodemo

//// created by zhanggui on 15/8/8.

//#import "viewcontroller.h"

#import "memo.h"

#import "caretaker.h"

#import "student.h"

@inte***ce

viewcontroller () @end

@implementation

viewcontroller

- (void)viewdidload

@end

也就是把zhangsan原來的姓名和位址先儲存到備忘錄中,可是過了一段時間zhangsan不行用自己的新名字和位址了,所有就通過備忘錄來還原自己的姓名和位址。

iOS設計模式之備忘錄模式

id initwithcoder nscoder coder void encodewithcoder nscoder coder caretaker.h memodemo created by zhanggui on 15 8 8.import 26lt foundation foundation...

iOS設計模式之備忘錄模式

id initwithcoder nscoder coder void encodewithcoder nscoder coder caretaker.h memodemo created by zhanggui on 15 8 8.import 26lt foundation foundation...

iOS設計模式之備忘錄模式

id initwithcoder nscoder coder void encodewithcoder nscoder coder caretaker.h memodemo created by zhanggui on 15 8 8.import 26lt foundation foundation...