Oh!Coder

Coding Life

Objective C--享元模式

| Comments

今天和大家分享的模式叫享元模式。

首先,让我们给出享元模式的定义。

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

享元模式可以避免大量非常相似类的开销。在程序设计中,有时需要生成大量细粒度的类实例来表示数据。如果能发现这些实例除了几个参数外基本上都是相同的,有时就能够受大幅度地减少需要实例化的类数量。如果能把那些参数移到类实例的外面,在方法调用时将它们传递进来,就可以通过共享大幅度地减少单个实例的数目。

下面先给出类结构图:

pic

从图中可以看出,Flyweight执行时所需的状态可能有内部的也可能有外部的,内部状态存储于ConcreteFlyweight对象之中,而外部对象则应该考虑由客户端对象存储或计算,当调用Flyweight对象的操作时,将该状态传递给它。

那么,一般什么情况下使用享元模式呢?如果一个应用程序使用了大量的对象,而大量的这些对象造成了很大的存储开销时就应该考虑使用;还有就是对象的大多数状态可以作为外部状态,如果删除对象的外部状态,那么可以用相对较少的共享对象取代很多组对象,此时可以考虑使用享元模式。不过,享元模式也是有缺陷的,使用享元模式需要维护一个记录了系统已有的所有享元的列表,而这本身需要耗费资源,另外享元模式使得系统更加复杂。为了使对象可以共享,需要将一些状态外部化,这使得程序的逻辑复杂化。因此,应当在有足够多的对象实例可供共享时才值得使用享元模式。

好的,下面给出简单的代码实现。

注意:本文所有代码均在ARC环境下编译通过。

  • Flyweight类接口
1
2
3
4
5
#import <Foundation/Foundation.h>

@interface Flyweight:NSObject
-(void)Operation:(int)extrinsicestate;
@end
  • Flyweight类实现
1
2
3
4
5
6
7
#import "Flyweight.h"

@implementation Flyweight
-(void)Operation:(int)extrinsicestate{
    return;
}
@end
  • ConcreteFlyweight类接口
1
2
3
4
#import "Flyweight.h"

@interface ConcreteFlyweight :Flyweight
@end
  • ConcreteFlyweight类实现
1
2
3
4
5
6
7
#import "ConcreteFlyweight.h"

@implementation ConcreteFlyweight
-(void)Operation:(int)extrinsicestate{
    NSLog(@"具体Flyweight:%d", extrinsicestate);
}
@end
  • UnshareConcreteFlyweight类接口
1
2
3
4
#import "Flyweight.h"

@interface UnshareConcreteFlyweight:Flyweight
@end
  • UnshareConcreteFlyweight类实现
1
2
3
4
5
6
7
#import "UnshareConcreteFlyweight.h"

@implementation UnshareConcreteFlyweight
-(void)Operation:(int)extrinsicestate{
    NSLog(@"不共享的具体Flyweight:%d", extrinsicestate);
}
@end
  • FlyweightFactory类接口
1
2
3
4
5
6
7
8
9
#import <Foundation/Foundation.h>

@class Flyweight;
@class ConcreteFlyweight;
@interface FlyweightFactory :NSObject{
    NSMutableDictionary *nd;
}
-(Flyweight*)GetFlyweight:(NSString*)key;
@end
  • FlyweightFactory类实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#import "FlyweightFactory.h"
#import "ConcreteFlyweight.h"

@implementation FlyweightFactory
-(id)init{
    if (self == [super init]) {
        nd= [NSMutableDictionary dictionary];
        [nd setObject:[[ConcreteFlyweightalloc]init]forKey:@"X"];
        [nd setObject:[[ConcreteFlyweightalloc]init]forKey:@"Y"];
        [nd setObject:[[ConcreteFlyweightalloc]init]forKey:@"Z"];
    }
    return self;
}
-(Flyweight*)GetFlyweight:(NSString*)key{
    return [nd objectForKey:key];
}
@end
  • Main方法调用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#import <Foundation/Foundation.h>
#import "Flyweight.h"
#import "FlyweightFactory.h"
#import "UnshareConcreteFlyweight.h"

int main (int argc,const char * argv[])
{
    @autoreleasepool{
        int extrinsicstate =22;
        FlyweightFactory *f = [[FlyweightFactory alloc]init];
        Flyweight *fx = [f GetFlyweight:@"X"];
        [fx Operation:--extrinsicstate];
        Flyweight *fy = [f GetFlyweight:@"Y"];
        [fy Operation:--extrinsicstate];
        Flyweight *fz = [f GetFlyweight:@"Z"];
        [fz Operation:--extrinsicstate];
        Flyweight *uf = [[UnshareConcreteFlyweight alloc]init];
        [uf Operation:--extrinsicstate];
    }
    return 0;
}

上面是简单演示代码,还是那句老话,关键还是在思想,尽可能的融会贯通。尽可能做到手中无剑心中有剑!这才是上乘武学的方向!

Comments