设计模式——代理模式

引言

网上有很多介绍设计模式系列的优秀文章,看懂不一定是真懂,能写出来,说出个1、2、3点才算是真的懂了,如果能灵活的应用到工作中才算是真的熟练。

1、模式场景

记录不同的动物,比如小狗、小猫吃饭、睡觉、玩耍的时间。

2、模式结构

设计模式——代理模式_第1张图片

  • Subject: 抽象角色(模拟场景中的动物)
  • Proxy: 代理角色(模拟场景中的动物代理)
  • RealSubject: 真实角色(模拟场景中的小狗、小猫)

3、示例代码

3.1、抽象角色

public interface Animal {
    void eat() throws InterruptedException;

    void sleep() throws InterruptedException;

    void play() throws InterruptedException;
}

3.2、代理角色

public class AnimalProxy implements Animal {

    private Animal animal;

    public AnimalProxy(Animal animal){
        this.animal = animal;
    }

    @Override
    public void eat() throws InterruptedException {
        long start = System.currentTimeMillis();
        animal.eat();
        long end = System.currentTimeMillis();
        System.out.println("eating time is :" + (end - start) + "ms");
    }

    @Override
    public void sleep() throws InterruptedException {
        long start = System.currentTimeMillis();
        animal.sleep();
        long end = System.currentTimeMillis();
        System.out.println("sleeping time is :" + (end - start) + "ms");
    }

    @Override
    public void play() throws InterruptedException {
        long start = System.currentTimeMillis();
        animal.play();
        long end = System.currentTimeMillis();
        System.out.println("playing time is :" + (end - start) + "ms");
    }
}

3.3、真实角色

public class Dog implements Animal{

    @Override
    public void eat() throws InterruptedException {
        System.out.println("dog is eating");
        Thread.sleep(1000);
    }

    @Override
    public void sleep() throws InterruptedException {
        System.out.println("dog is sleeping");
        Thread.sleep(5000);
    }

    @Override
    public void play() throws InterruptedException {
        System.out.println("dog is playing");
        Thread.sleep(3000);
    }
}
public class Cat implements Animal{

    @Override
    public void eat() throws InterruptedException {
        System.out.println("cat is eating");
        Thread.sleep(2000);
    }

    @Override
    public void sleep() throws InterruptedException {
        System.out.println("cat is sleeping");
        Thread.sleep(2000);
    }

    @Override
    public void play() throws InterruptedException {
        System.out.println("cat is playing");
        Thread.sleep(3000);
    }
}

3.4、客户端

public class Client {

    public static void main(String[] args) throws InterruptedException {
        AnimalProxy dog = new AnimalProxy(new Dog());
        dog.eat();
        dog.sleep();
        dog.play();
        System.out.println("=================================");
        AnimalProxy cat = new AnimalProxy(new Cat());
        cat.eat();
        cat.sleep();
        cat.play();
    }
}

控制台输出:

dog is eating
eating time is :1000ms
dog is sleeping
sleeping time is :5000ms
dog is playing
playing time is :3001ms
=================================
cat is eating
eating time is :2001ms
cat is sleeping
sleeping time is :2001ms
cat is playing
playing time is :3000ms

4、模式优点

  • 代理模式中代理对象能够控制真实对象的权限,增加了安全性。
  • 协调调用者与被调用者,降低了它们的耦合度,简化了代码。

5、模式缺点

  • 增加了代理,可能会造成请求的处理速度变慢。
  • 增加了额外的代理部分工作量。

结束语

JDK中的动态代理是一种较为高级的代理模式。下一篇:设计模式——命令模式。

你可能感兴趣的:(设计模式,设计模式,java,代理模式)