国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

詳解iOS App設計模式開發中對于享元模式的運用

2020-07-26 03:23:35
字體:
來源:轉載
供稿:網友

享元模式的概念

在面向對象軟件設計中,利用公共對象不僅能節省資源還能提高性能。共享的對象只能提供某些內在的信息,而不能用來識別對象。專門用于設計可共享對象的一種設計模式叫做享元模式(Flyweight pattern)。

實現享元模式需要兩個關鍵組件,通常是可共享的享元對象和保存他們的池。某種中央對象維護這個池,并從它返回適當的實例。
運用共享技術有效地支持大量細粒度的對象。

公共交通(如公共汽車)已有一百多年的歷史了。大量去往相同方向的乘客可以分擔保有和經營車輛(如公共汽車)的費用。公共汽車有多個站臺,乘客沿著路線在接近他們目的地的地方上下車。到達目的地的費用僅與行程有關。跟保有車輛相比,乘坐公共汽車要便宜得多。這就是利用公共資源的好處。

在面向對象軟件設計中,我們利用公共對象不僅能節省資源還能提高性能。比方說,某個人物需要一個類的一百萬個實例,但我們可以把這個類的一個實例讓大家共享,而把某些獨特的信息放在外部,節省的資源可能相當可觀(一個實例與一百萬個實例的差別)。共享的對象只提供某些內在的信息,而不能用來識別對象。專門用于設計可共享對象的一種設計模式叫做享元模式。

使得享元對象是輕量級的最重要原因是什么呢?不是它們的大小,而是通過共享能夠節省的空間總量。某些對象的獨特狀態可以拿到外部,在別處管理,其余部分被共享。比如說,原來需要一個類的一百萬個對象,但因為這個類的對象為享元,現在只要一個就夠了。這就是由于可共享的享元對象讓整個系統變得輕量的原因。通過仔細的設計,內存的節省非常可觀。在iOS開發中,節省內存意味著提升整體性能。


享元模式的實例應用

    我們創建一個WebSiteFactory工廠類,來維護池中的享元對象,根據父類型返回各種類型的具體享元對象,代碼如下:

復制代碼 代碼如下:

#import <Foundation/Foundation.h>
#import "WebSiteProtocol.h"
@interface WebSiteFactory : NSObject
 
@property (nonatomic, strong) NSDictionary *flyweights; //共享對象
 
- (id<WebSiteProtocol>)getWebSiteCategory:(NSString *)webKey;
- (NSInteger)getWebSiteCount;
 
@end

復制代碼 代碼如下:

#import "WebSiteFactory.h"
#import "ConcreteWebSite.h"
@implementation WebSiteFactory
 
- (instancetype)init {
    self = [super init];
    if (self) {
        _flyweights = [NSDictionary dictionary];
    }
    return self;
}
 
- (id<WebSiteProtocol>)getWebSiteCategory:(NSString *)webKey {   
    __block id<WebSiteProtocol> webset = nil;
    [self.flyweights enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        if (webKey == key) {
            webset = obj;
            *stop = YES;
        }
    }];
    
    if (webset == nil) {
        ConcreteWebSite *concreteWebset = [[ConcreteWebSite alloc] init];
        concreteWebset.webName = webKey;
        webset = concreteWebset;
        
        NSMutableDictionary *mutabledic = [NSMutableDictionary dictionaryWithDictionary:self.flyweights];
        [mutabledic setObject:webset forKey:webKey];
        self.flyweights = [NSDictionary dictionaryWithDictionary:mutabledic];
    }
    
    return webset;
}
 
- (NSInteger)getWebSiteCount {
    return self.flyweights.count;
}
 
@end

    代碼中的getWebSiteCategory方法可以返回具體的享元對象,返回的這個享元對象同時遵守WebSiteProtocol的協議,WebSiteProtocol的代碼如下:
復制代碼 代碼如下:

#import <Foundation/Foundation.h>
#import "User.h"
@protocol WebSiteProtocol <NSObject>
 
