美文网首页
享元模式

享元模式

作者: 泥孩儿0107 | 来源:发表于2017-12-09 15:57 被阅读0次

    享元模式的概念

    在面向对象软件设计中,利用公共对象不仅能节省资源还能提高性能。共享的对象只能提供某些内在的信息,而不能用来识别对象。专门用于设计可共享对象的一种设计模式叫做享元模式(Flyweight pattern)。

    实现享元模式需要两个关键组件,通常是可共享的享元对象和保存他们的池。某种中央对象维护这个池,并从它返回适当的实例。

    运用共享技术有效地支持大量细粒度的对象。

    公共交通(如公共汽车)已有一百多年的历史了。大量去往相同方向的乘客可以分担保有和经营车辆(如公共汽车)的费用。公共汽车有多个站台,乘客沿着路线在接近他们目的地的地方上下车。到达目的地的费用仅与行程有关。跟保有车辆相比,乘坐公共汽车要便宜得多。这就是利用公共资源的好处。

    在面向对象软件设计中,我们利用公共对象不仅能节省资源还能提高性能。比方说,某个人物需要一个类的一百万个实例,但我们可以把这个类的一个实例让大家共享,而把某些独特的信息放在外部,节省的资源可能相当可观(一个实例与一百万个实例的差别)。共享的对象只提供某些内在的信息,而不能用来识别对象。专门用于设计可共享对象的一种设计模式叫做享元模式。

    使得享元对象是轻量级的最重要原因是什么呢?不是它们的大小,而是通过共享能够节省的空间总量。某些对象的独特状态可以拿到外部,在别处管理,其余部分被共享。比如说,原来需要一个类的一百万个对象,但因为这个类的对象为享元,现在只要一个就够了。这就是由于可共享的享元对象让整个系统变得轻量的原因。通过仔细的设计,内存的节省非常可观。在iOS开发中,节省内存意味着提升整体性能。

    享元模式的实例应用

    我们创建一个WebSiteFactory工厂类,来维护池中的享元对象,根据父类型返回各种类型的具体享元对象,代码如下:

    复制代码代码如下:

    #import

    #import "WebSiteProtocol.h"

    @interface WebSiteFactory : NSObject

    @property (nonatomic, strong) NSDictionary *flyweights; //共享对象

    - (id)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)getWebSiteCategory:(NSString *)webKey {

    __block id 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

    #import "User.h"

    @protocol WebSiteProtocol

    - (void)use:(User *)user;

    @end

    ConcreteWebSite的代码如下:

    复制代码代码如下:

    #import

    #import "WebSiteProtocol.h"

    @interface ConcreteWebSite : NSObject

    @property (nonatomic, copy) NSString *webName;

    @end

    复制代码代码如下:

    #import "ConcreteWebSite.h"

    @implementation ConcreteWebSite

    - (void)use:(User *)user {

    NSLog(@"网站分类:%@ 用户名字:%@", self.webName, user.userName);

    }

    @end

    User的代码如下:

    复制代码代码如下:

    #import

    @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 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)应用程序不依赖于对象标示,因为共享对象不能提供唯一的标示。

    相关文章

      网友评论

          本文标题:享元模式

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