其协作过程是:用户使用Compoment类接口与组合结构中的对象进行交互。如果接受者是一个叶节点,则直接处理请求。如果接受者是Composite,它通常将请求发送给他的字部件,在转发请求之前或者之后可能执行一些辅助操作。
在本例子中,没有什么特殊之处,所不同的是Composite可以容纳Component的子类对象,包括本身。例子本省也比较简单,平淡无味,单却可以在特定的场景能很好的解决特定的问题。
使用组合模式有如下的好处:
l 定义了饱含基本对象和组合对象的类层次结构,基本对象可以被组合成更复杂的组合对象,而这个组合对象有可以被组合。
l 简化客户代码 客户可以一直地使用组合结构和单个对象,通常用户不知道处理的是一个叶节点还是一个组合组件。
l 使得更容易增加新类型的组件, 新定义的Composite或leaf子类自动地与义有的结构和客户代码一起工作,客户程序不需要因为新的Component类而改变。
l 使你的设计变得更一般化。
实现的代码:
Component接口:
package composite;
public interface Component{
public int getSize();
public int getChildNum();
public String getType();
public String getName();
public void add(Component c);
public void remove(Component c);
}
Leaf类:
package composite;
public class Leaf implements Component{
private int size;
private String name;
public Leaf(String aName,int aSize){
size = aSize;
name = aName;
}
public String getName(){
return name;
}
public String getType(){
return "Leaf";
}
public int getSize(){
return size;
}
public int getChildNum(){
return 1;
}
public void add(Component c){
System.err.println("Not supported method!");
}
public void remove(Component c){
System.err.println("Not supported method!");
}
}
Composite类:
package composite;
public class Leaf implements Component{
private int size;
private String name;
public Leaf(String aName,int aSize){
size = aSize;
name = aName;
}
public String getName(){
return name;
}
public String getType(){
return "Leaf";
}
public int getSize(){
return size;
}
public int getChildNum(){
return 1;
}
public void add(Component c){
System.err.println("Not supported method!");
}
public void remove(Component c){
System.err.println("Not supported method!");
}
}
客户端代码:
package composite;
public class Client{
public static void main(String[] args){
Component root = new Composite("root");
Component composite = new Composite("composite");
Component leaf1 = new Leaf("leaf1",20);
Component leaf2 = new Leaf("leaf2",40);
root.add(leaf1);
composite.add(leaf2);
root.add(composite);
String str = "Leaf1's size is "+leaf1.getSize();
str += "\nleaf2's size is " + leaf2.getSize();
str += "\nComposite's size is " + composite.getSize();
str += "\nRoot's size is " + root.getSize();
System.out.println(str);
}
}
总结:组合模式在 解决整体与部分 的问题中 应用比较广泛,一个组合也可以被看作 部分来处理,降低了处理问题的难度
相关推荐
15、组合模式COMPOSITE PATTERN 16、观察者模式OBSERVER PATTERN 17、责任链模式 18、访问者模式VISITOR PATTERN 19、状态模式 20、原型模式 21、中介者模式 22、解释器模式 23、亨元模式 24、备忘录模式
Android 设计模式系列还包括工厂方法模式、抽象工厂模式、创建者模式、原型模式、单例模式、适配器模式、桥模式、组合模式、装饰模式、外观模式、享元模式、代理模式、解释器模式、模板方法模式、职责链模式、命令...
设计模式之Composite(组合) 设计模式之Decorator(油漆工) 设计模式之Bridge 设计模式之Flyweight(享元) 行为模式: 设计模式之Template 设计模式之Memento(备忘机制) 设计模式之Observer 设计模式之Chain of ...
设计模式Golang实现《研磨设计模式》读书笔记Go语言设计模式Go语言设计...组合模式(Composite)享元模式(Flyweight)装饰模式(Decorator)桥模式(Bridge)行为模式中介者模式(Mediator)观察者模式(Observer)...
Composite ( 组合模式 ) Decorator ( 装饰模式 ) Facade ( 外观模式 ) Flyweight ( 享元模式 ) Proxy ( 代理模式 ) Chain of Responsibility ( 责任链模式 ) Command ( 命令模式 ) Interpreter ( 解释器模式 ...
组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. 享元模式(Flyweight Pattern) 12. 代理模式(Proxy Pattern) 行为型: 13. 模板方法(Template Method) 14. 命令模式(Command Pattern) 15. 迭代器...
其中包括代理模式(Proxy)、装饰器模式(Decorator)、适配器模式(Adapter)、桥接模式(Bridge)、组合模式(Composite)、外观模式(Facade)和享元模式(Flyweight)。这些模式帮助我们在不修改原有代码的情况...
组合模式(Composite) 装饰者模式(Decorator) 外观模式(Facade) 蝇量模式(Flyweight) 代理模式(Proxy) 行为型: 责任链模式(Chain of Responsibility) 命令模式(Command) 解释器模式(Interpreter) 迭代器模式...
设计模式之Composite(组合) 设计模式之Decorator(油漆工) 设计模式之Bridge 设计模式之Flyweight(享元) 行为模式: 设计模式之Template 设计模式之Memento(备忘机制) 设计模式之Observer 设计模式之Chain of ...
组合模式(Composite Pattern) 装饰器模式(Decorator Pattern) 外观模式(Facade Pattern) 享元模式(Flyweight Pattern) 代理模式(Proxy Pattern) 3 行为型模式 这些设计模式特别关注对象之间的通信。 责任...
组合模式(composite) 装饰器模式(decorate) 外观模式(facecade) 享元模式(flyweight) 代理模式(proxy) 行为型模式(behaviour) 责任链模式(chainrespon) 命令模式(commond) 解释器模式(interpreter) 迭代器模式...
组合模式(Composite Pattern) 装饰器模式(Decorator Pattern) 外观模式(Facade Pattern) 享元模式(Flyweight Pattern) 代理模式(Proxy Pattern) 3. 行为型模式 责任链模式(Chain of Responsibility...
- 组合模式(Composite) - 外观模式(Facade) - 享元模式(Flyweight) - 观察者模式(Observer) - 模板方法模式(Template Method) - 策略模式(Strategy) - 责任链模式(Chain of Responsibility) - 中介者模式...
组合模式(Composite Pattern) 装饰者模式(Decorator Pattern) 外观模式(Facade Pattern) 享元模式(Flyweight Pattern) 代理模式(Proxy Pattern) 行为型模式用来对类或对象怎样交互和怎样...
组合模式( Composite ) 享元模式( Flyweight ) 行为型模式包含了: 策略模式( Strategy ) 模板方法模式( Template Method ) 观察者模式( Observer ) 迭代子模式( Iterator ) 责任链模式( Chain of ...
备忘录模式(Memento Pattern) 策略模式(Strategy Pattern) 抽象工厂模式(Abstract Factory Pattern) 代理模式(Proxy Pattern) ...装饰模式(Decorator Pattern) 状态模式(State Pattern) 组合模式(Composite Pattern)
组合模式( Composite ) 享元模式( Flyweight ) 行为型模式包含了: 策略模式( Strategy ) 模板方法模式( Template Method ) 观察者模式( Observer ) 迭代子模式( Iterator ) 责任链模式( Chain of ...
组合模式( Composite ) 享元模式( Flyweight ) 行为型模式包含了: 策略模式( Strategy ) 模板方法模式( Template Method ) 观察者模式( Observer ) 迭代子模式( Iterator ) 责任链模式( Chain of ...
组合模式( Composite ) 享元模式( Flyweight ) 行为型模式包含了: 策略模式( Strategy ) 模板方法模式( Template Method ) 观察者模式( Observer ) 迭代子模式( Iterator ) 责任链模式( Chain of ...
C#面向对象设计模式纵横谈(9):Composite 组合模式(结构型模式) C#面向对象设计模式纵横谈(10):Decorator 装饰模式(结构型模式) C#面向对象设计模式纵横谈(11):Facade 外观模式(结构型模式) C#面向对象设计...