组合模式
组合模式(Composite Pattern),又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象。组合模式依据树形结构来组合对象,用来表示部分以及整体层次。这种类型的设计模式属于结构型模式,它创建了对象组的树形结构。
这种模式创建了一个包含自己对象组的类。该类提供了修改相同对象组的方式。
我们通过下面的实例来演示组合模式的用法。实例演示了一个组织中员工的层次结构。
介绍
意图: 将对象组合成树形结构以表示"部分-整体"的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。
主要解决:它在我们树型结构的问题中,模糊了简单元素和复杂元素的概念,客户程序可以向处理简单元素一样来处理复杂元素,从而使得客户程序与复杂元素的内部结构解耦。
何时使用: 1、您想表示对象的部分-整体层次结构(树形结构)。 2、您希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。
如何解决: 树枝和叶子实现统一接口,树枝内部组合该接口。
关键代码: 树枝内部组合该接口,并且含有内部属性 List,里面放 Component。
应用实例: 1、算术表达式包括操作数、操作符和另一个操作数,其中,另一个操作符也可以是操作数、操作符和另一个操作数。 2、在 JAVA AWT 和 SWING 中,对于 Button 和 Checkbox 是树叶,Container 是树枝。
优点: 1、高层模块调用简单。 2、节点自由增加。
缺点: 在使用组合模式时,其叶子和树枝的声明都是实现类,而不是接口,违反了依赖倒置原则。
使用场景: 部分、整体场景,如树形菜单,文件、文件夹的管理。
注意事项: 定义时为具体类。
Android上的组合模式
View&ViewGroup
IOS上的组合模式
UIView的直观结构
UIView的层次结构
很直观的例子
Android
Employee.java
import java.util.ArrayList;
import java.util.List;
public class Employee {
private String name;
private String dept;
private int salary;
private List<Employee> subordinates;
//构造函数
public Employee(String name,String dept, int sal) {
this.name = name;
this.dept = dept;
this.salary = sal;
subordinates = new ArrayList<Employee>();
}
public void add(Employee e) {
subordinates.add(e);
}
public void remove(Employee e) {
subordinates.remove(e);
}
public List<Employee> getSubordinates(){
return subordinates;
}
public String toString(){
return ("Employee :[ Name : "+ name
+", dept : "+ dept + ", salary :"
+ salary+" ]");
}
}
DEMO
public class CompositePatternDemo {
public static void main(String[] args) {
Employee CEO = new Employee("John","CEO", 30000);
Employee headSales = new Employee("Robert","Head Sales", 20000);
Employee headMarketing = new Employee("Michel","Head Marketing", 20000);
Employee clerk1 = new Employee("Laura","Marketing", 10000);
Employee clerk2 = new Employee("Bob","Marketing", 10000);
Employee salesExecutive1 = new Employee("Richard","Sales", 10000);
Employee salesExecutive2 = new Employee("Rob","Sales", 10000);
CEO.add(headSales);
CEO.add(headMarketing);
headSales.add(salesExecutive1);
headSales.add(salesExecutive2);
headMarketing.add(clerk1);
headMarketing.add(clerk2);
//打印该组织的所有员工
System.out.println(CEO);
for (Employee headEmployee : CEO.getSubordinates()) {
System.out.println(headEmployee);
for (Employee employee : headEmployee.getSubordinates()) {
System.out.println(employee);
}
}
}
}
结果
Employee :[ Name : John, dept : CEO, salary :30000 ]
Employee :[ Name : Robert, dept : Head Sales, salary :20000 ]
Employee :[ Name : Richard, dept : Sales, salary :10000 ]
Employee :[ Name : Rob, dept : Sales, salary :10000 ]
Employee :[ Name : Michel, dept : Head Marketing, salary :20000 ]
Employee :[ Name : Laura, dept : Marketing, salary :10000 ]
Employee :[ Name : Bob, dept : Marketing, salary :10000 ]
IOS
Employee.h
#import <Foundation/Foundation.h>
@interface Employee : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *dept;
@property (nonatomic, assign) NSInteger salary;
@property (nonatomic, strong) NSMutableArray *subordinates;
-(instancetype)initWithName:(NSString*)name dept:(NSString*)dept salary:(NSInteger)salary;
-(void)add:(Employee*)e;
-(void)remove:(Employee*)e;
-(NSMutableArray*)getSubordinates;
-(void)callAll:(void(^)(Employee*))call;
@end
Employee.m
#import "Employee.h"
@implementation Employee
{
//全设置成私有的, 为了演示
@private
NSString *name_;
NSString *dept_;
NSInteger salary_;
NSMutableArray *subordinates_;
}
@synthesize name=name_, dept=dept_, salary=salary_, subordinates=subordinates_;
- (instancetype)initWithName:(NSString *)name dept:(NSString *)dept salary:(NSInteger)salary{
if(self = [self init]) {
self.name = name;
self.dept = dept;
self.salary = salary;
subordinates_ = [[NSMutableArray alloc]init];
}
return self;
}
- (void)add:(Employee *)e{
[subordinates_ addObject:e];
}
- (void)remove:(Employee *)e{
[subordinates_ removeObject:e];
}
- (NSMutableArray *)getSubordinates{
return subordinates_;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"Employee : [Name : %@, dept : %@, salary : %ld", name_, dept_, salary_];
}
-(void)callAll:(void(^)(Employee*))callEmployee{
for (Employee * emp in subordinates_) {
callEmployee(emp);
if([[emp getSubordinates] count]>0) {
[emp callAll:callEmployee];
}
}
}
@end
ViewController.h
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Employee *CEO = [[Employee alloc]initWithName:@"John" dept:@"CEO" salary:30000];
Employee *headSales = [[Employee alloc]initWithName:@"Robert" dept:@"Head Sales" salary:20000];
Employee *headMarketing = [[Employee alloc]initWithName:@"Michel" dept:@"Head Marketing" salary:20000];
Employee *clerk1 = [[Employee alloc]initWithName:@"Laura" dept:@"Marking" salary:10000];
Employee *clerk2 = [[Employee alloc]initWithName:@"Bob" dept:@"Marking" salary:10000];
Employee *salesExecutive1 = [[Employee alloc]initWithName:@"Richard" dept:@"Sales" salary:10000];
Employee *salesExecutive2 = [[Employee alloc]initWithName:@"Rob" dept:@"Sales" salary:10000];
[CEO add:headSales];
[CEO add:headMarketing];
[headSales add:salesExecutive1];
[headSales add:salesExecutive2];
[headMarketing add:clerk1];
[headMarketing add:clerk2];
//打印该组织的所有员工
NSLog(@"%@", [CEO description]);
[CEO callAll:^(Employee *employee) {
NSLog(@"%@", [employee description]);
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
结果
Employee : [Name : John, dept : CEO, salary : 30000
Employee : [Name : Robert, dept : Head Sales, salary : 20000
Employee : [Name : Richard, dept : Sales, salary : 10000
Employee : [Name : Rob, dept : Sales, salary : 10000
Employee : [Name : Michel, dept : Head Marketing, salary : 20000
Employee : [Name : Laura, dept : Marking, salary : 10000
Employee : [Name : Bob, dept : Marking, salary : 10000
不错的例子
24种设计模式——组合模式IOS设计模式之组合设计模式