组合模式的简单应用
2012-09-02 11:49
225 查看
public abstract class Component { protected String name; protected String type; protected void setType(String type) { this.type=type; } protected final String getType() { return type; } protected void setName(String name) { this.name=name; } protected final String getName() { return name; } public abstract void print(); } import java.util.ArrayList; import java.util.Iterator; public class Composite extends Component { protected ArrayList<Component>list=new ArrayList<Component>(); public Composite() { } protected void add(Component c) { this.list.add(c); } protected void remove(Component c) { if(this.list.isEmpty()==false) this.list.remove(c); } protected void remove(int index) { if(index <0 || index>=this.getSize()) { System.out.println("out of bound!"); return; } else this.list.remove(index); } protected final Component getComponent(int index) { if(index <0 || index>=this.getSize()) return null; else return this.list.get(index); } protected final int getSize() { return this.list.size(); } @Override public void print() { // TODO Auto-generated method stub System.out.println("I am: "+this.getType()); System.out.println("My name is: "+this.getName()); Iterator<Component>iterator=list.iterator(); while(iterator.hasNext()) { iterator.next().print(); System.out.println("I belong to: "+this.getName()); } } } public class Leaf extends Component { public Leaf(String name) { this.setName(name); this.setType("leaf"); } @Override public void print() { // TODO Auto-generated method stub System.out.println("I am: "+this.getType()); System.out.println("My name : "+this.getName()); } } public class Branches extends Composite { public Branches(String name) { super(); this.setName(name); this.setType("Branches"); } public Branches() { } } public class Tree extends Branches { public Tree(String name) { super(); // TODO Auto-generated constructor stub this.setName(name); this.setType("Tree"); } } public class Client { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Leaf leaf1=new Leaf("leaf1"); Leaf leaf2=new Leaf("leaf2"); Leaf leaf3=new Leaf("leaf3"); Leaf leaf4=new Leaf("leaf4"); Branches branches1=new Branches("branches1"); Branches branches2=new Branches("branches2"); branches1.add(leaf1); branches1.add(leaf2); branches2.add(leaf3); branches2.add(leaf4); Tree tree=new Tree("Binary Tree"); tree.add(branches1); tree.add(branches2); tree.print(); } }
测试:
I am: Tree My name is: Binary Tree I am: Branches My name is: branches1 I am: leaf My name : leaf1 I belong to: branches1 I am: leaf My name : leaf2 I belong to: branches1 I belong to: Binary Tree I am: Branches My name is: branches2 I am: leaf My name : leaf3 I belong to: branches2 I am: leaf My name : leaf4 I belong to: branches2 I belong to: Binary Tree
相关文章推荐
- 设计模式实战应用之四:简单工厂模式
- JAVA接口的应用及工厂模式的简单示例
- Silverlight实用窍门系列:33.OOB模式下WebBrowser的简单应用--模拟浏览器和百度地图【附带源码实例】
- 设计模式--简单工厂应用 java
- WPF---MVVM模式简单应用
- 模版方法设计模式简单应用
- 设计模式实战应用之四:简单工厂模式
- 【机房重构】模板方法模式应用—组合查询
- WebSphere MQ Java 应用开发简单实例(上篇:客户端模式开发)(client mode和binding mode)
- Strategry模式简单理解及应用
- 【设计模式】工厂模式结合反射技术的简单应用
- iOS疯狂讲解之单例模式的简单应用
- 外观模式-facade实现interface的方式(简单工厂+facade组合使用)
- MSDN 体系结构为Windows应用创建简单的异步调用模式(文摘)
- 为Windows应用创建简单的异步调用模式
- java Script 组合模式 简单实例
- 为Windows应用创建简单的异步调用模式
- rabbitmq简单应用-RPC模式
- 简单的Android中MVP模式的应用
- 装饰者模式的简单应用