您的位置:首页 > 编程语言 > Java开发

让你一看就明白什么是聚合-java版本_源码下载

2013-02-24 01:33 330 查看
项目结构:



运行效果:



=================================================

代码部分:

=================================================

/MovProxy/src/com/b510/movproxy/test/MovProxyTest.java

/**
*
*/
package com.b510.movproxy.test;

import com.b510.movproxy.dao.Moveable;
import com.b510.movproxy.dao.impl.Train;
import com.b510.movproxy.proxy.TrainLogProxy;
import com.b510.movproxy.proxy.TrainTimeProxy;
import com.b510.movproxy.proxy.TrainTranscationProxy;

/**
* 测试类
*
* @author hongten(hongtenzone@foxmail.com)
* @date 2013-2-24
*/
public class MovProxyTest {
public static void main(String[] args) throws Exception {
// (0.火车 1.事物 2.时间 3.日志) 从内向外格式
Train train = new Train();
TrainTranscationProxy trainTranscationProxy = new TrainTranscationProxy(train);
TrainTimeProxy trainTimeProxy = new TrainTimeProxy(trainTranscationProxy);
TrainLogProxy trainLogProxy = new TrainLogProxy(trainTimeProxy);
Moveable moveable = trainLogProxy;
moveable.move();
}

}


/MovProxy/src/com/b510/movproxy/dao/impl/Train.java

/**
*
*/
package com.b510.movproxy.dao.impl;

import java.util.Random;

import com.b510.movproxy.dao.Moveable;

/**
* 火车类
* @author hongten(hongtenzone@foxmail.com)
* @date 2013-2-24
*/
public class Train implements Moveable{

public void move() throws Exception{
System.out.println("the Train begin to move ......");
long timer = new Random().nextInt(15000);
try {
Thread.sleep(timer);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("the Train end to move ......" + timer);
}

}


/MovProxy/src/com/b510/movproxy/dao/Moveable.java

/**
*
*/
package com.b510.movproxy.dao;

/**
* @author hongten(hongtenzone@foxmail.com)
* @date 2013-2-24
*/
public interface Moveable {

public void move() throws Exception;
}


/MovProxy/src/com/b510/movproxy/proxy/TrainTranscationProxy.java

/**
*
*/
package com.b510.movproxy.proxy;

import java.util.Random;

import com.b510.movproxy.dao.Moveable;

/**
* 火车的事物代理
* @author hongten(hongtenzone@foxmail.com)
* @date 2013-2-24
*/
public class TrainTranscationProxy implements Moveable{

/**
* Moveable接口
*/
private Moveable moveable;

/**
* 在构造方法中传递Moveable
*/
public TrainTranscationProxy(Moveable moveable){
this.moveable = moveable;
}

public void move() throws Exception {
System.out.println("the transcation begin......");
long timer = new Random().nextInt(15000);
try {
moveable.move();
Thread.sleep(timer);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("the transcation end ....." + timer);

}

}


/MovProxy/src/com/b510/movproxy/proxy/TrainTimeProxy.java

/**
*
*/
package com.b510.movproxy.proxy;

import com.b510.movproxy.dao.Moveable;

/**
* 火车的时间代理
* @author hongten(hongtenzone@foxmail.com)
* @date 2013-2-24
*/
public class TrainTimeProxy implements Moveable{
/**
* Moveable接口
*/
private Moveable moveable;

/**
* 在构造方法中传递Moveable
*/
public TrainTimeProxy(Moveable moveable){
this.moveable = moveable;
}

public void move() throws Exception {
long start = System.currentTimeMillis();
System.out.println("the time of start .........");
moveable.move();
long end = System.currentTimeMillis();
System.out.println("the result time :" + (end - start));

}

}


/MovProxy/src/com/b510/movproxy/proxy/TrainLogProxy.java

/**
*
*/
package com.b510.movproxy.proxy;

import com.b510.movproxy.dao.Moveable;

/**
* 火车的日志代理
* @author hongten(hongtenzone@foxmail.com)
* @date 2013-2-24
*/
public class TrainLogProxy implements Moveable{

/**
* Moveable接口
*/
private Moveable moveable;

/**
* 在构造方法中传递Moveable
*/
public TrainLogProxy(Moveable moveable){
this.moveable = moveable;
}

public void move() throws Exception {
System.out.println("start to log of the move() method!!!");
moveable.move();
System.out.println("stop to log of the move() method!!!");
}

}


源码下载http://files.cnblogs.com/hongten/MovProxy.zip
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: