<pre name="code" class="java">//: net/mindview/util/Generator.java // A generic interface. package net.mindview.util; public interface Generator<T> { T next(); } ///:~
/** * 书本:《Thinking In Java》 * 功能:数据生成器--策略设计模式 * 文件:CountingGenerator.java * 时间:2015年4月29日19:05:09 * 作者:cutter_point */ package net.mindview.util; public class CountingGenerator { //一个布尔值的静态类 public static class Boolean implements Generator<java.lang.Boolean> { private boolean value = false; @Override public java.lang.Boolean next() { //返回一个和value相反的值 value = !value; return value; } } //字节类型的静态类 public static class Byte implements Generator<java.lang.Byte> { private byte value = 0; @Override public java.lang.Byte next() { //下一个就是加一位,并返回前面那个值 return value++; } } //静态的字符数组包含所有的字母 static char[] chars = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray(); //字符型的 public static class Character implements Generator<java.lang.Character> { int index = -1; //索引 @Override//得到一串字符里面的 public java.lang.Character next() { index = (index + 1)%chars.length; //看看下一个字符在所有的字母里面的第几位 return chars[index]; } } //字符串类型 public static class String implements Generator<java.lang.String> { private int length = 7; Generator<java.lang.Character> cg = new Character(); public String() {} public String(int length) { this.length = length; } public java.lang.String next() { char[] buf = new char[length]; for(int i = 0; i < length; ++i) { buf[i] = cg.next(); } return new java.lang.String(buf); } } //short类型的数据 public static class Short implements Generator<java.lang.Short> { private short value = 0; @Override public java.lang.Short next() { return value++; } } public static class Integer implements Generator<java.lang.Integer> { private int value = 0; @Override public java.lang.Integer next() { return value++; } } public static class Float implements Generator<java.lang.Float> { private float value = 0; public java.lang.Float next() { float result = value; value += 1.0; return result; } } public static class Double implements Generator<java.lang.Double> { private double value = 0.0; public java.lang.Double next() { double result = value; value += 1.0; return result; } } // public static void main(java.lang.String[] args) // { // Byte b = new Byte(); // int i = b.next(); // System.out.println(i); // // System.out.println(new Character().next()); // // String s = new String(9); // // System.out.println(s.next()); // } }
/*** 书本:《Thinking In Java》* 功能:数据生成器--策略设计模式--测试类* 文件:CountingGenerator.java* 时间:2015年4月29日19:05:09* 作者:cutter_point*/package Lesson16Arrays;import net.mindview.util.CountingGenerator;import net.mindview.util.Generator;public class GeneratorsTest{public static
int size = 10;public static void test(Class<?> surroundingClass){for(Class<?> type : surroundingClass.getClasses()) //取得这个类对象里面的类{//取得这个类型的数组System.out.println(type.getSimpleName() + ": ");try{Generator<?> g = (Generator<?>)type.newInstance(); //获取一个实例化对象//输出他的计数for(int
i = 0; i < size; ++i){System.out.printf(g.next() + " ");}System.out.println();}catch (Exception e){e.printStackTrace();}}}public static void main(String[] args){test(CountingGenerator.class);}}
输出:
Boolean:
true false true false true false true false true false
Byte:
0 1 2 3 4 5 6 7 8 9
Character:
a b c d e f g h i j
Double:
0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
Float:
0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
Integer:
0 1 2 3 4 5 6 7 8 9
Short:
0 1 2 3 4 5 6 7 8 9
String:
abcdefg hijklmn opqrstu vwxyzAB CDEFGHI JKLMNOP QRSTUVW XYZabcd efghijk lmnopqr