HeadFirst设计模式_读书笔记_009_ 组合模式
2017-04-11 10:24
204 查看
组合模式:允许将对象组合成树形结构来表现“整体/部分”的结构,让用户可以用一致的方式处理个别对象以及组合对象。
public abstract class Node {
protected String name;
protected String desc;
public Node(String desc, String name)
{
this.name = name;
this.desc = desc;
}
public void addChild(Node node)
{
throw new UnsupportedOperationException();
}
public void removeChild(Node node)
{
throw new UnsupportedOperationException();
}
public Node getChild(int index)
{
throw new UnsupportedOperationException();
}
public Iterator createIterator()
{
throw new UnsupportedOperationException();
}
public void print()
{
}
} public class Leaf extends Node {
public Leaf(String desc, String name) {
super(desc, name);
}
public void print()
{
System.out.print("-name:" + name + "-desc:" + desc);
System.out.println();
}
} public class NodeItem extends Node {
public NodeItem(String desc, String name) {
super(desc, name);
this.childs = new ArrayList<Node>();
}
private List<Node> childs;
public void addChild(Node node)
{
this.childs.add(node);
}
public void removeChild(Node node)
{
this.childs.remove(node);
}
public Node getChild(int index)
{
return childs.get(index);
}
public Iterator createIterator()
{
return this.childs.iterator();
}
public void print()
{
System.out.println("-name:" + name + "-desc:" + desc);
for(Node child: childs)
{
child.print();
}
}
}
大小: 4.5 KB
查看图片附件
public abstract class Node {
protected String name;
protected String desc;
public Node(String desc, String name)
{
this.name = name;
this.desc = desc;
}
public void addChild(Node node)
{
throw new UnsupportedOperationException();
}
public void removeChild(Node node)
{
throw new UnsupportedOperationException();
}
public Node getChild(int index)
{
throw new UnsupportedOperationException();
}
public Iterator createIterator()
{
throw new UnsupportedOperationException();
}
public void print()
{
}
} public class Leaf extends Node {
public Leaf(String desc, String name) {
super(desc, name);
}
public void print()
{
System.out.print("-name:" + name + "-desc:" + desc);
System.out.println();
}
} public class NodeItem extends Node {
public NodeItem(String desc, String name) {
super(desc, name);
this.childs = new ArrayList<Node>();
}
private List<Node> childs;
public void addChild(Node node)
{
this.childs.add(node);
}
public void removeChild(Node node)
{
this.childs.remove(node);
}
public Node getChild(int index)
{
return childs.get(index);
}
public Iterator createIterator()
{
return this.childs.iterator();
}
public void print()
{
System.out.println("-name:" + name + "-desc:" + desc);
for(Node child: childs)
{
child.print();
}
}
}
大小: 4.5 KB
查看图片附件
相关文章推荐
- 《HeadFirst设计模式》读书笔记-第7章-外观模式
- Delphi 设计模式:《HeadFirst设计模式》Delphi2007代码---组合模式之Menus
- 《HeadFirst设计模式》读书笔记-第8章-模板方法模式
- 【读书笔记】HeadFirst设计模式——命令模式简述
- HeadFirst设计模式_读书笔记_001_策略模式
- HeadFirst设计模式_读书笔记_010_状态模式
- 《Android源码设计模式》读书笔记 (19) 第19章 组合模式
- HeadFirst设计模式_读书笔记_002_观察者模式
- 《HeadFirst设计模式》读书笔记-第6章-命令模式
- 代理模式、装饰模式、适配器模式、组合模式、桥梁模式、外观模式、享元模式【读书笔记】设计模式4章:结构型模式
- 【读书笔记】HeadFirst设计模式——单件不简单:详述实现Singleton模式需要考虑的方方面面
- HeadFirst设计模式学习之组合模式
- HeadFirst设计模式_读书笔记_003_装饰者模式
- 《Android源码设计模式解析与实战》读书笔记(十九)——组合模式
- HeadFirst设计模式_读书笔记_004_单例模式
- HeadFirst设计模式(十一) - 组合模式
- 读书笔记16:组合模式
- HeadFirst设计模式_读书笔记_005_命令模式
- 读书笔记16:组合模式