iOS設計模式之備忘錄模式

2021-07-04 10:12:47 字數 2679 閱讀 3570

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

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

//

// caretaker.h

// memodemo

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

//#import %26lt;foundation/foundation.h%26gt;

@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 %26quot;caretaker.h%26quot;

@implementation caretaker

@end

上面這個是看管人。

memo.h

//

// memo.h

// memodemo

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

//#import %26lt;foundation/foundation.h%26gt;

@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 %26quot;memo.h%26quot;

@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 %26lt;foundation/foundation.h%26gt;

@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 %26quot;student.h%26quot;

#import %26quot;memo.h%26quot;

@implementation student

-(memo *)creatememo

-(void)setmemo:(memo *)memo

-(void)display

@end

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

//

// viewcontroller.m

// memodemo

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

//#import %26quot;viewcontroller.h%26quot;

#import %26quot;memo.h%26quot;

#import %26quot;caretaker.h%26quot;

#import %26quot;student.h%26quot;

@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 class memo inte ce caretak...