《JAVA与模式》第8天―合成模式 (五)

2014-11-24 08:26:42 · 作者: · 浏览: 4
String[] args) {
Component root = new Composite("服装");
Component c1 = new Composite("男装");
Component c2 = new Composite("女装");

Component leaf1 = new Leaf("衬衫");
Component leaf2 = new Leaf("夹克");
Component leaf3 = new Leaf("裙子");
Component leaf4 = new Leaf("套装");

root.addChild(c1);
root.addChild(c2);
c1.addChild(leaf1);
c1.addChild(leaf2);
c2.addChild(leaf3);
c2.addChild(leaf4);

root.printStruct("");
}
}
package com.bankht.Composite.transparent;

/**
* @author: 特种兵—AK47
* @创建时间:2012-6-25 下午10:12:22
*
* @类说明 :客户端类的主要变化是不再区分Composite对象和Leaf对象。
*/
public class Client {
public static void main(String[] args) {
Component root = new Composite("服装");
Component c1 = new Composite("男装");
Component c2 = new Composite("女装");

Component leaf1 = new Leaf("衬衫");
Component leaf2 = new Leaf("夹克");
Component leaf3 = new Leaf("裙子");
Component leaf4 = new Leaf("套装");

root.addChild(c1);
root.addChild(c2);
c1.addChild(leaf1);
c1.addChild(leaf2);
c2.addChild(leaf3);
c2.addChild(leaf4);

root.printStruct("");
}
}

作者:m13666368773