组合模式 —composite 模式,将对角组合成树形结构以用来表示“部分与整体”的层次结构, composite 使得用户对单个对象的使用和组合对象的使用具有一致性。
Composite 模式也是一种常用的模式,因为大多数时候,对象都是由一系列复杂的对象和简单对象组合而成,而很多操作,我们则希望忽略组合对象跟简单对象的不同,将组合对象视为一个简单对象,从而能够减轻编程过程中的复杂性。
Composite 主要用在以下几个场景
1) 表示对象的整体 - 部分层次结构
2) 希望忽略组合对象与单个对象的不同。
典型的 composite 的结构如下图所示
以我们常用的去中关村组装电脑为例,来看看 composite 模式。
类图如下
看下具体实现的代码:
1)Equipment.java
package zl.study.designpattern.composite; import java.util.Iterator; /** * 基类 * @author peter * */ public abstract class Equipment { protected String name; protected Equipment(String name){ this.name = name; } public String getName(){ return this.name; } public abstract int price(); public abstract void add( Equipment e); public abstract void remove( Equipment e); public abstract Iterator<Equipment> createIterator(); }
2)CompositeEquipment.java
package zl.study.designpattern.composite; import java.util.Iterator; import java.util.LinkedList; import java.util.List; /** * 复杂设备,由几个简单设备组成 * @author peter * */ public class CompositeEquipment extends Equipment{ public int price(){ int total=0; Iterator<Equipment> iterator = createIterator(); while(iterator.hasNext()){ total += iterator.next().price(); } return total; } @Override public void add(Equipment e) { // TODO Auto-generated method stub list.add(e); } @Override public void remove(Equipment e) { list.remove(e); } @Override public Iterator<Equipment> createIterator() { return list.iterator(); } protected CompositeEquipment(String name){ super(name); this.list = new LinkedList<Equipment>(); } private List<Equipment> list; }
3)Power.java
package zl.study.designpattern.composite; import java.util.Iterator; public class Power extends Equipment{ private int price; public Power(int price){ super("power"); this.price = price; } @Override public int price() { return price; } @Override public void add(Equipment e) { // TODO Auto-generated method stub } @Override public void remove(Equipment e) { // TODO Auto-generated method stub } @Override public Iterator<Equipment> createIterator() { // TODO Auto-generated method stub return null; } }
4)Cabinet.java
package zl.study.designpattern.composite; /** * 机箱 * @author peter * */ public class Cabinet extends CompositeEquipment{ private int price; public Cabinet(int price) { super("cabinet"); this.price = price; } @Override public int price() { // TODO Auto-generated method stub return price + super.price(); } }
5)Display
package zl.study.designpattern.composite; import java.util.Iterator; public class Display extends Equipment{ private int price; public Display(int price){ super("display"); this.price = price; } @Override public int price() { return price; } @Override public void add(Equipment e) { // TODO Auto-generated method stub } @Override public void remove(Equipment e) { // TODO Auto-generated method stub } @Override public Iterator<Equipment> createIterator() { // TODO Auto-generated method stub return null; } }
6)FloppyDiskDriver.java
package zl.study.designpattern.composite; import java.util.Iterator; /** * 软盘驱动器 * @author peter * */ public class FloppyDiskDriver extends Equipment { private int price; public FloppyDiskDriver(int price){ super("FloppyDisk"); this.price = price; } @Override public int price() { return price; } @Override public void add(Equipment e) { // TODO Auto-generated method stub } @Override public Iterator<Equipment> createIterator() { // TODO Auto-generated method stub return null; } @Override public void remove(Equipment e) { // TODO Auto-generated method stub } }
7)Computer.java
package zl.study.designpattern.composite; public class Computer extends CompositeEquipment { public Computer(){ super("computer"); } }
8)测试程序
组装一台电脑,然后算出价格
package zl.study.designpattern.composite.test; import zl.study.designpattern.composite.Cabinet; import zl.study.designpattern.composite.Computer; import zl.study.designpattern.composite.Display; import zl.study.designpattern.composite.Equipment; import zl.study.designpattern.composite.FloppyDiskDriver; import zl.study.designpattern.composite.Power; public class CompositeTest { public static void main(String args[]){ Equipment computer = new Computer(); Equipment cabinet = new Cabinet(300); Equipment power = new Power(200); Equipment floppyDriver = new FloppyDiskDriver(50); cabinet.add(power); cabinet.add(floppyDriver); Equipment display = new Display(2000); /** * 电脑组装完毕(机箱,显示器) */ computer.add(cabinet); computer.add(display); //计算最终价格 int price =computer.price(); System.out.println(price); } }