iOS 備忘錄模式(簡(jiǎn)單使用)

  • 備忘錄模式
    設(shè)計(jì)存儲(chǔ)中心,指定存儲(chǔ)接口,實(shí)現(xiàn)存儲(chǔ)機(jī)制。
    • 優(yōu)化存儲(chǔ)方案
      統(tǒng)一存儲(chǔ)規(guī)范,實(shí)現(xiàn)靈活多變的存儲(chǔ)機(jī)制。

FastCoder 本地序列化,轉(zhuǎn)NSData,比對(duì)象實(shí)現(xiàn)NSCopying存儲(chǔ)好

  • 應(yīng)用,使用場(chǎng)景
    存儲(chǔ)UIView的狀態(tài),
    數(shù)據(jù)庫(kù)回退

備忘錄中心

//
//  MementoCenter.h
//  LearnMemento
//
//  Created by 印林泉 on 2017/3/8.
//  Copyright ? 2017年 ylq. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "MementoCenterProtocol.h"

@interface MementoCenter : NSObject

/**
 存儲(chǔ)備忘錄對(duì)象

 @param object 值
 @param key 鍵
 */
+ (void)saveMementoObject:(id<MementoCenterProtocol>)object withKey:(NSString *)key;

/**
 獲取備忘錄對(duì)象

 @param key 鍵
 @return 值
 */
+ (id)mementoObjectWithKey:(NSString *)key;

@end
//
//  MementoCenter.m
//  LearnMemento
//
//  Created by 印林泉 on 2017/3/8.
//  Copyright ? 2017年 ylq. All rights reserved.
//

#import "MementoCenter.h"

@implementation MementoCenter

+ (void)saveMementoObject:(id)object withKey:(NSString *)key {
    
}

+ (id)mementoObjectWithKey:(NSString *)key {
    return nil;
}

@end

備忘錄中心協(xié)議,存儲(chǔ)的對(duì)象必須滿足這個(gè)協(xié)議,才能存儲(chǔ)到備忘錄中心

//
//  MementoCenterProtocol.h
//  LearnMemento
//
//  Created by 印林泉 on 2017/3/8.
//  Copyright ? 2017年 ylq. All rights reserved.
//

#import <Foundation/Foundation.h>

@protocol MementoCenterProtocol <NSObject>

/**
 獲取狀態(tài)

 @return 狀態(tài)值
 */
- (id)currentState;

/**
 從某種狀態(tài)恢復(fù)

 @param state 狀態(tài)值
 */
- (void)recoverFromState:(id)state;

@end

存儲(chǔ)對(duì)象(蘋(píng)果)

//
//  Apple.h
//  LearnMemento
//
//  Created by 印林泉 on 2017/3/8.
//  Copyright ? 2017年 ylq. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "MementoCenterProtocol.h"

@interface Apple : NSObject<MementoCenterProtocol>

@end
//
//  Apple.m
//  LearnMemento
//
//  Created by 印林泉 on 2017/3/8.
//  Copyright ? 2017年 ylq. All rights reserved.
//

#import "Apple.h"

@implementation Apple

- (id)currentState {
    return self;
}

- (void)recoverFromState:(id)state {
    
}

@end

使用

//
//  ViewController.m
//  LearnMemento
//
//  Created by 印林泉 on 2017/3/8.
//  Copyright ? 2017年 ylq. All rights reserved.
//

#import "ViewController.h"
#import "Apple.h"
#import "MementoCenter.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    Apple *apple = [[Apple alloc] init];
    ///save
    [MementoCenter saveMementoObject:[apple currentState] withKey:@"Apple"];
    ///get
    [apple recoverFromState:[MementoCenter mementoObjectWithKey:@"Apple"]];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1 場(chǎng)景問(wèn)題# 1.1 開(kāi)發(fā)仿真系統(tǒng)## 考慮這樣一個(gè)仿真應(yīng)用,功能是:模擬運(yùn)行針對(duì)某個(gè)具體問(wèn)題的多個(gè)解決方案,記...
    七寸知架構(gòu)閱讀 2,248評(píng)論 1 50
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,193評(píng)論 4 61
  • *面試心聲:其實(shí)這些題本人都沒(méi)怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個(gè)offer,總結(jié)起來(lái)就是把...
    Dove_iOS閱讀 27,604評(píng)論 30 472
  • // //RYTCustomAlertViewController.h //wojiubuxinle // //C...
    TimmyR閱讀 272評(píng)論 0 1

友情鏈接更多精彩內(nèi)容