Objective C 组合模式
来源:互联网 发布:linux开机不启动防火墙 编辑:程序博客网 时间:2024/04/29 08:52
组合模式(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;
}
- Objective C--组合模式
- Objective C 组合模式
- Objective-C 设计模式-组合模式 (Composite)
- iOS Objective C--组合模式
- iOS Objective C--组合模式
- Objective-C 委托模式
- Objective C--观察者模式
- Objective C--外观模式
- Objective C--原型模式
- Objective C--访问者模式
- Objective C--命令模式
- Objective C--代理模式
- Objective C--装饰模式
- Objective C--策略模式
- Objective C--迭代器模式
- Objective C--状态模式
- Objective C--备忘录模式
- Objective C--适配器模式
- 判断字符串中每个字符出现的次数
- Install Apache 2.4.2 on Ubuntu 12.04 LTS
- 抓取网页的本地数据存储
- JavaScript实现BASE62编码与解码
- sqlite备忘
- Objective C 组合模式
- Hibernate Oracle 11g 无法自动生成表
- reltool使用入门
- 优先队列
- 弹出窗口大全
- OpenOffice源文件中的文件扩展
- ORACLE SQL语句中的“SWITCH语句”函数DECODE
- UCOS ‖应用实例
- 用OpenCv找出最大连通域,并对其进行连通域进行ROI