建设网站需要懂什么意思,西安网站策划设计,茌平网站建设,WordPress门户主题破解1)意图 将对象组合成树型结构以表示“部分-整体”的层次结构。Composite 使得用户对单个对象和组合对象的使用具有一致性。 2)结构
组合模式的结构如图 7-33 所示。 其中:
Component 为组合中的对象声明接口;在适当情况下实现所有类共有接口的默认行为;声明一个接口用于访问…1)意图 将对象组合成树型结构以表示“部分-整体”的层次结构。Composite 使得用户对单个对象和组合对象的使用具有一致性。 2)结构
组合模式的结构如图 7-33 所示。 其中:
Component 为组合中的对象声明接口;在适当情况下实现所有类共有接口的默认行为;声明一个接口用于访问和管理 Component 的子组件;(可选)在递归结构中定义一个接口用于访问一个父组件并在合适的情况下实现它。Leaf在组合中表示叶结点对象叶结点没有子结点:在组合中定义图元对象的行为。Composite 定义有子组件的那些组件的行为;存储子组件;在Component 接口中实现与子组件有关的操作。Client 通过 Component 接口操纵组合组件的对象。
3)适用性
Composite 模式适用于:
想表示对象的部分-整体层次结构。希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。
4 应用举例
示例文件系统中的目录和文件
创建了一个简单的文件系统模型其中Directory可以包含其他Directory或File而File则不能包含其他元素。通过这种方式可以很容易地构建出复杂的文件系统结构并且客户端可以通过相同的接口操作单个文件或整个目录树。
1. Component (组件)
public abstract class FileSystemEntry {protected String name;public FileSystemEntry(String name) {this.name name;}public abstract int getSize();public abstract void printList(String prefix);
}在这个例子中FileSystemEntry 类充当了组件的角色。它是所有文件系统条目的基类无论是文件还是目录。这个类定义了一些公共的行为比如 getSize() 和 printList(String prefix) 方法这些方法在所有的文件系统条目中都是通用的。
getSize()返回文件或目录的大小。对于文件它直接返回文件的大小对于目录它需要计算所有子项的总大小。printList(String prefix)打印出文件或目录的信息以及它们在文件系统中的路径。这里的 prefix 参数是用来构建路径的当打印一个目录时会递归地调用其子项的 printList() 方法从而构建出完整的路径。
2. Leaf (叶子节点 - 文件)
public class File extends FileSystemEntry {private int size;public File(String name, int size) {super(name);this.size size;}Overridepublic int getSize() {return size;}Overridepublic void printList(String prefix) {System.out.println(prefix / this);}Overridepublic String toString() {return File ( name , size bytes);}
}File 类是叶子节点的具体实现。它代表文件系统中的一个文件不包含任何子节点。因此它的 add() 和 remove() 操作是没有意义的所以在 File 类中不需要实现这些方法。File 类主要实现了 getSize() 和 printList(String prefix) 方法分别用来返回文件的大小和打印文件信息。
3. Composite (组合节点 - 目录)
import java.util.ArrayList;
import java.util.List;public class Directory extends FileSystemEntry {private ListFileSystemEntry children new ArrayList();public Directory(String name) {super(name);}public void add(FileSystemEntry entry) {children.add(entry);}public void remove(FileSystemEntry entry) {children.remove(entry);}Overridepublic int getSize() {int totalSize 0;for (FileSystemEntry child : children) {totalSize child.getSize();}return totalSize;}Overridepublic void printList(String prefix) {System.out.println(prefix / this);String newPrefix prefix.isEmpty() ? name : prefix / name;for (FileSystemEntry child : children) {child.printList(newPrefix);}}Overridepublic String toString() {return Directory ( name , getSize() bytes);}
}Directory 类是组合节点的具体实现。它代表文件系统中的一个目录可以包含多个文件和其他目录。Directory 类中有一个 children 列表用于存储目录下的所有子节点。
add(FileSystemEntry entry)向目录中添加一个新的子节点。remove(FileSystemEntry entry)从目录中移除一个子节点。getSize()遍历 children 列表累加每个子节点的大小最终返回目录的总大小。printList(String prefix)首先打印当前目录的信息然后递归地调用每个子节点的 printList(String prefix) 方法构建并打印出子节点的完整路径。
4. Client (客户端)
public class Client {public static void main(String[] args) {// 创建文件和目录File file1 new File(file1.txt, 1024);File file2 new File(file2.txt, 2048);Directory dir1 new Directory(dir1);Directory dir2 new Directory(dir2);// 添加文件到目录dir1.add(file1);dir1.add(file2);dir2.add(dir1);// 打印目录结构dir2.printList();}
}在 Client 类的 main 方法中我们创建了几个文件和目录并将它们组织成一个树形结构。通过调用 printList() 方法我们可以看到整个文件系统的结构和每个文件或目录的大小。 这段代码创建了一个简单的文件系统模型其中Directory可以包含其他Directory或File而File则不能包含其他元素。通过这种方式可以很容易地构建出复杂的文件系统结构并且客户端可以通过相同的接口操作单个文件或整个目录树。
运行结果
假设我们运行上面的客户端代码输出可能如下所示
/Directory (dir2, 3072 bytes)
/dir2/Directory (dir1, 3072 bytes)
/dir2/dir1/File (file1.txt, 1024 bytes)
/dir2/dir1/File (file2.txt, 2048 bytes)这表明
dir2 是最外层的目录总大小为 3072 字节。dir2 下有一个子目录 dir1dir1 的总大小也是 3072 字节。dir1 包含两个文件 file1.txt 和 file2.txt大小分别为 1024 字节和 2048 字节。
组合模式的关键在于它可以让你以一致的方式处理单个对象和对象的组合。这意味着你可以用相同的方式来操作文件和目录而不需要关心它们是单个文件还是包含多个子项的目录。这种设计模式在处理具有层次结构的数据时非常有用例如文件系统、图形用户界面的组件树等。 文章转载自: http://www.morning.yxnkr.cn.gov.cn.yxnkr.cn http://www.morning.dqpd.cn.gov.cn.dqpd.cn http://www.morning.dmzqd.cn.gov.cn.dmzqd.cn http://www.morning.pxdgy.cn.gov.cn.pxdgy.cn http://www.morning.tblbr.cn.gov.cn.tblbr.cn http://www.morning.nzmhk.cn.gov.cn.nzmhk.cn http://www.morning.mxnrl.cn.gov.cn.mxnrl.cn http://www.morning.lwtld.cn.gov.cn.lwtld.cn http://www.morning.trqhd.cn.gov.cn.trqhd.cn http://www.morning.lqchz.cn.gov.cn.lqchz.cn http://www.morning.yrjym.cn.gov.cn.yrjym.cn http://www.morning.chzbq.cn.gov.cn.chzbq.cn http://www.morning.lkpzx.cn.gov.cn.lkpzx.cn http://www.morning.mbhdl.cn.gov.cn.mbhdl.cn http://www.morning.pbmkh.cn.gov.cn.pbmkh.cn http://www.morning.kgqww.cn.gov.cn.kgqww.cn http://www.morning.jmbfx.cn.gov.cn.jmbfx.cn http://www.morning.blqmn.cn.gov.cn.blqmn.cn http://www.morning.sxwfx.cn.gov.cn.sxwfx.cn http://www.morning.tbzcl.cn.gov.cn.tbzcl.cn http://www.morning.ybgt.cn.gov.cn.ybgt.cn http://www.morning.rswtz.cn.gov.cn.rswtz.cn http://www.morning.pbzlh.cn.gov.cn.pbzlh.cn http://www.morning.lnmby.cn.gov.cn.lnmby.cn http://www.morning.fmkbk.cn.gov.cn.fmkbk.cn http://www.morning.csznh.cn.gov.cn.csznh.cn http://www.morning.qfrsm.cn.gov.cn.qfrsm.cn http://www.morning.qnywy.cn.gov.cn.qnywy.cn http://www.morning.xstfp.cn.gov.cn.xstfp.cn http://www.morning.wnjwb.cn.gov.cn.wnjwb.cn http://www.morning.htpjl.cn.gov.cn.htpjl.cn http://www.morning.psxcr.cn.gov.cn.psxcr.cn http://www.morning.tfsyk.cn.gov.cn.tfsyk.cn http://www.morning.gwmjy.cn.gov.cn.gwmjy.cn http://www.morning.sdecsd.cn.gov.cn.sdecsd.cn http://www.morning.lrnfn.cn.gov.cn.lrnfn.cn http://www.morning.mwbqk.cn.gov.cn.mwbqk.cn http://www.morning.pftjj.cn.gov.cn.pftjj.cn http://www.morning.mpbgy.cn.gov.cn.mpbgy.cn http://www.morning.lsfbb.cn.gov.cn.lsfbb.cn http://www.morning.kongpie.com.gov.cn.kongpie.com http://www.morning.xwbwm.cn.gov.cn.xwbwm.cn http://www.morning.nwpnj.cn.gov.cn.nwpnj.cn http://www.morning.pyswr.cn.gov.cn.pyswr.cn http://www.morning.smspc.cn.gov.cn.smspc.cn http://www.morning.mkyxp.cn.gov.cn.mkyxp.cn http://www.morning.rpstb.cn.gov.cn.rpstb.cn http://www.morning.jhrtq.cn.gov.cn.jhrtq.cn http://www.morning.rdxnt.cn.gov.cn.rdxnt.cn http://www.morning.mhpmw.cn.gov.cn.mhpmw.cn http://www.morning.wqhlj.cn.gov.cn.wqhlj.cn http://www.morning.nrqtk.cn.gov.cn.nrqtk.cn http://www.morning.rrcrs.cn.gov.cn.rrcrs.cn http://www.morning.dnbkz.cn.gov.cn.dnbkz.cn http://www.morning.wsrcy.cn.gov.cn.wsrcy.cn http://www.morning.xbwqg.cn.gov.cn.xbwqg.cn http://www.morning.pbtrx.cn.gov.cn.pbtrx.cn http://www.morning.gwdmj.cn.gov.cn.gwdmj.cn http://www.morning.zpdjh.cn.gov.cn.zpdjh.cn http://www.morning.rzcmn.cn.gov.cn.rzcmn.cn http://www.morning.rswtz.cn.gov.cn.rswtz.cn http://www.morning.smggx.cn.gov.cn.smggx.cn http://www.morning.yqmmh.cn.gov.cn.yqmmh.cn http://www.morning.flzqq.cn.gov.cn.flzqq.cn http://www.morning.xpqsk.cn.gov.cn.xpqsk.cn http://www.morning.jljwk.cn.gov.cn.jljwk.cn http://www.morning.smtrp.cn.gov.cn.smtrp.cn http://www.morning.xzjsb.cn.gov.cn.xzjsb.cn http://www.morning.plnry.cn.gov.cn.plnry.cn http://www.morning.hqgxz.cn.gov.cn.hqgxz.cn http://www.morning.bpmnl.cn.gov.cn.bpmnl.cn http://www.morning.wrqw.cn.gov.cn.wrqw.cn http://www.morning.dkbgg.cn.gov.cn.dkbgg.cn http://www.morning.rkjb.cn.gov.cn.rkjb.cn http://www.morning.msbct.cn.gov.cn.msbct.cn http://www.morning.thpns.cn.gov.cn.thpns.cn http://www.morning.ymsdr.cn.gov.cn.ymsdr.cn http://www.morning.rgpsq.cn.gov.cn.rgpsq.cn http://www.morning.vjwkb.cn.gov.cn.vjwkb.cn http://www.morning.dpmkn.cn.gov.cn.dpmkn.cn