composite模式 透明和安全
来源:互联网 发布:java新技术框架 编辑:程序博客网 时间:2024/05/14 18:26
// 抽象构件类、节点类abstract class Component {public String name;public Component(String name) {this.name = name;}// 公有操作public void getName() {System.out.println(this.name);}}
// 树枝构件类class Composite extends Component {private LinkedList<Component> children;public Composite(String name) {super(name);this.children = new LinkedList<Component>();}// 添加一个节点,可能是树枝、叶子public void add(Component child) {this.children.add(child);}// 删除一个节点,可能是树枝、叶子public void remove(String child) {this.children.remove(child);}// 获取子树public LinkedList<Component> getChildren() {return this.children;}}
// 树叶构件类class Leaf extends Component {public Leaf(String name) {super(name);}}
// 测试类,负责构建整棵树public class Client {public static void main(String[] args) {Composite root = new Composite("树根");Composite branch01 = new Composite("树枝01");Composite branch02 = new Composite("树枝02");root.add(branch01);root.add(branch02);Component leaf01 = new Leaf("树叶01");Component leaf02 = new Leaf("树叶02");Component leaf03 = new Leaf("树叶03");Component leaf04 = new Leaf("树叶04");Component leaf05 = new Leaf("树叶05");branch01.add(leaf01);branch01.add(leaf02);branch02.add(leaf03);branch02.add(leaf04);branch02.add(leaf05);displayTree(root);}// 递归遍历整棵树public static void displayTree(Composite root) {LinkedList<Component> children = root.getChildren();for (Component c : children) {if (c instanceof Leaf) {System.out.print("\t");c.getName();} else {c.getName();// 递归displayTree((Composite)c);}}}}
树枝01树叶01树叶02树枝02树叶03树叶04树叶05
// 树叶构件类class Leaf extends Component {public Leaf(String name) {super(name);}@Deprecated // 抛出不支持的操作异常public void add(Component child) throws UnsupportedOperationException{throws new UnsupportedOperationException();}@Deprecatedpublic void remove(String child) throws UnsupportedOperationException{throws new UnsupportedOperationException();}@Deprecatedpublic LinkedList<Component> getChildren() throws UnsupportedOperationException{throws new UnsupportedOperationException();}}
0 0
- composite模式 透明和安全
- Bridge模式 和Composite模式
- Bridge模式 和Composite模式
- Bridge模式 和Composite模式
- 安全透明的合成模式
- 用户权限设置和Composite模式
- Composite和Decorater模式联系和区别
- Composite模式
- composite 模式
- composite模式
- Composite模式
- Composite模式
- COMPOSITE 模式
- Composite模式
- Composite 模式
- Composite 模式
- Composite模式
- Composite模式
- URAL 1152 False Mirrors
- log for c
- 空格 在不同浏览器中显示距离不一致问题解决方法
- LeetCode N-Queens
- 彻底解决Operation not allowed after ResultSet closed的问题
- composite模式 透明和安全
- 用户个人界面简单UI及手势动画设计
- Qt之下拉框QComboBox相关signal对比--activated与currentIndexChanged
- Scripts:查询library cache lock和hang的脚本library_cache_locks_pins.sql
- 解决dos窗口下运行.class文件出现NoClassDefFoundError问题
- ftk学习记(combox篇)
- Scripts:查询使用UNDO最多的10个SQL hundo10.sql
- python 装饰器与AOP
- Set UID