组合模式:允许你将对象组合成树形结构来表现 “整体/部分” 层次结构。组合能让客户以一致的方式处理个别对象以及对象组合。
案例情景:上次,我和友人去超市分头购物,在结帐时,我们遍历一下我们的 “扫货” 清单(遍历打印购物清单),在实现上,Me使用ArrayList,友人使用Thing[]数组,给遍历造成困扰。
在遍历时发现赠送的商品,没有打印。现在我们有打印赠送物品的需求。
AbstractThing.java
public abstract class AbstractThing { public abstract String toString(); public void add(AbstractThing thing){}; public void remove(AbstractThing thing){}; }
Thing.java
import java.util.ArrayList; import java.util.Iterator; public class Thing extends AbstractThing{ private ArrayList<AbstractThing> list = new ArrayList<AbstractThing>(); private String name; private long price; public Thing(String name,long price){ this.name = name; this.price = price; } @Override public String toString() { String returnString = "Thing [name=" + name + ", price=" + price + "]"; for(Iterator<AbstractThing> iterator=list.iterator();iterator.hasNext();){ returnString +=iterator.next(); } return returnString; } @Override public void add(AbstractThing thing) { list.add(thing); } @Override public void remove(AbstractThing thing) { list.remove(thing); } }
ThingItem.java
public class ThingItem extends AbstractThing{ private String name; private long price; public ThingItem(String name,long price){ this.name = name; this.price = price; } @Override public String toString() { return "赠送 [name=" + name + ", price=" + price + "]"; } }
ArrayIterator.java
import java.util.Iterator; public class ArrayIterator implements Iterator<Thing>{ private Thing[] thingArray; private int position = 0; public ArrayIterator(Thing[] array){ thingArray = array; } @Override public boolean hasNext() { while(position<thingArray.length && thingArray[position] != null){ return true; } return false; } @Override public Thing next() { return thingArray[position++]; } @Override public void remove() { //noting to do } }
Me.java
import java.util.ArrayList; import java.util.Iterator; public class Me { private ArrayList<Thing> shopping = new ArrayList<Thing>(); public Me(){ shopping.add(new Thing("巧克力", 35)); shopping.add(new Thing("凉茶", 4)); shopping.add(new Thing("袜子", 9)); shopping.add(new Thing("香蕉", 12)); //买电脑送鼠标,耳机,键盘 Thing computer = new Thing("电脑", 3000); computer.add(new ThingItem("鼠标", 0)); computer.add(new ThingItem("耳机", 0)); computer.add(new ThingItem("键盘", 0)); shopping.add(computer); } public Iterator<Thing> createIterator(){ return shopping.iterator(); } }
Friends.java
import java.util.Iterator; public class Friends { private Thing[] shopping = new Thing[100]; public Friends(){ shopping[0] = new Thing("牛奶",15); shopping[1] = new Thing("苹果",10); shopping[2] = new Thing("薯片",4); //买手机送蓝牙耳机 Thing phone = new Thing("手机", 2000); ThingItem bluetoothHeadset = new ThingItem("蓝牙耳机", 0); phone.add(bluetoothHeadset); shopping[3] = phone; } public Iterator<Thing> createIterator(){ return new ArrayIterator(shopping); } }
Test.java
import java.security.AllPermission; import java.util.ArrayList; import java.util.Iterator; public class Test { public static void main(String args[]){ ArrayList<Iterator<Thing>> allIterator = new ArrayList<Iterator<Thing>>(); Me me = new Me(); Friends friends = new Friends(); Iterator<Thing> meIterator = me.createIterator(); Iterator<Thing> friendsIterator = friends.createIterator(); allIterator.add(meIterator); allIterator.add(friendsIterator); for(Iterator<Thing> iterator:allIterator){ while(iterator.hasNext()){ System.out.println(iterator.next()); } System.out.println("---------------------"); } } }
测试结果:
Thing [name=巧克力, price=35] Thing [name=凉茶, price=4] Thing [name=袜子, price=9] Thing [name=香蕉, price=12] Thing [name=电脑, price=3000]赠送 [name=鼠标, price=0]赠送 [name=耳机, price=0]赠送 [name=键盘, price=0] --------------------- Thing [name=牛奶, price=15] Thing [name=苹果, price=10] Thing [name=薯片, price=4] Thing [name=手机, price=2000]赠送 [name=蓝牙耳机, price=0] ---------------------
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)