Generator设计模式

Generator设计模式其实只是著名的Factory Method设计模式在《Thinking in Java》中的一个变种,区别在于Factory Method在生成新实例时通常要参数,而Generator不输入参数。

基本形式:

//: net/mindview/util/Generator.java

// A generic interface.

package net.mindview.util;

public interface Generator<T> { T next(); } ///:~

以咖啡为例:

//: generics/coffee/Coffee.java

package generics.coffee;

public class Coffee {

private static long counter = 0;

private final long id = counter++;

public String toString() {

return getClass().getSimpleName() + " " + id;

}

} ///:~

//: generics/coffee/Latte.java

package generics.coffee;

public class Latte extends Coffee {} ///:~

//: generics/coffee/Mocha.java

package generics.coffee;

public class Mocha extends Coffee {} ///:~

//: generics/coffee/Cappuccino.java

package generics.coffee;

public class Cappuccino extends Coffee {} ///:~

//: generics/coffee/Americano.java

package generics.coffee;

public class Americano extends Coffee {} ///:~

//: generics/coffee/Breve.java

package generics.coffee;

public class Breve extends Coffee {} ///:~

具体运用:

//: generics/coffee/CoffeeGenerator.java

// Generate different types of Coffee:

package generics.coffee;

import java.util.*;

import net.mindview.util.*;

public class CoffeeGenerator

implements Generator<Coffee>, Iterable<Coffee> {

private Class[] types = { Latte.class, Mocha.class,

Cappuccino.class, Americano.class, Breve.class, };

private static Random rand = new Random(47);

public CoffeeGenerator() {}

// For iteration:

private int size = 0;

public CoffeeGenerator(int sz) { size = sz; }

public Coffee next() {

try {

return (Coffee)

types[rand.nextInt(types.length)].newInstance();

// Report programmer errors at run time:

} catch(Exception e) {

throw new RuntimeException(e);

}

}

class CoffeeIterator implements Iterator<Coffee> {

int count = size;

public boolean hasNext() { return count > 0; }

public Coffee next() {

count--;

return CoffeeGenerator.this.next();

}

public void remove() { // Not implemented

throw new UnsupportedOperationException();

}

};

public Iterator<Coffee> iterator() {

return new CoffeeIterator();

}

public static void main(String[] args) {

CoffeeGenerator gen = new CoffeeGenerator();

for(int i = 0; i < 5; i++)

System.out.println(gen.next());

for(Coffee c : new CoffeeGenerator(5))

System.out.println(c);

}

} //:~

这里面有几个细节值得一提:

首先是

return (Coffee)types[rand.nextInt(types.length)].newInstance();

这个是Class的public T newInstance()方法,由于这个方法会throw InstantiationExceptionIllegalAccessException 这两种Exception,所以要用try catch框起来。

然后是代码中throw的两个Exception,throw new RuntimeException(e);和throw new UnsupportedOperationException();都很值得学习。前者相当于强制让编译器通过,后者是个见到的比较少的Exception。

同时这里面还有个比较简单的iterator实现,主要看点在于两个next()方法

另一个比较简单点的例子是这个Fibonacci数列:

//: generics/Fibonacci.java

// Generate a Fibonacci sequence.

import net.mindview.util.*;

public class Fibonacci implements Generator<Integer> {

private int count = 0;

public Integer next() { return fib(count++); }

private int fib(int n) {

if(n < 2) return 1;

return fib(n-2) + fib(n-1);

}

public static void main(String[] args) {

Fibonacci gen = new Fibonacci();

for(int i = 0; i < 18; i++)

System.out.print(gen.next() + " ");

}

} //:~

当然,如果加入iterator又会开始变得复杂:

//: generics/IterableFibonacci.java

// Adapt the Fibonacci class to make it Iterable.

import java.util.*;

public class IterableFibonacci

extends Fibonacci implements Iterable<Integer> {

private int n;

public IterableFibonacci(int count) { n = count; }

public Iterator<Integer> iterator() {

return new Iterator<Integer>() {

public boolean hasNext() { return n > 0; }

public Integer next() {

n--;

return IterableFibonacci.this.next();

}

public void remove() { // Not implemented

throw new UnsupportedOperationException();

}

};

}

public static void main(String[] args) {

for(int i : new IterableFibonacci(18))

System.out.print(i + " ");

}

} //:~

参考资料:

Thinking in Java第四版446页-449页

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值