- (void)use:(User *)user;
 
@end

    ConcreteWebSite的代碼如下:
復制代碼 代碼如下:

#import <Foundation/Foundation.h>
#import "WebSiteProtocol.h"
@interface ConcreteWebSite : NSObject <WebSiteProtocol>
 
@property (nonatomic, copy) NSString *webName;
 
@end

復制代碼 代碼如下:

#import "ConcreteWebSite.h"
 
@implementation ConcreteWebSite
 
- (void)use:(User *)user {
    NSLog(@"網站分類:%@ 用戶名字:%@", self.webName, user.userName);
}
 
@end

    User的代碼如下:
復制代碼 代碼如下:

#import <Foundation/Foundation.h>
 
@interface User : NSObject
 
@property (nonatomic, copy) NSString *userName;
 
@end

復制代碼 代碼如下:

#import "User.h"
 
@implementation User
 
@end

    至此,享元模式的代碼已經完成了,我們來看下在客戶端怎么使用享元模式,代碼如下:
復制代碼 代碼如下:

#import "ViewController.h"
#import "WebSiteProtocol.h"
#import "WebSiteFactory.h"
#import "ConcreteWebSite.h"
#import "User.h"
typedef id<WebSiteProtocol> WebsiteType;
@interface ViewController ()
 
@end

復制代碼 代碼如下:

@implementation ViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    // 通過工廠方法返回各種具體享元對象,維護池中的享元對象
    WebSiteFactory *factory = [[WebSiteFactory alloc] init];
    
    // 返回具體的享元對象
    WebsiteType type1 = [factory getWebSiteCategory:@"首頁"];
    User *user1 = [[User alloc] init];
    user1.userName = @"張三";
    // 享元對象都具有use方法
    [type1 use:user1];
    
    WebsiteType type2 = [factory getWebSiteCategory:@"商店"];
    User *user2 = [[User alloc] init];
    user2.userName = @"李四";
    [type2 use:user2];
    
    WebsiteType type3 = [factory getWebSiteCategory:@"案例"];
    User *user3 = [[User alloc] init];
    user3.userName = @"王五";
    [type3 use:user3];
    
    NSInteger count = [factory getWebSiteCount];
    NSLog(@"個數: %ld", (long)count);
    
}
 
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
@end

    輸出如下:

2015-09-12 15:59:55.322 FlyweightPattern[42020:1723017] 網站分類:首頁 用戶名字:張三2015-09-12 15:59:55.322 FlyweightPattern[42020:1723017] 網站分類:商店 用戶名字:李四2015-09-12 15:59:55.322 FlyweightPattern[42020:1723017] 網站分類:案例 用戶名字:王五2015-09-12 15:59:55.323 FlyweightPattern[42020:1723017] 個數: 3

    分享相同的資源以執行任務,可能比使用個人的資源完成同樣的事情更加高效。享元模式可以通過共享一部分必需的對象,來節省大量的內存。
   
何時使用享元模式
(1)應用程序使用很多對象;
(2)在內存中保存對象會影響內存性能;
(3)對象的多數特有狀態(外在狀態)可以放到外部而輕量化;
(3)移除了外在狀態后,可以用較少的共享對象替代原來的那組對象;
(4)應用程序不依賴于對象標示,因為共享對象不能提供唯一的標示。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 淅川县| 克什克腾旗| 京山县| 竹北市| 白山市| 平遥县| 女性| 新和县| 高淳县| 马尔康县| 广平县| 洞头县| 多伦县| 嘉兴市| 旬阳县| 察雅县| 龙口市| 高雄县| 平山县| 鄱阳县| 仲巴县| 盐城市| 积石山| 宝山区| 竹山县| 温泉县| 肥东县| 登封市| 林周县| 石棉县| 灌阳县| 柳州市| 夏邑县| 棋牌| 枝江市| 焉耆| 沙洋县| 普兰县| 手游| 商南县| 什邡市|