`
ribishuangba
  • 浏览: 292851 次
文章分类
社区版块
存档分类
最新评论

J2SE 5.0 Generic应用

 
阅读更多

J2SE 5.0 Generic应用一:类型安全的functor

一、简介

函数式编程是非常常用和非常重要的一种编程范式,有的语言直接提供支持,C++则通过()运算符重载和模板提供了还算灵活的支持,而Java中的函数式编程则由于语言本身的局限没有得到广泛应用,Apache Commons Functor 项目是一个正在开发中的函数式编程库,但目前看来并不是类型安全的;J2SE 5.0提供了有限的generic能力,除了用于Collection之外,类型安全的functor也是其用武之地,已有一个开源项目Generic Algorithms for Java开始了这方面的工作

二、示例

  • 一元函数、谓词、过程

public interface UnaryFunction<R, P> {
R evaluate(P obj);
}

public interface UnaryPredicate<T> {
boolean test(T obj);
}

public interface UnaryProcedure<T> {
void run(T obj);
}

  • 二元函数、谓词、过程

public interface BinaryFunction<R, T, S> {
R evaluate(T left, S right);
}

public interface BinaryPredicate<T, S> {
boolean test(T left, S right);
}

public interface BinaryProcedure<T, S> {
void run(T left, S right);
}

  • 特化一:过滤

public interface Filter<T> extends UnaryFunction<T, T>{
}

  • 几个示例算法:transform、select、foreach

public static <Destination, Source> List<Destination> transform(Collection<Source> source, UnaryFunction<Destination, Source> transformer){
List<Destination> result = new ArrayList<Destination>();
for(Source item : source){
result.add(transformer.evaluate(item));
}
return result;
}

public static <T> List<T> select(Collection<T> source, UnaryPredicate<T> selector){
List<T> result = new ArrayList<T>();
for(T item : source){
if(selector.test(item)){
result.add(item);
}
}
return result;
}

public static <T> void foreach(Collection<T> source, UnaryProcedure<T> procedure){
for(T item : source){
procedure.run(item);
}
}

  • 几个composite:And、Or、Not

public class And<T> implements UnaryPredicate<T>, Serializable{
private UnaryPredicate<T>[] predicates;
public And(UnaryPredicate<T>... predicates){
this.predicates = predicates;
}
public boolean test(T obj) {
for(UnaryPredicate<T> predicate : predicates){
if( !predicate.test(obj) ){
return false;
}
}
return true;
}
public static <T> And<T> and(UnaryPredicate<T>... predicates){
return new And<T>(predicates);
}
}

public class Or<T> implements UnaryPredicate<T>, Serializable{
private UnaryPredicate<T>[] predicates;
public Or(UnaryPredicate<T>... predicates){
this.predicates = predicates;
}
public boolean test(T obj) {
for(UnaryPredicate<T> predicate : predicates){
if( predicate.test(obj) ){
return true;
}
}
return false;
}
public static <T> Or<T> or(UnaryPredicate<T>... predicates){
return new Or<T>(predicates);
}
}

public class Not<T> implements UnaryPredicate<T>, Serializable{
private UnaryPredicate<T> predicate;
public Not(UnaryPredicate<T> predicate){
this.predicate = predicate;
}
public boolean test(T obj) {
return !predicate.test(obj);
}
public static <T> Not<T> not(UnaryPredicate<T> predicate){
return new Not<T>(predicate);
}
}

三、问题

1,interface vs. functor

functor在Java里就是一个“只包含一个method的接口”,在使用接口的大部分地方都可以使用functor,优点是低侵入性(不需要实现接口),缺点是无法访问类的私有信息;鉴于目前Generic的实现,涉及到Java动态特性(序列化/反序列化,反射等)的模块,优先使用interface,其它场合可酌情使用泛化的functor

2,function vs. functor

只在内部使用的,做成function,在多处使用的,做成functor

3,classic vs. modern

传统分支控制语句if,else,switch在面向对象的程序中,可使用“子类化”大面积的消除

传统循环控制语句for,do while也该使用“algorithm + functor”大面积消除了

J2SE 5.0 Generic应用二:类型安全的多返回值

一、简介

out参数和ref参数提供了同一个函数返回多个值的途径,C++和C#分别用引用和out、ref关键字支持,Java中一般用数组或集合解决这个问题;Generic提供了另外一种途径,但并不比数组来得方便实用,所以这里仅仅是为了泛型而泛型,没有实际价值;另外pair<F, S>等技术也常被使用,其实就是map.entry

二、示例

  • out

public class Out<T> {
private T obj = null;
public T get() {
return obj;
}
public void set(T obj) {
this.obj = obj;
}
}

  • ref

public class Ref<T> {
private T obj = null;
public RefHolder(T obj){
this.obj = obj;
}
public T get() {
return obj;
}
public void set(T obj) {
this.obj = obj;
}
}

  • pair<first, second>

public class Pair<First, Second> {

private First first;
private Second second;

public Pair(First first, Second second) {
this.first = first;
this.second = second;
}

public First getFirst() {
return this.first;
}
public void setFirst(First first) {
this.first = first;
}

public Second getSecond() {
return this.second;
}
public void setSecond(Second second) {
this.second = second;
}

public boolean equals(Object obj) {
Pair pair = Pair.class.cast(obj);
return pair == null ? false : pair.first.equals(first) && pair.second.equals(second);
}

public int hashCode() {
return first.hashCode() * 7 + second.hashCode();
}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics