十八、组合模式(Composite)

1. 何为组合模式

定义:将对象组合成树形结构以表示‘部分-整体’的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。

十八、组合模式(Composite)_第1张图片
图1-1 组合模式结构图

Component为组合中的对象声明接口,在适当情况下,实现所有类共有接口的默认行为。
  Leaf在组合中表示叶节点对象,叶节点没有子节点。
  Composite定义有枝节点行为,用来存储子部件,在Component接口中实现与子部件有关的操作,比如增加Add和删除Remove。

2. 情景设置

3. 代码实现

(1)ComComponent

@interface ComComponent : NSObject
{
    NSString *name;
}

- (ComComponent *)initWithName:(NSString *)myName;

// 通常都用add和remove方法来提供增加或者移除树叶或树枝的功能
- (void)add:(ComComponent *)c;
- (void)remove:(ComComponent *)c;
- (void)display:(int)depth;

@end
@implementation ComComponent

- (ComComponent *)initWithName:(NSString *)myName
{
    if (self = [super init]) {
        name = myName;
    }
    return self;
}

- (void)add:(ComComponent *)c
{
    return;
}

- (void)remove:(ComComponent *)c
{
    return;
}

- (void)display:(int)depth
{
    return;
}

@end

(2)Leaf

@interface Leaf : ComComponent

- (Leaf *)initWithName:(NSString *)myName;

@end
@implementation Leaf

- (Leaf *)initWithName:(NSString *)myName
{
    if (self = [super initWithName:myName]) {
        
    }
    return self;
}

- (void)add:(ComComponent *)c
{
    NSLog(@"Can not add a leaf");
}

- (void)remove:(ComComponent *)c
{
    NSLog(@"Can not remove a leaf");
}

- (void)display:(int)depth
{
    NSLog(@"[%dLevel]%@", depth, name);
}

@end

(3)Composite

@interface Composite : ComComponent
{
    NSMutableArray *childrenArr;
}

- (Composite *)initWithName:(NSString *)myName;

@end
@implementation Composite

- (Composite *)initWithName:(NSString *)myName
{
    if (self = [super initWithName:myName]) {
        childrenArr = [NSMutableArray array];
    }
    return self;
}

- (void)add:(ComComponent *)c
{
    [childrenArr addObject:c];
}

- (void)remove:(ComComponent *)c
{
    [childrenArr removeObject:c];
}

- (void)display:(int)depth
{
    NSLog(@"[%dLevel]%@", depth, name);
    for (ComComponent *component in childrenArr) {
        [component display:depth+1];
    }
}

@end

(4)客户端调用

Composite *root = [[Composite alloc] initWithName:@"root"];
[root add:[[Leaf alloc] initWithName:@"Leaf A"]];
[root add:[[Leaf alloc] initWithName:@"Leaf B"]];
    
Composite *comp = [[Composite alloc] initWithName:@"Composite X"];
[comp add:[[Leaf alloc] initWithName:@"Leaf XA"]];
[comp add:[[Leaf alloc] initWithName:@"Leaf XB"]];
[root add:comp];
    
Composite *comp1 = [[Composite alloc] initWithName:@"Composite XY"];
[comp1 add:[[Leaf alloc] initWithName:@"Leaf XYA"]];
[comp1 add:[[Leaf alloc] initWithName:@"Leaf XYB"]];
[root add:comp1];
    
[root add:[[Leaf alloc] initWithName:@"Leaf C"]];
Leaf *leaf = [[Leaf alloc] initWithName:@"Leaf D"];
[root add:leaf];
    
[root remove:leaf];
[root display:1];

控制台输出结果:

2017-06-21 18:38:29.268670+0800 Composite[76962:2201266] [1Level]root 2017-06-21 18:38:29.269293+0800 Composite[76962:2201266] [2Level]Leaf A 2017-06-21 18:38:29.269322+0800 Composite[76962:2201266] [2Level]Leaf B 2017-06-21 18:38:29.269337+0800 Composite[76962:2201266] [2Level]Composite X 2017-06-21 18:38:29.269351+0800 Composite[76962:2201266] [3Level]Leaf XA 2017-06-21 18:38:29.269363+0800 Composite[76962:2201266] [3Level]Leaf XB 2017-06-21 18:38:29.269376+0800 Composite[76962:2201266] [2Level]Composite XY 2017-06-21 18:38:29.269387+0800 Composite[76962:2201266] [3Level]Leaf XYA 2017-06-21 18:38:29.269398+0800 Composite[76962:2201266] [3Level]Leaf XYB 2017-06-21 18:38:29.269408+0800 Composite[76962:2201266] [2Level]Leaf C

该例子总共有三个类包括一个接口类ComComponents和两个接口类的子类,这样的话,我们可以直接在两个子类(Leaf和Composite)中去使用接口类中定义的方法,而且也可以重写方法。从而达到了组合(Composite)和单个对象(Leaf)的一致性。

组合模式的主要意图是让树形结构中的每个节点具有相同的抽象接口。这样整个结构可以作为一个统一的抽象结构使用,而不是暴露其内部表示。对每个结点的任何操作,可以通过协议或抽象基类中定义的相同接口来进行。

在Cocoa Touch 框架中,UIView被组织成一个组合结构。每个UIView的实例可以包含UIView的其他实例,形成统一的树形结构。让客户端对单个UIView对象和UIView的组合统一对待。

你可能感兴趣的:(十八、组合模式(Composite))