美文网首页
iOS设计模式:备忘录

iOS设计模式:备忘录

作者: younger_times | 来源:发表于2017-03-03 10:36 被阅读40次

    视频教程-极客学院

    ps:感觉打的一手好广告啊,因为自己不太爱看视频,但这类又必须看才能明白。粘贴源代码是为了以后查阅方便,也注释了自己的理解。

    需要有特定的key值去取得所需要的值。好比超市的存货柜类似,并且能存储任何对象的数据
    自我认为实用与本地存储,对用户登录状态信息等进行必要存储就免得再次请求。

    项目中需要引入:FastCoding (-nfo-objc-arc)

    FastCoding-Github

    MementoCenterProtocol

    规定了协议,这个协议在Apple.h中体现

    @protocol MementoCenterProtocol <NSObject>
    @required
    -(id)currentState;
    -(void)recoverFromState:(id)state;
    

    MementCenter

    #import <Foundation/Foundation.h>
    #import "MementoCenterProtocol.h"
    @interface MementoCenter : NSObject
    
    + (void)saveMementoObject:(id <MementoCenterProtocol>)object withKey:(NSString *)key;
    + (id)mementoObjectWithKey:(NSString *)key;
    @end
    
    #import "MementoCenter.h"
    #import "FastCoder.h"
    
    @implementation MementoCenter
    + (void)saveMementoObject:(id <MementoCenterProtocol>)object withKey:(NSString *)key {
        NSParameterAssert(object);
        NSParameterAssert(key);
    
        // 获取data
    
        id      data    = [object currentState]; //按上面顺序是很难看明白这里上面意思,要去看Apple再看这里。
        NSData *tmpData = [FastCoder dataWithRootObject:data];
    
        // 进行存储
    
        if (tmpData) {
            [[NSUserDefaults standardUserDefaults] setObject:tmpData  forKey:key];
        }
    }
    
    + (id)mementoObjectWithKey:(NSString *)key {
        NSParameterAssert(key);
    
        id     data    = nil;
        NSData *tmpData = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    
        if (tmpData) {
            data = [FastCoder objectWithData:tmpData];
        }
        return data;
    }
    @end
    
    

    MementoCenter

    高度封装

    #import <Foundation/Foundation.h>
    @interface NSObject (MementoCenter)
    
    /**
     *  存储状态
     *
     *  @param key 键值
     */
    - (void)saveStateWithKey:(NSString *)key;
    
    /**
     *  恢复状态
     *
     *  @param key 键值
     */
    - (void)recoverFromStateWithKey:(NSString *)key;
    @end
    
    #import "NSObject+MementoCenter.h"
    #import "MementoCenter.h"
    @implementation NSObject (MementoCenter)
    - (void)saveStateWithKey:(NSString *)key {
        NSParameterAssert(key);    
    
        id <MementoCenterProtocol> obj = (id <MementoCenterProtocol>)self;
        if ([obj respondsToSelector:@selector(currentState)]) {
    
       [MementoCenter saveMementoObject:obj withKey:key];
        }
    }
    
    - (void)recoverFromStateWithKey:(NSString *)key {
        NSParameterAssert(key);
    
        id                         state = [MementoCenter mementoObjectWithKey:key];
        id <MementoCenterProtocol> obj   = (id <MementoCenterProtocol>)self;
        if ([obj respondsToSelector:@selector(recoverFromStateWithKey:)]) {
            [obj recoverFromState:state];
        }
    }
    

    Apple

    #import <Foundation/Foundation.h>
    #import "MementoCenterProtocol.h"
    
    @interface Apple : NSObject <MementoCenterProtocol>
    @property (nonatomic, strong) NSString *name;
    @property (nonatomic, strong) NSNumber *age;
    @end
    
    #import "Apple.h"
    
    @implementation Apple
    - (id)currentState {
        return @{@"name": self.name,
                 @"age" : self.age};
    }
    
    - (void)recoverFromState:(id)state {
    
        NSDictionary *data = state;
        self.name = data[@"name"];
        self.age  = data[@"age"];
    }
    @end
    
    

    使用

    #import "ViewController.h"
    #import "Apple.h"
    #import "MementoCenter.h"
    #import "NSObject+MementoCenter.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
    
        [super viewDidLoad];
        Apple *apple = [[Apple alloc] init];
        apple.name = @"JiKeXueYuan";
        apple.age  = @(30);
    
      
    //一般的使用方法
    //    // save state 
    //    [MementoCenter saveMementoObject:apple  withKey:@"Apple"];
    //    
    
    //    // get state
    //    [apple recoverFromState:[MementoCenter mementoObjectWithKey:@"Apple"]];
    
    
    //高度封装的使用方法
        [apple saveStateWithKey:@"Ben"];
        [apple recoverFromStateWithKey:@"Ben"];
        NSLog(@"name:%@ age:%@", apple.name, apple.age);
    
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:iOS设计模式:备忘录

          本文链接:https://www.haomeiwen.com/subject/adplgttx.html