java: 四器 之 生成器初学

package GeneIterAdaptReflect.com;

import java.util.Random;

/*
 * 了解 Generator 及其应有方式;
 * 生成器是专门负责创建对象的类,实际上,这是工厂方法设计模式的一种应用;不过当使用生成器创建新对象时,它不需要任何参数;
 * 一般而言:一个生成器只定义一个方法,该方法用于产生新的对象,通常定义为next()方法;
 *   interface Generator<T>{
        T next();
 *   }
 * 
 */
interface Generator<T>{
    T next();
}

class Fibonacci implements Generator<Integer>{
    /*
     * 产生数学上常用的斐波那契数列
     * 序列:1 2 3 4 5 6 7  8  9……
     * 数据:1 1 2 3 5 8 13 24
     * */
    private static int count=0;
    public int fibo(int x){
        if(x<2)
            return 1;
        return fibo(x-2)+fibo(x-1);
    }

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

/*java t4上一个咖啡例子
 * 
 * 
 * */
class Coffee{
    private static int count=1;
    private  final int id = count++;

    public String toString(){
        return id+"  "+ this.getClass().getSimpleName();
    }
}
class Latter extends Coffee{}
class Mocha extends Coffee{}
class Cappuccino extends Coffee{}
class Breve extends Coffee{}

class CoffeeGenerator implements Generator<Coffee>{
    private Class[] types = new Class[]{Latter.class,Mocha.class,Cappuccino.class,Breve.class};
    private Random rand = new Random();

    public Coffee next(){
        try {
            return (Coffee) types[rand.nextInt(types.length)].newInstance();
        } catch (InstantiationException e) {
            System.out.println("new Instance() fail");
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }
}

//随机数生产器
class RandomGenerator{
    private static Random r = new Random();

    public static class Boolean implements Generator<java.lang.Boolean>{
        public java.lang.Boolean next(){
            return r.nextBoolean();
        }
    }

    public static class Character implements Generator<java.lang.Character>{
        private char[] chars =("abcdefghijklmnopqrstuvwsyz"+"ABCDEFGHIJKLMNOPQRSTUVWSYZ").toCharArray();
        public java.lang.Character next(){
            return  chars[r.nextInt(chars.length)];
        }
    }

    public static class Integer implements Generator<java.lang.Integer>{
        public java.lang.Integer next(){
            return r.nextInt(1000);
        }
    }
}
public class GeneratorTest  {

    public static void test(Class<?> sc){
        for(Class<?> c:sc.getClasses()){
            try {
                Generator<?> ge = (Generator<?>)c.newInstance();
                for(int i=0;i<10;i++)
                    System.out.print(ge.next()+"  ");
                System.out.println();
            } catch (InstantiationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
    public static void main(String[] args) {
        Fibonacci fibo = new Fibonacci();
        for(int i=0;i<20;i++)
            System.out.print(fibo.next()+" ");
        System.out.println();



        System.out.println("------------------------------");
        CoffeeGenerator cg = new CoffeeGenerator();
        for(int i=0;i<4;i++)
            System.out.println(cg.next().toString());

        System.out.println("------------------------------");
        test(RandomGenerator.class);
    }

}
/*输出结果:
  1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 

    ------------------------------
    1  Mocha
    2  Cappuccino
    3  Breve
    4  Latter
    ------------------------------
    true  true  true  true  false  true  false  true  true  false  
    B  m  V  k  q  C  W  J  O  z  
    442  626  975  3  375  377  179  513  110  106  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值