组合模式
关于组合模式的定义百度百科 比我解释详细的多。百度组合模式:
http://baike.baidu.com/view/3591789.htm
这里我说一下我自己的体会:
[整体和部分都有一个公共的借口]
[整体的组成都是由部分组成,整体也有公共接口关于业务方法的实现,但是整体的实现方式一般和部分实现的方式不一样。]
[整体一般实现了对部分的管理,i.e. add & remove等,但是我认为不是一定需要]
在apache commons-io中子包 org.apache.commons.io.comparator 有一处组合模式的例子:
公共接口:AbstractFileComparator 这里准确的说是Comparator。但是在comparator包所有的类都从AbstractFileComparator继承得来。AbstractFileComparator不对外开放,准确说不是公共接口。
package org.apache.commons.io.comparator;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/*公共的接口的方法是Comparator的campare(arg1,arg2)*/
abstract class AbstractFileComparator implements Comparator<File> {
public File[] sort(File... files) {
if (files != null) {
Arrays.sort(files, this);
}
return files;
}
public List<File> sort(List<File> files) {
if (files != null) {
Collections.sort(files, this);
}
return files;
}
@Override
public String toString() {
return getClass().getSimpleName();
}
}
部分由这些组成:DefaultFileComparator,
DirectoryFileComparator,
NameFileComparator,
LastModifiedFileComparator,
ReverseComparator,
SizeFileComparator,
PathFileComparator
/*部分都实现了Comparator的compare方法只是实现的方法不一样这里只给出DefaultFileComparator的实现方式*/
/**
* Compare the two files using the {@link File#compareTo(File)} method.
*
* @param file1 The first file to compare
* @param file2 The second file to compare
* @return the result of calling file1's
* {@link File#compareTo(File)} with file2 as the parameter.
*/
public int compare(File file1, File file2) {
return file1.compareTo(file2);
}
整体部分表现在:CompositeFileComparator 整体要实现的功能是将上面的不同比较方法组合起来,实现比较复杂的比较规则。
/*整体也需要实现compare方法,这里使用了部分的compare的具体的实现*/
/**
* Compare the two files using delegate comparators.
*
* @param file1 The first file to compare
* @param file2 The second file to compare
* @return the first non-zero result returned from
* the delegate comparators or zero.
*/
public int compare(File file1, File file2) {
int result = 0;
for (Comparator<File> delegate : delegates) {
result = delegate.compare(file1, file2);
if (result != 0) {
break;
}
}
return result;
}
/*整体需要有个接口来组合部分,在这个类的构造方法中有体现*/
private static final Comparator<?>[] NO_COMPARATORS = {};
private final Comparator<File>[] delegates;
/**
* Create a composite comparator for the set of delegate comparators.
*
* @param delegates The delegate file comparators
*/
@SuppressWarnings("unchecked") // casts 1 & 2 must be OK because types are already correct
public CompositeFileComparator(Comparator<File>... delegates) {
if (delegates == null) {
this.delegates = (Comparator<File>[]) NO_COMPARATORS;//1
} else {
this.delegates = (Comparator<File>[]) new Comparator<?>[delegates.length];//2
System.arraycopy(delegates, 0, this.delegates, 0, delegates.length);
}
}
/**
* Create a composite comparator for the set of delegate comparators.
*
* @param delegates The delegate file comparators
*/
@SuppressWarnings("unchecked") // casts 1 & 2 must be OK because types are already correct
public CompositeFileComparator(Iterable<Comparator<File>> delegates) {
if (delegates == null) {
this.delegates = (Comparator<File>[]) NO_COMPARATORS; //1
} else {
List<Comparator<File>> list = new ArrayList<Comparator<File>>();
for (Comparator<File> comparator : delegates) {
list.add(comparator);
}
this.delegates = (Comparator<File>[]) list.toArray(new Comparator<?>[list.size()]); //2
}
}
这样整体不用重写每个部分的功能,但是拥有了每个部分的功能。代码的可维护强。
- 大小: 8.2 KB
分享到:
相关推荐
commons-io-2.6.jar下载
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
用开源的FileUpload组件,可以很方便的给自己的系统添加功能强大的文件上传模块。 该资源文件是两个jar打包成的压缩文件。
commons-io 是 Apache 开源基金组织提供的一组有关 IO 操作的类库,可以挺提高 IO 功能开发的效率。commons-io 工具包提供了很多有关 IO 操作的类
org.apache.commons的jar包,Apache Commons包含了很多开源的工具。 包括commons-beanutils-1.8.0-bin、commons-betwixt-0.8、commons-cli-...1.2.1-bin、commons-io-1.4-bin、commons-lang-2.4-bin、commons-net-1.4.1
Apache Commons是一个非常有用的工具包,解决各种实际的通用问题。(附件中提供了该工具包的jar包,及源文件以供研究) BeanUtils Commons-BeanUtils 提供对 Java 反射和自省API的包装 Betwixt Betwixt提供将 ...
Apache的commons-fileupload.jar可方便的实现文件的上传功能
commons-io开发jar包
org.apache.commons相关的所以jar包,包括commons-beanutils-1.8.0-bin.zip;commons-betwixt-0.8.zip;...commons-io-1.4-bin.zip;commons-lang-2.4-bin.zip;commons-logging-1.1.3-bin.zip;commons-net-1.4.1.zip
标签:apache、xmlgraphics、commons、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译...
org.apache.commons.io的jar包集合,少了啥欢迎补充
-下载后解压zip包,将commons-fileupload-1.1.1.jar,和commons-io-1.2.jar(这里我们用的是更新的版本,但是用法是一样的)复制到tomcat的webapps\你的webapp\WEB-INF\lib\下,如果目录不存在请自建目录。 新建一个...
Apache Commons IO Apache Commons IO库包含实用程序类,流实现,文件过滤器,文件比较器,字节序转换类等。 文献资料 可以在上找到更多信息。 可以浏览 。 与Apache Commons IO的使用有关的问题应张贴到。 在哪里...
Commons-IO 包括 工具类 (utility classes), 字节序列类 (endian classes), 行迭代器 (line iterator), 文件过滤器 (file filters),文件比较器 (file comparators) 和 流实现 (stream implementations).
apache commons 工具包中包括很多JAVA的实用工具。IO包主要是扩展JDK中的IO包,可用性很高。
Commons-IO 包括 工具类 (utility classes), 字节序列类 (endian classes), 行迭代器 (line iterator), 文件过滤器 (file filters),文件比较器 (file comparators) 和 流实现 (stream implementations).
标签:commons、lang、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。
开发java时,使用的一个引用包,平时dos中测试开发小程序时,可能缺少;
Apachec插件
Java借助commons-fileupload组件实现文件上传所需包 Apache Commons FileUpload 1.4 (requires Java 1.6 or later) Note: This version of Commons FileUpload also requires Commons IO 2.2.