定義
組合模式允許你將對(duì)象組合成樹形結(jié)構(gòu)來表現(xiàn)”部分-整體“的層次結(jié)構(gòu)熟吏,使得客戶以一致的方式處理單個(gè)對(duì)象以及對(duì)象的組合妓雾。
組合模式實(shí)現(xiàn)的最關(guān)鍵的地方是——簡(jiǎn)單對(duì)象和復(fù)合對(duì)象必須實(shí)現(xiàn)相同的接口娶吞。這就是組合模式能夠?qū)⒔M合對(duì)象和簡(jiǎn)單對(duì)象進(jìn)行一致處理的原因。(有點(diǎn)遞歸的意思)
角色
- 組合部件(Component):它是一個(gè)抽象角色械姻,為要組合的對(duì)象提供統(tǒng)一的接口妒蛇。
- 葉子(Leaf):在組合中表示子節(jié)點(diǎn)對(duì)象机断,葉子節(jié)點(diǎn)不能有子節(jié)點(diǎn)。
- 合成部件(Composite):定義有枝節(jié)點(diǎn)的行為绣夺,用來存儲(chǔ)部件吏奸,實(shí)現(xiàn)在Component接口中的有關(guān)操作,如增加(Add)和刪除(Remove)陶耍。
分類
透明的組合模式奋蔚、安全的組合模式
場(chǎng)景模擬
設(shè)計(jì)XML 結(jié)構(gòu),要求打印每個(gè)節(jié)點(diǎn)的名字烈钞;
組合模式的UML圖
安全模式代碼
#import <Foundation/Foundation.h>
@interface ComponentSafe : NSObject
@property (nonatomic,strong) NSString * name;
- (instancetype)initWithName:(NSString *)name;
-(void)display:(int)level;
@end
#import "ComponentSafe.h"
@implementation ComponentSafe
- (instancetype)initWithName:(NSString *)name
{
self = [super init];
if (self) {
self.name = name;
}
return self;
}
@end
#import "ComponentSafe.h"
@interface CompositeSafe : ComponentSafe
-(void)add:(ComponentSafe *)component;
-(void)remove:(ComponentSafe *)component;
@end
#import "CompositeSafe.h"
@interface CompositeSafe()
@property (nonatomic,strong) NSMutableArray *children;
@end
@implementation CompositeSafe
-(instancetype)initWithName:(NSString *)name{
self= [super initWithName:name];
if (self) {
self.children = [NSMutableArray array];
}
return self;
}
-(void)add:(ComponentSafe *)component{
[self.children addObject:component];
}
-(void)remove:(ComponentSafe *)component{
[self.children removeObject:component];
}
-(void)display:(int)level{
NSLog(@"- %d %@",level,self.name);
for (ComponentSafe * component in self.children) {
[component display:level +2];
}
}
@end
#import <Foundation/Foundation.h>
#import "ComponentSafe.h"
@interface LeafSafe : ComponentSafe
@end
#import "LeafSafe.h"
@implementation LeafSafe
-(void)display:(int)level{
NSLog(@"- %d %@",level,self.name);
}
@end
測(cè)試代碼
CompositeSafe * root = [[CompositeSafe alloc]initWithName:@"root"];
[root add:[[LeafSafe alloc]initWithName:@"Leaf A in Root"]];
[root add:[[LeafSafe alloc]initWithName:@"Leaf B in Root"]];
CompositeSafe * branchX = [[CompositeSafe alloc]initWithName:@"branch x in root"];;
CompositeSafe * branchY = [[CompositeSafe alloc]initWithName:@"branch y in root"];;
[root add:branchX];
[root add:branchY];
[branchX add:[[LeafSafe alloc]initWithName:@"Leaf in branchA"]];
CompositeSafe * branchZ = [[CompositeSafe alloc]initWithName:@"branch z in branch X"];;
[branchX add:branchZ];
[branchY add:[[LeafSafe alloc]initWithName:@"Leaf in branch Y"]];
[branchZ add:[[LeafSafe alloc]initWithName:@"Leaf in branch z"]];
[root display:1];
測(cè)試結(jié)果
2018-04-08 18:02:22.958308+0800 結(jié)構(gòu)型設(shè)計(jì)模式-組合模式[30405:7279512] - 1 root
2018-04-08 18:02:22.958412+0800 結(jié)構(gòu)型設(shè)計(jì)模式-組合模式[30405:7279512] - 3 Leaf A in Root
2018-04-08 18:02:22.958517+0800 結(jié)構(gòu)型設(shè)計(jì)模式-組合模式[30405:7279512] - 3 Leaf B in Root
2018-04-08 18:02:22.958680+0800 結(jié)構(gòu)型設(shè)計(jì)模式-組合模式[30405:7279512] - 3 branch x in root
2018-04-08 18:02:22.958934+0800 結(jié)構(gòu)型設(shè)計(jì)模式-組合模式[30405:7279512] - 5 Leaf in branchA
2018-04-08 18:02:22.959220+0800 結(jié)構(gòu)型設(shè)計(jì)模式-組合模式[30405:7279512] - 5 branch z in branch X
2018-04-08 18:02:22.959381+0800 結(jié)構(gòu)型設(shè)計(jì)模式-組合模式[30405:7279512] - 7 Leaf in branch z
2018-04-08 18:02:22.959570+0800 結(jié)構(gòu)型設(shè)計(jì)模式-組合模式[30405:7279512] - 3 branch y in root
2018-04-08 18:02:22.959792+0800 結(jié)構(gòu)型設(shè)計(jì)模式-組合模式[30405:7279512] - 5 Leaf in branch Y
透明模式代碼
#import <Foundation/Foundation.h>
@interface Component : NSObject
@property (nonatomic,strong) NSString *name;
-(void)add:(Component *)component;
-(void)remove:(Component *)component;
-(void)display:(int)level;
@end
#import "Component.h"
@implementation Component
@end
#import "Component.h"
@interface Leaf : Component
@end
#import "Leaf.h"
@implementation Leaf
-(void)add:(Component *)component{
NSLog(@"can not add a component to a leaf.");
}
-(void)remove:(Component *)component{
NSLog(@"can not remove a component to a leaf");
}
-(void)display:(int)level{
NSLog(@"- %d %@",level,self.name);
}
@end
#import "Component.h"
@interface Composite : Component
@end
#import "Composite.h"
@interface Composite()
@property (nonatomic,strong) NSMutableArray *children;
@end
@implementation Composite
- (instancetype)init
{
self = [super init];
if (self) {
self.children = [NSMutableArray array];
}
return self;
}
-(void)add:(Component *)component{
[self.children addObject:component];
}
-(void)remove:(Component *)component{
[self.children removeObject:component];
}
-(void)display:(int)level{
NSLog(@"- %d %@",level,self.name);
for (Component * component in self.children) {
[component display:level +2];
}
}
@end
測(cè)試代碼
Component * root = [Composite new];
root.name =@"root";
Component * a = [Leaf new];
a.name = @"Leaf in Root";
[root add:a];
Component * b = [Leaf new];
b.name = @"Leaf in Root";
[root add:b];
Component * branchX = [Composite new];
branchX.name=@"branch x in root";
[root add:branchX];
Component * branchY = [Composite new];
branchY.name=@"branch y in root";
[root add:branchY];
Component * leafA = [Leaf new];
leafA.name = @"leaf in Branch X";
[branchX add:leafA];
Component * branchZ = [Composite new];
branchZ.name = @"branch z in Branch X";
[branchX add:branchZ];
Component * leafY = [Leaf new];
leafY.name = @"leaf in Branch y";
[branchY add:leafY];
Component * leafz = [Leaf new];
leafz.name = @"leaf in Branch z";
[branchZ add:leafz];
[root display:1];
測(cè)試結(jié)果
2018-04-08 18:02:22.957212+0800 結(jié)構(gòu)型設(shè)計(jì)模式-組合模式[30405:7279512] - 1 root
2018-04-08 18:02:22.957364+0800 結(jié)構(gòu)型設(shè)計(jì)模式-組合模式[30405:7279512] - 3 Leaf in Root
2018-04-08 18:02:22.957458+0800 結(jié)構(gòu)型設(shè)計(jì)模式-組合模式[30405:7279512] - 3 Leaf in Root
2018-04-08 18:02:22.957554+0800 結(jié)構(gòu)型設(shè)計(jì)模式-組合模式[30405:7279512] - 3 branch x in root
2018-04-08 18:02:22.957659+0800 結(jié)構(gòu)型設(shè)計(jì)模式-組合模式[30405:7279512] - 5 leaf in Branch X
2018-04-08 18:02:22.957873+0800 結(jié)構(gòu)型設(shè)計(jì)模式-組合模式[30405:7279512] - 5 branch z in Branch X
2018-04-08 18:02:22.957955+0800 結(jié)構(gòu)型設(shè)計(jì)模式-組合模式[30405:7279512] - 7 leaf in Branch z
2018-04-08 18:02:22.958030+0800 結(jié)構(gòu)型設(shè)計(jì)模式-組合模式[30405:7279512] - 3 branch y in root
2018-04-08 18:02:22.958141+0800 結(jié)構(gòu)型設(shè)計(jì)模式-組合模式[30405:7279512] - 5 leaf in Branch y
透明組合模式和安全組合模式區(qū)別就是葉子是否有add:和remove方法泊碑。有就是透明組合,沒有就是安全組合毯欣,(因?yàn)槿~子調(diào)用add:和remove:在編譯階段就報(bào)錯(cuò)了)
優(yōu)缺點(diǎn)
優(yōu)點(diǎn)
- 組合模式使得客戶端代碼可以一致地處理對(duì)象和對(duì)象容器馒过,無需關(guān)心處理的是單個(gè)對(duì)象,還是組合的對(duì)象容器酗钞。
- 將”客戶代碼與復(fù)雜的對(duì)象容器結(jié)構(gòu)“解耦腹忽。
- 可以更容易地往組合對(duì)象中加入新的構(gòu)件。
缺點(diǎn)
- 使得設(shè)計(jì)更加復(fù)雜砚作【阶啵客戶端需要花更多時(shí)間理清類之間
的層次關(guān)系。
注意問題
- 有時(shí)候系統(tǒng)需要遍歷一個(gè)樹枝結(jié)構(gòu)的子構(gòu)件很多次偎巢,這時(shí)候可以考慮把遍歷子構(gòu)件的結(jié)構(gòu)存儲(chǔ)在父構(gòu)件里面作為緩存蔼夜。
- 客戶端盡量不要直接調(diào)用樹葉類中的方法(在我上面實(shí)現(xiàn)就是這樣的,創(chuàng)建的是一個(gè)樹枝的具體對(duì)象;)压昼,而是借用其父類(Graphics)的多態(tài)性完成調(diào)用求冷,這樣可以增加代碼的復(fù)用性。
《設(shè)計(jì)模式》一書中提倡:相對(duì)于安全性窍霞,我們比較強(qiáng)調(diào)透明性匠题。對(duì)于第一種方式中葉子節(jié)點(diǎn)內(nèi)不需要的方法可以使用空處理或者異常報(bào)告的方式來解決。(因?yàn)檫@樣可以使用基類處理但金,符合依賴倒置原則韭山,有更好的擴(kuò)展性)