[Thinking in Java]15.泛型
2014-08-05 11:37
218 查看
1.泛型接口与工厂方法设计模式相结合.
2.与模板方法设计模式相结合.(模板方法设计模式我的理解是在接口与实现之间再加一个抽象类,完成基本实现)
3.装饰器设计模式.这一点似乎和泛型关系不大?(装饰器模式就是类的不同组合,实现一种复杂的功能,典型应用在java i/o)
4.与动态代理模式结合.
class MixinProxy implements InvocationHandler {
Map<String,Object> delegatesByMethod;
public MixinProxy(TwoTuple<Object,Class<?>>... pairs) {
delegatesByMethod = new HashMap<String,Object>();
for(TwoTuple<Object,Class<?>> pair : pairs) {
for(Method method : pair.second.getMethods()) {
String methodName = method.getName();
// The first interface in the map
// implements the method.
if (!delegatesByMethod.containsKey(methodName))
delegatesByMethod.put(methodName, pair.first);
}
}
}
public Object invoke(Object proxy, Method method,
Object[] args) throws Throwable {
String methodName = method.getName();
Object delegate = delegatesByMethod.get(methodName);
return method.invoke(delegate, args);
}
@SuppressWarnings("unchecked")
public static Object newInstance(TwoTuple... pairs) {
Class[] interfaces = new Class[pairs.length];
for(int i = 0; i < pairs.length; i++) {
interfaces[i] = (Class)pairs[i].second;
}
ClassLoader cl =
pairs[0].first.getClass().getClassLoader();
return Proxy.newProxyInstance(
cl, interfaces, new MixinProxy(pairs));
}
} 5.泛型与数组:无法直接创建数组.有趣的是,你可以声明一个泛型数组,但是没有办法初始化.
6.推荐创建泛型数组的方式:
public class GenericArray2<T> {
private Object[] array;
public GenericArray2(int sz) {
array = new Object[sz];
}
public void put(int index, T item) {
array[index] = item;
}
@SuppressWarnings("unchecked")
public T get(int index) { return (T)array[index]; }
@SuppressWarnings("unchecked")
public T[] rep() {
return (T[])array; // Warning: unchecked cast
}
public static void main(String[] args) {
GenericArray2<Integer> gai =
new GenericArray2<Integer>(10);
for(int i = 0; i < 10; i ++)
gai.put(i, i);
for(int i = 0; i < 10; i ++)
System.out.print(gai.get(i) + " ");
System.out.println();
try {
Integer[] ia = gai.rep();
} catch(Exception e) { System.out.println(e); }
}
}
7.通配符的使用原则:PECS producer-extends ,consumer-super.如果参数产生实例供函数使用,就是生产者,如果参数消费实例,则是消费者.不要用通配符类型作为返回类型.
例如我们将 public static <T extends Comparable<T> > max(List<T> list> 修改成如下:
public static<T extends Comparable<? super T> > max(List<? extends T> list )理由如下:
1)list是生产者,生成实例供max选择,因此需要? extends T.
2)Comparable是消费者(永远是消费者),需要两个实例来比较,因此改写成 Comparable<? super T>
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); } }
2.与模板方法设计模式相结合.(模板方法设计模式我的理解是在接口与实现之间再加一个抽象类,完成基本实现)
abstract class GenericWithCreate<T> { final T element; GenericWithCreate() { element = create(); } abstract T create(); } class X {} class Creator extends GenericWithCreate<X> { X create() { return new X(); } void f() { System.out.println(element.getClass().getSimpleName()); } } public class CreatorGeneric { public static void main(String[] args) { Creator c = new Creator(); c.f(); } }
3.装饰器设计模式.这一点似乎和泛型关系不大?(装饰器模式就是类的不同组合,实现一种复杂的功能,典型应用在java i/o)
4.与动态代理模式结合.
class MixinProxy implements InvocationHandler {
Map<String,Object> delegatesByMethod;
public MixinProxy(TwoTuple<Object,Class<?>>... pairs) {
delegatesByMethod = new HashMap<String,Object>();
for(TwoTuple<Object,Class<?>> pair : pairs) {
for(Method method : pair.second.getMethods()) {
String methodName = method.getName();
// The first interface in the map
// implements the method.
if (!delegatesByMethod.containsKey(methodName))
delegatesByMethod.put(methodName, pair.first);
}
}
}
public Object invoke(Object proxy, Method method,
Object[] args) throws Throwable {
String methodName = method.getName();
Object delegate = delegatesByMethod.get(methodName);
return method.invoke(delegate, args);
}
@SuppressWarnings("unchecked")
public static Object newInstance(TwoTuple... pairs) {
Class[] interfaces = new Class[pairs.length];
for(int i = 0; i < pairs.length; i++) {
interfaces[i] = (Class)pairs[i].second;
}
ClassLoader cl =
pairs[0].first.getClass().getClassLoader();
return Proxy.newProxyInstance(
cl, interfaces, new MixinProxy(pairs));
}
} 5.泛型与数组:无法直接创建数组.有趣的是,你可以声明一个泛型数组,但是没有办法初始化.
6.推荐创建泛型数组的方式:
public class GenericArray2<T> {
private Object[] array;
public GenericArray2(int sz) {
array = new Object[sz];
}
public void put(int index, T item) {
array[index] = item;
}
@SuppressWarnings("unchecked")
public T get(int index) { return (T)array[index]; }
@SuppressWarnings("unchecked")
public T[] rep() {
return (T[])array; // Warning: unchecked cast
}
public static void main(String[] args) {
GenericArray2<Integer> gai =
new GenericArray2<Integer>(10);
for(int i = 0; i < 10; i ++)
gai.put(i, i);
for(int i = 0; i < 10; i ++)
System.out.print(gai.get(i) + " ");
System.out.println();
try {
Integer[] ia = gai.rep();
} catch(Exception e) { System.out.println(e); }
}
}
7.通配符的使用原则:PECS producer-extends ,consumer-super.如果参数产生实例供函数使用,就是生产者,如果参数消费实例,则是消费者.不要用通配符类型作为返回类型.
例如我们将 public static <T extends Comparable<T> > max(List<T> list> 修改成如下:
public static<T extends Comparable<? super T> > max(List<? extends T> list )理由如下:
1)list是生产者,生成实例供max选择,因此需要? extends T.
2)Comparable是消费者(永远是消费者),需要两个实例来比较,因此改写成 Comparable<? super T>
相关文章推荐
- thinking in java笔记 15 泛型
- thinking in java笔记 15 泛型
- Java基础15:treeset;排序方法-比较器;泛型;
- java 集合框架 泛型--15
- Java编程思想 ch15 泛型
- Thinking in Java--ch15 分布式计算
- Java学习15 泛型
- Thinking in java-38 Java 泛型Generics in java
- 黑马程序员_JAVA笔记15——集合框架(泛型)
- Thinking in java学习笔记—泛型(擦除)
- Thinking in java:泛型
- java学习(15)泛型
- Thinking in Java 15
- 黑马程序员--Java基础加强--15.利用反射操作泛型IV【通过反射Method解析泛型方法思路】【通过Method对四种Type子接口类型进行解剖】【使用递归对任意复合泛型类型进行彻底解剖】【个人
- Java 读书笔记 15泛型
- Thinking in java-15 传值机制&&关系问题
- Thinking in Java 泛型章节中不用LinkedList来实现自己的内部链式存储机制
- Thinking in Java——第十五章-泛型
- 黑马程序员—15—java基础:有关泛型的学习笔记和学习心得体会
- Thinking in Java 第15章 泛型(15.5-15.19)