In this chapter, we will discuss the use of generic interfaces.
We take generator as an example. Generator is an application of factory method. It is mainly used to create objects. Generally, factory method needs input parameters to obtain different objects, but generators are a series of objects.
Code:
package com.ray.ch13; import java.util.Iterator; import java.util.Random; public class Test implements Generator<Father> { private Class<?>[] classes = { Sub1.class, Sub2.class, Sub3.class }; private Random random = new Random(); @Override public Father next() { Father father = null; try { father = (Father) classes[random.nextInt(3)].newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return father; } public static void main(String[] args) { Test test = new Test(); for (int i = 0; i < 5; i++) { System.out.println(test.next()); } } } interface Generator<T> { T next(); } class Father { private static int counter = 0; private final int id = counter++; @Override public String toString() { return "name:" + getClass().getSimpleName() + " id:" + id; } } class Sub1 extends Father { } class Sub2 extends Father { } class Sub3 extends Father { }
Output:
name:Sub3 id:0
name:Sub1 id:1
name:Sub3 id:2
name:Sub3 id:3
name:Sub3 id:4
The above code generates five Father's self-object Sub through the generator. In the process of creating, the generator does not need input parameters, but generates some column Father's subclass objects directly.
Let's modify the above code to make it suitable for foreach (that is, to implement the Iterable interface):
package com.ray.ch13; import java.util.Iterator; import java.util.Random; public class Test implements Generator<Father>, Iterable<Father> { public Test() { } private int size = 0; public Test(int size) { this.size = size; } private Class<?>[] classes = { Sub1.class, Sub2.class, Sub3.class }; private Random random = new Random(); @Override public Father next() { Father father = null; try { father = (Father) classes[random.nextInt(3)].newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return father; } public static void main(String[] args) { Test test = new Test(); for (int i = 0; i < 5; i++) { System.out.println(test.next()); } System.out.println("------------------"); for (Father father : new Test(5)) { System.out.println(father); } } @Override public Iterator<Father> iterator() { return new FatherIterator(); } class FatherIterator implements Iterator<Father> { private int count = size; @Override public boolean hasNext() { return count > 0; } @Override public Father next() { count--; return Test.this.next(); } @Override public void remove() { throw new UnsupportedOperationException(); } } } interface Generator<T> { T next(); } class Father { private static int counter = 0; private final int id = counter++; @Override public String toString() { return "name:" + getClass().getSimpleName() + " id:" + id; } } class Sub1 extends Father { } class Sub2 extends Father { } class Sub3 extends Father { }
Output:
name:Sub3 id:0
name:Sub1 id:1
name:Sub3 id:2
name:Sub3 id:3
name:Sub3 id:4
------------------
name:Sub3 id:5
name:Sub1 id:6
name:Sub1 id:7
name:Sub2 id:8
name:Sub3 id:9
The parameterized interface ensures the return type of next ().
Let's look at another example, the famous mathematical problem Fibonacci sequence:
package com.ray.ch13; public class Test implements Generator<Integer> { private Integer count = 0; @Override public Integer next() { return fib(count++); } private Integer fib(int param) { if (param < 2) { return 1; } return fib(param - 2) + fib(param - 1); } public static void main(String[] args) { Test test = new Test(); for (int i = 0; i < 10; i++) { System.out.print(test.next() + " "); } } } interface Generator<T> { T next(); }
Output:
1 1 2 3 5 8 13 21 34 55
Summary: This chapter mainly shows the use of generic interfaces.
That's all for this chapter. Thank you.
-----------------------------------
Reproduced in: https://my.oschina.net/u/2325575/blog/543734