Cocoa Touch框架在归档、属性列表序列化和核心数据采用了备忘录模式。
-(id)initWithCoder:(NSCoder *)coder;
-(void)encodeWithCoder:(NSCoder *)coder;
该例子就是通过备忘录模式来保存Student的实例stu1更改名字和地址之前的原名字和地址。然后stu1恢复原来的名字和地址。具体代码如下:Caretaker.h
// // Caretaker.h // memoDemo // // Created by zhanggui on 15/8/8. // Copyright (c) 2015年 zhanggui. All rights reserved. // #import <Foundation/Foundation.h> @class Memo; @interface Caretaker : NSObject @property(nonatomic,strong) Memo *memo; @end
Caretaker.m
// // Caretaker.m // memoDemo // // Created by zhanggui on 15/8/8. // Copyright (c) 2015年 zhanggui. All rights reserved. // #import "Caretaker.h" @implementation Caretaker @end
上面这个是看管人。Memo.h
// // Memo.h // memoDemo // // Created by zhanggui on 15/8/8. // Copyright (c) 2015年 zhanggui. All rights reserved. // #import <Foundation/Foundation.h> @interface Memo : NSObject { NSString *tempname; NSString *tempaddress; } -(Memo *)initWithName:(NSString *)name andWithAddress:(NSString *)address; -(NSString *)getName; -(NSString *)getAddress; @end
Memo.m
// // Memo.m // memoDemo // // Created by zhanggui on 15/8/8. // Copyright (c) 2015年 zhanggui. All rights reserved. // #import "Memo.h" @implementation Memo -(Memo *)initWithName:(NSString *)name andWithAddress:(NSString *)address { tempname = name; tempaddress = address; return self; } -(NSString *)getAddress { return tempaddress; } -(NSString *)getName { return tempname; } @end
上面这个是备忘录。Student.h
// // Student.h // memoDemo // // Created by zhanggui on 15/8/8. // Copyright (c) 2015年 zhanggui. All rights reserved. // #import <Foundation/Foundation.h> @class Memo; @interface 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. // Copyright (c) 2015年 zhanggui. All rights reserved. // #import "Student.h" #import "Memo.h" @implementation Student -(Memo *)createMemo { return [[Memo alloc] initWithName:_myName andWithAddress:_address]; } -(void)setMemo:(Memo *)memo { _myName = [memo getName]; _address = [memo getAddress]; } -(void)display { NSLog(@"State is name:%@;address:%@",_myName,_address); } @end
上面这个是Student。然后看一下具体的调用:
// // ViewController.m // memoDemo // // Created by zhanggui on 15/8/8. // Copyright (c) 2015年 zhanggui. All rights reserved. // #import "ViewController.h" #import "Memo.h" #import "Caretaker.h" #import "Student.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; Student *stu1 = [[Student alloc] init]; [stu1 setMyName:@"zhangsan"]; [stu1 setAddress:@"he nan xin xiang"]; [stu1 display]; //use caretaker to save old name and address Caretaker *careTaker = [[Caretaker alloc] init]; [careTaker setMemo:[stu1 createMemo]]; //updated name and address [stu1 setMyName:@"zhangxiaoqiang"]; [stu1 setAddress:@"bei jing san huan"]; [stu1 display]; //back to old name and address [stu1 setMemo:[careTaker memo]]; [stu1 display]; } @end
也就是把zhangsan原来的姓名和地址先保存到备忘录中,可是过了一段时间zhangsan不行用自己的新名字和地址了,所有就通过备忘录来还原自己的姓名和地址。