坦克大战(一期)-设计模式-静态代理

DesignPatterns
bingor 2 years ago
parent 411050d429
commit b5fc39183d

@ -0,0 +1,11 @@
package com.msb.proxy.v5;
/**
* @Author bingor
* @Date 2022/10/17 9:40
* @Description: com.msb.proxy.v5
* @Version: 1.0
*/
public interface Movable {
public void move();
}

@ -0,0 +1,30 @@
package com.msb.proxy.v5;/**
* @Author bingor
* @Date 2022/10/17 9:42
* @Description: com.msb.proxy.v5
* @Version: 1.0
*/
/**
*@ClassName ProxyTank
*@Description TODO
*@Author bingor
*@Date 2022/10/17 9:42
*@Version 3.0
*/
public class ProxyTank implements Movable {
private Tank tank = new Tank();
@Override
public void move() {
long startTime = System.currentTimeMillis();
tank.move();
long entTime = System.currentTimeMillis();
System.out.println(entTime - startTime);
}
public static void main(String[] args) {
new ProxyTank().move();
}
}

@ -0,0 +1,27 @@
package com.msb.proxy.v5;/**
* @Author bingor
* @Date 2022/10/17 9:41
* @Description: com.msb.proxy.v5
* @Version: 1.0
*/
import java.util.Random;
/**
*@ClassName Tank
*@Description TODO
*@Author bingor
*@Date 2022/10/17 9:41
*@Version 3.0
*/
public class Tank implements Movable {
@Override
public void move() {
System.out.println("tank move……");
try {
Thread.sleep(new Random().nextInt(10000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

@ -0,0 +1,19 @@
package com.msb.proxy.v6;/**
* @Author bingor
* @Date 2022/10/17 9:59
* @Description: com.msb.proxy.v6
* @Version: 1.0
*/
/**
*@ClassName Main
*@Description TODO
*@Author bingor
*@Date 2022/10/17 9:59
*@Version 3.0
*/
public class Main {
public static void main(String[] args) {
new TankLogProxy(new TankTimeProxy(new Tank())).move();
}
}

@ -0,0 +1,11 @@
package com.msb.proxy.v6;
/**
* @Author bingor
* @Date 2022/10/17 9:40
* @Description: com.msb.proxy.v5
* @Version: 1.0
*/
public interface Movable {
public void move();
}

@ -0,0 +1,27 @@
package com.msb.proxy.v6;/**
* @Author bingor
* @Date 2022/10/17 9:41
* @Description: com.msb.proxy.v5
* @Version: 1.0
*/
import java.util.Random;
/**
*@ClassName Tank
*@Description TODO
*@Author bingor
*@Date 2022/10/17 9:41
*@Version 3.0
*/
public class Tank implements Movable {
@Override
public void move() {
System.out.println("tank move……");
try {
Thread.sleep(new Random().nextInt(10000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

@ -0,0 +1,30 @@
package com.msb.proxy.v6;/**
* @Author bingor
* @Date 2022/10/17 9:42
* @Description: com.msb.proxy.v5
* @Version: 1.0
*/
/**
*@ClassName ProxyTank
*@Description TODO
*@Author bingor
*@Date 2022/10/17 9:42
*@Version 3.0
*/
public class TankLogProxy implements Movable {
private Movable movable;
public TankLogProxy(Movable movable) {
this.movable = movable;
}
@Override
public void move() {
System.out.println("move start run");
movable.move();
System.out.println("move end run");
}
}

@ -0,0 +1,31 @@
package com.msb.proxy.v6;/**
* @Author bingor
* @Date 2022/10/17 9:42
* @Description: com.msb.proxy.v5
* @Version: 1.0
*/
/**
*@ClassName ProxyTank
*@Description TODO
*@Author bingor
*@Date 2022/10/17 9:42
*@Version 3.0
*/
public class TankTimeProxy implements Movable {
private Movable movable;
public TankTimeProxy(Movable movable) {
this.movable = movable;
}
@Override
public void move() {
long startTime = System.currentTimeMillis();
movable.move();
long entTime = System.currentTimeMillis();
System.out.println(entTime - startTime);
}
}
Loading…
Cancel
Save