java中对于混型的方案
来源:互联网 发布:淘宝店铺怎么改店名 编辑:程序博客网 时间:2024/05/01 02:45
所谓的混型就是让多个类的特性集中到一个类的上面,在java中主要有两种方案来针对这个问题,第一个是用接口,第二种使用动态代理的方式,如果使用第一种方式而且构造的对象模型非常复杂的话,那么对象的层次结构也将变的复杂,第二种方式显的更有灵活性,下面是两种方案的代码
package com.eric.generic;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import java.util.Date;import java.util.HashMap;import java.util.Map;public class MixinSolution {public static void main(String[] args) {/* * use interface to produce effect of mixing, to make EleWatch have * Watch,Clock Calendar functions */EleWatch ew = new EleWatch();ew.calendar();ew.clock();ew.printTime();/* * use dynamic proxy to produce effect of mixing */Object mixing = MixinHandler.newInstance(new Tuple2(new ClockImp(), Clock.class), new Tuple2(new CalendarImp(), Calendar.class));Clock clock = (Clock) mixing;clock.clock();Calendar calendar = (Calendar) mixing;calendar.calendar();}}// ............................................dynamic proxyclass MixinHandler implements InvocationHandler {private static Map<String, Object>delegateMethod= new HashMap<String, Object>();private MixinHandler(Tuple2<Object, Class<?>>... paris) {}private static void initDelegateMethods(Tuple2<Object, Class<?>>... paris) {for (Tuple2<Object, Class<?>> tuple2 : paris) {for (Method method : tuple2.b.getMethods()) {String methodName = method.getName();if (!delegateMethod.containsKey(methodName)) {// put method name as key, Object as value in deledateMethod// mapdelegateMethod.put(methodName, tuple2.a);}}}}public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {System.out.println("before invoke");String methodName = method.getName();Object delegate = delegateMethod.get(methodName);if (delegate != null) {return method.invoke(delegate, args);} else {throw new RuntimeException("method not found!");}}public static Object newInstance(Tuple2<Object, Class<?>>... pairs) {initDelegateMethods(pairs);Class<?>[] interfaces = new Class [pairs.length];int count = 0;for (Tuple2<Object, Class<?>> tuple2 : pairs) {interfaces[count++] = tuple2.b;}return Proxy.newProxyInstance(interfaces[0].getClassLoader(), interfaces, new MixinHandler());}}// ............................................interfaceinterface Clock {public void clock();}class ClockImp implements Clock {public void clock() {System.out.println("Clocking....");}}interface Calendar {public void calendar();}class CalendarImp implements Calendar {public void calendar() {System.out.println("Calending....");}}interface Timer {public void printTime();}class Watch implements Timer {public void printTime() {System.out.println(new Date());}}/* * use interface to produce effect of mixing, to make EleWatch have Watch,Clock * Calendar functions */class EleWatch extends Watch implements Clock, Calendar {private Clock clock = new ClockImp();private Calendarcalendar= new CalendarImp();public void calendar() {calendar.calendar();}public void clock() {clock.clock();}}
- java中对于混型的方案
- java中对于多线程程序的捕获异常的方案
- SSH2对于Java的轻量级实现方案-ganymed
- 对于Java中汉字编码的万能思路
- java中对于文件属性的一些操作
- java中对于String类的理解
- 对于Java中重载的思考
- Java中对于main函数的理解
- Java中对于延迟加载的探讨
- Java中对于callback的理解
- java中对于日期的处理
- 对于java中clone()函数的理解
- Java中对于时间的操作
- 对于java中接口的理解
- 对于Java中*流* 的认识
- 对于java中运算符‘%’的理解
- Java中对于二维数组的clone
- 对于java中volatile关键字的误用
- java----实现小九九
- Maven 小结
- 修复 VS2008 asp.net 设计视图 -工具-选项-[Html设计视图]出现"加载此属性页时出错" 解决方案
- google的速度
- 高精度加减乘法
- java中对于混型的方案
- 跨平台的个人知识管理工具设计思路
- 各常见排序算法实践
- 8种人将被淘汰
- java---实现数组的输出
- usaco 1.5.4 Checker Challenge
- Android模拟打电话程序实现
- 1028. Hanoi Tower Sequence (高精,除2)
- 多一分陪伴少一分孤独