设计模式-组合模式
来源:互联网 发布:zip解压软件 编辑:程序博客网 时间:2024/05/27 19:27
组合模式的定义为:将对象组合成树形结构以表示整体和部分的层次结构。是构造型的设计模式。
组合模式的主要构成元素:(1)Component类:组合中的对象声明接口,在适当情况下,实现所有类共有接口的行为。用于访问和管理Component的子部件。(2)Leaf类:叶节点对象,叶节点没有子节点。不能增加分支和树叶,其行为用来存储叶节点集合。(3)Composite类:实现Component接口的相关操作,比如Add和Remove操作。(4)用来存储叶节点的集合。组合模式的角色:(1)抽象构件(Component)角色:给参加组合的对象规定一个接口,这个角色给出共有的接口和其默认行为。(2)树叶构件(Leaf)角色:代表参加组合的树叶对象。没有下级子对象,定义出参加组合的原始对象的行为。(3)树枝构件(Composite)角色:代表参加组合的树枝对象,可以有下级子对象,并给出树枝构件对象的行为。组合模式又有两种形式:透明方式,安全方式(1)透明方式:即统一的在Component接口中声明所有的用来管理子类对象的方法,这样所有构件类都有相同的接口,客户端可以同等的对待所有对象。当然缺点就是不够安全,叶节点没有子对象,管理子类对象的方法对其毫无意义,甚至容易出错。(2)安全方式:即在树枝构件中定义所有的管理子类的方法,这样树叶构件与其他某些组合构件将具有不同的接口。实用例子:实现文件系统的文件目录结构
public interface RootComponent{ public boolean addFile(RootComponent file); public boolean removeFile(RootComponent file); public List<RootComponent> getFile(); public void display(); } public class FileLeaf implements RootComponent{ private String name; public FileLeaf(String name){ this.name = name; } @Override public boolean addFile(RootComponent file){ return false; } @Override public boolean removeFile(RootComponent file){ return false; } @Override public List<RootComponent> getFile(){ return null; } @Override public void display(){ System.out.println(" | "+name); } } public class FolderComposite implements RootComponent{ String name; List<RootComponent> folder; public FolderComposite(String name){ this.name = name; this.folder = new ArrayList<RootComponent>(); } @Override public boolean addFile(RootComponent file){ return folder.add(file); } @Override public boolean removeFile(RootComponent file){ return folder.remove(file); } @Override public List<RootComponent> getFile(){ return folder; } @Override public void display(){ System.out.println(name); for(RootComponent file:folder){ if(file instanceof FolderComposite){ System.out.print("| "); } file.display(); } } } public class Client{ public static void main(String[] args){ RootComponent root1 = new FolderComposite("c://"); RootComponent root2 = new FolderComposite("d://"); RootComponent win = new FolderComposite("windows"); RootComponent sys = new FolderComposite("Systems"); RootComponent hello = new FileLeaf("helloworld.java"); root1.addFile(win); root1.addFile(sys); win.addFile(hello); root1.display(); root2.display(); } }
输出结果c://| windows | helloworld.java| Systemsd://Process finished with exit code 0组合模式的优点:容易增加新的构件组合模式的缺点:很难用继承的方法增加新的行为。
0 0
- 设计模式:组合模式
- 设计模式-----组合模式
- 设计模式 组合模式
- 设计模式--组合模式
- 设计模式---组合模式
- 【设计模式】组合模式
- 设计模式 - 组合模式
- 设计模式----组合模式
- 设计模式 - 组合模式
- 设计模式:组合模式
- 设计模式 - 组合模式
- 设计模式-组合模式
- 设计模式 - 组合模式
- 【设计模式】组合模式
- 设计模式--组合模式
- 设计模式-组合模式
- 设计模式:组合模式
- 设计模式-----组合模式
- #宝哥教你#查看jquery绑定的事件函数
- Qt、Qt Create、MinGW
- SPOJ375——Query on a tree(树链剖分模板详解以及入门)
- JS事件函数不能传参数的解决办法。
- POJ1338 Ugly Numbers
- 设计模式-组合模式
- MySQL的用户变量和系统变量
- 模板 欧拉筛法 分解质因数
- Android常用开源框架
- hive报错java.io.IOException: Could not find status of job:job_1470047186803_131111
- Android Audio System 之一:AudioTrack如何与AudioFlinger交换音频数据
- Android之drawable state各个属性详解
- BestCoder Round #85 1002 domino
- 勾股定理一日一证连载62