美文网首页
iOS设计模式 (四)享元模式

iOS设计模式 (四)享元模式

作者: handsome5 | 来源:发表于2018-05-12 18:26 被阅读19次
    • 享元模式(英语:Flyweight Pattern)。使用共享物件,减少同一类对象的大量创建,尽可能减少内存使用量以及分享资讯给尽可能多的相似物件,运用共享技术有效地支持大量细粒度的对象。
    • 享元模式UML


      4.png
    • 实现享元模式需要两个关键组件,
      1.可攻享享元对象
      2.享元池
      工厂是这一角色的理想候选。它可以通过一个工厂方法,根据父类型返回各种类型的具体享元对象,其主要的目的就是使用池中的享元对象,并适当地从中返回享元对象。
    • 享元模式的使用场景
      @:应用程序使用很多对象。
      @:在内存中保存对象会影响内存性能。
      @:对象的多数特有状态可以放到外部而轻量化。
      @:移除了外在状态后,可以用较少的共享对象替代原来的那组对象。
      @:应用程序不依赖于对象标识,因为共享对象不能提供唯一的标识。

    享元模式的实例应用

    • User类
    #import <Foundation/Foundation.h>
    
    @interface User : NSObject
    
    @property (nonatomic, copy)NSString *useName;
    
    @end
    #import "User.h"
    
    @implementation User
    
    @end
    
    • FlySiteProtocol 协议
    #import <Foundation/Foundation.h>
    #import "User.h"
    
    @protocol FlySiteProtocol <NSObject>
    
    - (void)use:(User *)user;
    
    @end
    
    • ConcreteFlySite 类
    #import <Foundation/Foundation.h>
    #import "FlySiteProtocol.h"
    
    @interface ConcreteFlySite : NSObject <FlySiteProtocol>
    
    @property (nonatomic, copy) NSString *webName;
    
    @end
    #import "ConcreteFlySite.h"
    
    
    @implementation ConcreteFlySite
    
    - (void)use:(User *)user
    {
        NSLog(@"网站分类:%@ 用户名字:%@", self.webName, user.useName);
    }
    
    @end
    
    • FlyWeightFactory 工厂类
    #import <Foundation/Foundation.h>
    #import "FlySiteProtocol.h"
    
    @interface FlyWeightFactory : NSObject
    
    @property (nonatomic, strong) NSDictionary *flyweights; //共享对象
    
    - (id <FlySiteProtocol>)flyWeightCategory:(NSString *) webKey;
    
    - (NSInteger )getWebSiteCount;
    @end
    
    #import "FlyWeightFactory.h"
    #import "ConcreteFlySite.h"
    
    @implementation FlyWeightFactory
    
    - (instancetype)init
    {
        self = [super init];
        if (self) {
            _flyweights= [NSDictionary dictionary];
        }
        return self;
    }
    
    - (id <FlySiteProtocol>)flyWeightCategory:(NSString *)webKey
    {
        __block id <FlySiteProtocol> webFlySet = nil;
        
        [self.flyweights enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            if (webKey == key) {
                webFlySet = obj;
                * stop = YES;
            }
        }];
        
        if (webFlySet == nil) {
            ConcreteFlySite *concrete = [[ConcreteFlySite alloc] init];
            concrete.webName = webKey;
            webFlySet = concrete;
            
            NSMutableDictionary *mutableDict = [NSMutableDictionary dictionaryWithDictionary:self.flyweights];
            [mutableDict setObject:webFlySet forKey:webKey];
            self.flyweights = [NSDictionary dictionaryWithDictionary:mutableDict];
        }
        
        return webFlySet;
        
    }
    
    - (NSInteger)getWebSiteCount {
        return self.flyweights.count;
    }
    
    @end
    
    • ViewController 类
    #import "ViewController.h"
    #import "FlyWeightFactory.h"
    #import "ConcreteFlySite.h"
    #import "User.h"
    #import "FlySiteProtocol.h"
    
    typedef id <FlySiteProtocol> WebsiteType;
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    
    - (NSArray *)testArray
    {
        NSMutableArray *array = [NSMutableArray array];
        
        for (NSInteger i =  0; i < 5000; i ++) {
            if (i %2 == 0) {
                [array addObject:[NSString stringWithFormat:@"订单%ld",(long)i]];
            }
            else {
                [array addObject:[NSString stringWithFormat:@"订单%ld",(long)i]];
            }
        }
        
        return array;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        // 通过工厂方法返回各种具体享元对象,维护池中的享元对象
        FlyWeightFactory *factory = [[FlyWeightFactory alloc] init];
        
        
        for (NSInteger i = 0; i < [self testArray].count; i++ ) {
            WebsiteType type  = [factory flyWeightCategory:[self testArray][i]];
            User *user = [[User alloc] init];
            user.useName = [NSString stringWithFormat:@"用户%ld",i];
            [type use:user];
        }
        
    //     WebsiteType type1 =  [factory flyWeightCategory:@"首页"];
    //
    //    NSLog(@"factroy=====:%ld",[factory getWebSiteCount]);
    //
    ////    //  返回具体的享元对象
    //
    //    User *user1 = [[User alloc] init];
    //    user1.useName = @"张三";
    //    // 享元对象都具有use方法
    //    [type1 use:user1];
    //
    //    WebsiteType type2 = [factory flyWeightCategory:@"商店"];
    //    User *user2 = [[User alloc] init];
    //    user2.useName = @"李四";
    //    [type2 use:user2];
    //
    //     NSLog(@"factroy111=====:%ld",[factory getWebSiteCount]);
    }
    @end
    

    相关文章

      网友评论

          本文标题:iOS设计模式 (四)享元模式

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