Java设计模式泛型化之组合模式
来源:互联网 发布:月季 淘宝推荐 编辑:程序博客网 时间:2024/05/21 11:20
组合模式通常用来描述一个类似树状的结构。
一个“根”节点几个“枝”节点几个“叶”节点
其代码结构如下:
抽象组件层(视为任何一种类型节点的抽象)
public abstract class Component { private String name = ""; private int position = 0; private String content = ""; private Component parent = null; public Component(String name, int position, String content) { this.name = name; this.position = position; this.content = content; } public void print() { System.out.println("------------------"); System.out.println("Name: " + this.name + "\nPosition: " + this.position + "\nContent: " + this.content); } public Component getParent() { return this.parent; } protected void setParent(Component parent) { this.parent = parent; }}
”根“结构
import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class CompositeRoot extends Component { private String name = ""; private int position = 0; private String content = ""; private List<Component> subordinaryList = new ArrayList<Component>(); private Component root = null; public CompositeRoot(String name, int position, String content) { super(name, position, content); this.name = name; this.position = position; this.content = content; } protected void setRoot(Component root) { this.root = root; } public Component getRoot() { return this.root; } public List<Component> getSubordinary() { return this.subordinaryList; } public void add(Component component) { component.setParent(this); subordinaryList.add(component); } @Override public void print() { System.out.println("--------------"); System.out.println("Name: " + this.name + "\nPosition: " + this.position + "\nContent: " + this.content + " (Root)"); Iterator<Component> it = subordinaryList.iterator(); while (it.hasNext()) { Component comp = it.next(); comp.print(); } } }
”树枝“结构
import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class CompositeBranch extends Component { private String name = ""; private int position = 0; private String content = ""; private List<Component> subordinaryList = new ArrayList<Component>(); public CompositeBranch(String name, int position, String content) { super(name, position, content); this.name = name; this.position = position; this.content = content; } public List<Component> getSubordinary() { return this.subordinaryList; } public void add(Component component) { component.setParent(this); subordinaryList.add(component); } @Override public void print() { System.out.println("------------------"); System.out.println("Name: " + this.name + "\nPosition: " + this.position + "\nContent: " + this.content + " (Branch)"); Iterator<Component> it = subordinaryList.iterator(); while (it.hasNext()) { Component comp = it.next(); comp.print(); } }}
”叶子“结构
public class CompositeLeaf extends Component { private String name = ""; private int position = 0; private String content = ""; public CompositeLeaf(String name, int position, String content) { super(name, position, content); this.name = name; this.position = position; this.content = content; } @Override public void print() { System.out.println("--------------"); System.out.println("Name: " + this.name + "\nPosition: " + this.position + "\nContent: " + this.content + " (Leaf)"); }}
统一调用类
public class Invoker { private Component node; public Invoker(Component node) { this.node = node; } public void printNode() { node.print(); }}
调用者
public class CompositeCaller { public static void main(String[] args) { CompositeRoot root = new CompositeRoot("Animal", 0, "Animal"); CompositeBranch cat = new CompositeBranch("Cat", 1, "Cat"); CompositeBranch dog = new CompositeBranch("Dog", 1, "Dog"); CompositeBranch duck = new CompositeBranch("Duck", 1, "Duck"); CompositeLeaf peikingDuck = new CompositeLeaf("PeikingDuck", 2, "PeikingDuck"); CompositeLeaf blackDuck = new CompositeLeaf("BlackDuck", 2, "BlackDuck"); root.add(cat); root.add(dog); root.add(duck); duck.add(peikingDuck); duck.add(blackDuck); Invoker invoker = new Invoker(root); invoker.printNode(); }}
那么,如何泛型化呢?
其实,在这个结构中,重要的部分就是”树枝“结构或者”根“结构。因为,只有这两个部分才是对整个”树“的结构进行维护的。其中,”树枝“结构可以包含”根“结构。
以下是泛型化代码:
import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class CompositeRoot<T extends Component> extends Component { private String name = ""; private int position = 0; private String content = ""; private List<T> subordinaryList = new ArrayList<T>(); private Component root = null; public CompositeRoot(String name, int position, String content) { super(name, position, content); this.name = name; this.position = position; this.content = content; } protected void setRoot(T root) { this.root = root; } public Component getRoot() { return this.root; } public List<T> getSubordinary() { return this.subordinaryList; } public void add(T component) { component.setParent(this); subordinaryList.add(component); } @Override public void print() { System.out.println("--------------"); System.out.println("Name: " + this.name + "\nPosition: " + this.position + "\nContent: " + this.content + " (Root)"); Iterator<T> it = subordinaryList.iterator(); while (it.hasNext()) { Component comp = it.next(); comp.print(); } } }
import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class CompositeBranch<T extends Component> extends Component { private String name = ""; private int position = 0; private String content = ""; private List<T> subordinaryList = new ArrayList<T>(); public CompositeBranch(String name, int position, String content) { super(name, position, content); this.name = name; this.position = position; this.content = content; } public List<T> getSubordinary() { return this.subordinaryList; } public void add(T component) { component.setParent(this); subordinaryList.add(component); } @Override public void print() { System.out.println("------------------"); System.out.println("Name: " + this.name + "\nPosition: " + this.position + "\nContent: " + this.content + " (Branch)"); Iterator<T> it = subordinaryList.iterator(); while (it.hasNext()) { Component comp = it.next(); comp.print(); } }}
public class CompositeCaller { public static void main(String[] args) { CompositeRoot<Component> root = new CompositeRoot<Component>("Animal", 0, "Animal"); CompositeBranch<Component> cat = new CompositeBranch<Component>("Cat", 1, "Cat"); CompositeBranch<Component> dog = new CompositeBranch<Component>("Dog", 1, "Dog"); CompositeBranch<Component> duck = new CompositeBranch<Component>("Duck", 1, "Duck"); CompositeLeaf peikingDuck = new CompositeLeaf("PeikingDuck", 2, "PeikingDuck"); CompositeLeaf blackDuck = new CompositeLeaf("BlackDuck", 2, "BlackDuck"); root.add(cat); root.add(dog); root.add(duck); duck.add(peikingDuck); duck.add(blackDuck); Invoker invoker = new Invoker(root); invoker.printNode(); }}
0 0
- Java设计模式泛型化之组合模式
- java设计模式之组合设计模式
- java设计模式之组合模式
- java设计模式之组合模式Composite
- JAVA设计模式之组合模式
- java设计模式之组合模式
- 设计模式之组合模式java实现
- java设计模式之组合模式
- 【Java设计模式】之组合模式
- Java设计模式之组合模式
- java设计模式之组合模式
- JAVA设计模式初探之组合模式
- JAVA设计模式初探之组合模式
- java设计模式之组合模式
- java设计模式之组合模式
- JAVA设计模式初探之组合模式
- java设计模式之组合模式篇
- Java设计模式之组合模式
- 线段树的创建插入查找删除
- PE文件跟病毒有关吗
- springMVC的注解
- statusbar—下拉栏时上栏是否显示
- spring中bean的作用域属性single与prototype的区别
- Java设计模式泛型化之组合模式
- 获取某月份第N周结束日期(即星期日)
- parallel studio XE 2013 with VS2010 自带ipp库的配置
- java 学习写架构必会几大技术点
- 无条件前转
- SDN 下 高效率流量传输
- Python学习之函数
- linux 查看cpu的信息
- PHP_EOL