IOS 设计模式 享元模式

转载:http://my.oschina.net/daguoshi/blog/505322

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

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

何为享元模式?

    实现享元模式需要两个关键组件,通常是可共享的享元对象和保存它们的池。某种中央对象为何这个池,并从它返回适当的实例。工厂是这一角色的理想候选。它可以通过一个工厂方法,根据父类型返回各种类型的具体享元对象,其主要的目的就是为何池中的享元对象,并适当地从中返回享元对象。

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

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

何时使用享元模式?

    @:应用程序使用很多对象。

    @:在内存中保存对象会影响内存性能。

    @:对象的多数特有状态可以放到外部而轻量化。

    @:移除了外在状态后,可以用较少的共享对象替代原来的那组对象。

    @:应用程序不依赖于对象标识,因为共享对象不能提供唯一的标识。

享元模式的实例应用

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

?
1
2
3
4
5
6
7
8
9
10
#import <Foundation/Foundation.h>
#import "WebSiteProtocol.h"
@interface WebSiteFactory : NSObject
 
@property (nonatomic, strong) NSDictionary *flyweights;  //共享对象
 
- (id<WebSiteProtocol>)getWebSiteCategory:(NSString *)webKey;
- (NSInteger)getWebSiteCount;
 
@end

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#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的代码如下:

?
1
2
3
4
5
6
7
#import <Foundation/Foundation.h>
#import "User.h"
@protocol WebSiteProtocol <NSObject>
 
- ( void )use:(User *)user;
 
@end

    ConcreteWebSite的代码如下:

?
1
2
3
4
5
6
7
#import <Foundation/Foundation.h>
#import "WebSiteProtocol.h"
@interface ConcreteWebSite : NSObject <WebSiteProtocol>
 
@property (nonatomic, copy) NSString *webName;
 
@end

?
1
2
3
4
5
6
7
8
9
#import "ConcreteWebSite.h"
 
@implementation ConcreteWebSite
 
- ( void )use:(User *)user {
     NSLog(@ "网站分类:%@ 用户名字:%@" , self.webName, user.userName);
}
 
@end

    User的代码如下:

?
1
2
3
4
5
6
7
#import <Foundation/Foundation.h>
 
@interface User : NSObject
 
@property (nonatomic, copy) NSString *userName;
 
@end

?
1
2
3
4
5
#import "User.h"
 
@implementation User
 
@end

    至此,享元模式的代码已经完成了,我们来看下在客户端怎么使用享元模式,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#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

    输出如下:

?
1
2
3
4
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

    分享相同的资源以执行任务,可能比使用个人的资源完成同样的事情更加高效。享元模式可以通过共享一部分必需的对象,来节省大量的内存。

   Demo链接地址:https://github.com/guoshimeihua/FlyweightPattern.git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值