java: 四器 之 生成器初学

来源:互联网 发布:多源异构数据集成 编辑:程序博客网 时间:2024/05/22 07:54
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
原创粉丝点击