今天和大家分享的模式是组合模式。组合模式具体说来也不复杂。下面还是先给出定义。
组合模式(Composite),将对象组合成树形结构以表示‘部分-整体’的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。
组合模式的适用场合是,一般当你发现需求中是体现部分与整体层次的结构时,以及你希望用户可以忽略组合对象与单个对象的不同,统一地使用组合结构中地所有对象时,就应该考虑使用组 合模式了。组合模式定义了基本对象和组合对象的类层次结构。基本对象可以被组合成更复杂的组合对象,而这个组合对象又可以被组合,这样不断地递归下去,这样,在客户代码中,任何使用到基本对象的地方都可以使用组合对象了。说了这么多,其实,组合模式就是让客户可以一致地使用组合结构和单个对象。那么,下面还是给出类结构图,想必大家一看就明白了。
上图中类之间的关系基本可以类比为一棵树的关系。有根(Component)、有枝节点(Composite)、有叶子节点(Leaf)。逻辑很清晰,结构也比较简单。其实相当于叶子节点(Leaf)和枝节点(Composite)都继承自根节点(Component)。好的,下面给出简单的代码实现。
-
ComComponents类接口 //对应图中的Component
1
2
3
4
5
6
7
8
9
10
|
#import <Foundation/Foundation.h>
@interface
ComComponents:
NSObject
{
NSString
*name;
}
-(ComComponents*)MyInit:(
NSString
*)myName;
-(
void
)Add:(ComComponents*)c;
-(
void
)Remove:(ComComponents*)c;
-(
void
)Display:(
int
)depth;
@end
|
-
ComComponents类实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#import "ComComponents.h"
@implementation
ComComponents
-(ComComponents*)MyInit:(
NSString
*)myName{
name = myName;
return
self
;
}
-(
void
)Add:(ComComponents *)c{
return
;
}
-(
void
)Remove:(ComComponents *)c{
return
;
}
-(
void
)Display:(
int
)depth{
return
;
}
@end
|
-
Leaf类接口
1
2
3
4
5
|
#import "ComComponents.h"
@interface
Leaf:ComComponents
-(Leaf*)MyInit:(
NSString
*)myName;
@end
|
-
Leaf类实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#import "Leaf.h"
@implementation
Leaf
-(Leaf*)MyInit:(
NSString
*)myName{
name = myName;
return
self
;
}
-(
void
)Add:(ComComponents *)c{
NSLog
(@
"Cannot add to a leaf"
);
}
-(
void
)Remove:(ComComponents *)c{
NSLog
(@
"Cannot remove from a leaf"
);
}
-(
void
)Display:(
int
)depth{
NSLog
(@
"[%dLevel]%@"
, depth,name);
}
@end
|
-
Composite类接口
1
2
3
4
5
6
7
|
#import "ComComponents.h"
@interface
Composite :ComComponents{
NSMutableArray
*children;
}
-(Composite*)MyInit:(
NSString
*)myName;
@end
|
-
Composite类实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#import "Composite.h"
@implementation
Composite
-(Composite*)MyInit:(
NSString
*)myName{
name = myName;
children= [
NSMutableArray
new
];
return
self
;
}
-(
void
)Add:(ComComponents *)c{
[children addObject:c];
}
-(
void
)Remove:(ComComponents *)c{
[children addObject:c];
}
-(
void
)Display:(
int
)depth{
NSLog
(@
"[%dLevel]%@"
, depth,name);
for
(ComComponents *component in children)
[component Display:depth +1];
}
@end
|
-
Main方法调用
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
|
#import <Foundation/Foundation.h>
#import "Leaf.h"
#import "Composite.h"
int
main (
int
argc,
const
char
*argv[])
{
@autoreleasepool
{
Composite *root = [[Composite alloc]MyInit:@
"root"
];
[root Add:[[Leaf alloc]MyInit:@
"Leaf A"
]];
[root Add:[[Leaf alloc]MyInit:@
"Leaf B"
]];
Composite *comp = [[Composite alloc]MyInit:@
"Composite X"
];
[comp Add:[[Leaf alloc]MyInit:@
"Leaf XA"
]];
[comp Add:[[Leaf alloc]MyInit:@
"Leaf XB"
]];
[root Add:comp];
Composite *comp2 = [[Composite alloc]MyInit:@
"Composite XY"
];
[comp2 Add:[[Leaf alloc]MyInit:@
"Leaf XYA"
]];
[comp2 Add:[[Leaf alloc]MyInit:@
"Leaf XYB"
]];
[comp Add:comp2];
[root Add:[[Leaf alloc]MyInit:@
"Leaf C"
]];
Leaf *leaf = [[Leaf alloc]MyInit:@
"Leaf D"
];
[root Add:leaf];
[root Remove:leaf];
[root Display:1];
}
return
0;
}
|
好啦,组合模式结构还算简单,关键还是活学活用啊,这个才是真难啊!