Java简单工厂类模型,静态代理模式,适配器模式

  1. 未使用简单工厂模式
public class l6 {
    public static void main(String[] args) {
        // 使用者和被使用者耦合,产生了依赖,当被使用着改变时会影响使用者
        // 使用工厂模式降低两者之间依赖
        Product phone = new Phone();
        phone.work();
    }
}

interface Product {
    public void work();
}

class Phone implements Product {
    public void work() {
        System.out.println("手机开始工作");
    }
}

class Computer implements Product {
    public void work() {
        System.out.println("电脑开始工作");
    }
}
  1. 使用简单工厂模式
public class l6 {
    public static void main(String[] args) {
        // 使用者和被使用者耦合,产生了依赖,当被使用着改变时会影响使用者
        // 使用工厂模式降低两者之间依赖
        Product phone = ProductFactory.getProduct("phone");
        if (null != phone) {
            phone.work();
        }
    }
}

// 工厂类
class ProductFactory {
    public static Product getProduct(String name) {
        if ("phone".equals(name)) {
            return new Phone();
        } else if ("computer".equals(name)) {
            return new Computer();
        } else {
            return null;
        }
    }
}

interface Product {
    public void work();
}

class Phone implements Product {
    public void work() {
        System.out.println("手机开始工作");
    }
}

class Computer implements Product {
    public void work() {
        System.out.println("电脑开始工作");
    }
}

在这里插入图片描述

  1. 未使用静态代理
public class l6 {
    public static void main(String[] args) {

    }
}

interface Action {
    public void doAction();
}

class UserAction implements Action {
    public void doAction() {
        System.out.println("用户开始做工作啦");
    }
}

但是在用户开始工作前后的准备啥事情一定得需要做,但是又不是老板要的结果,又为必需品,此时就要用到静态代理模式,请往下看

  1. 使用静态代理模式
public class l6 {
    public static void main(String[] args) {
        Action useAction = new UserAction();
        ActionProxy proxy = new ActionProxy(useAction);
        proxy.doAction();
    }
}

class ActionProxy implements Action {
    private Action target;// 被代理的对象

    public ActionProxy(Action target) {
        this.target = target;
    }

    // 执行操作
    public void doAction() {
        long startTimne = System.currentTimeMillis();
        target.doAction();// 执行真正的业务
        long endTime = System.currentTimeMillis();
        System.out.println("共耗时:" + (endTime - startTimne));
    }
}

interface Action {
    public void doAction();
}

class UserAction implements Action {
    public void doAction() {
        System.out.println("用户开始做工作啦");
    }
}

为啥 为啥耗时0呢太短了就一个输出,让我们给它加点复杂点

class UserAction implements Action {
    public void doAction() {
        for (int i = 1; i <= 200; i++) {
            System.out.println("用户开始做工作啦");
        }
    }
}

在这里插入图片描述

  1. 使用适配器前
public class l6 {
    public static void main(String[] args) {
        PowerA powerA = new PowerAImpl();
        work(powerA);
    }

    public static void work(PowerA a) {
        System.out.println("正在连接");
        a.insert();
        ;
        System.out.println("工作结束");
    }
}

interface PowerA {
    public void insert();
}

class PowerAImpl implements PowerA {
    public void insert() {
        System.out.println("电源A开始工作");
    }
}

在这里插入图片描述
注意对修改关闭,对扩展开放
怎么补救到达上面想让也输出PowerB,但是不再重新定义方法来输出调用…适配器(解决A和B不兼容,B不能转到work()方法里面调用)

public class l6 {
    public static void main(String[] args) {
        PowerA powerA = new PowerAImpl();
        work(powerA);
        PowerB powerB = new PowerBImpl();
        Adapter adapter = new Adapter(powerB);
        work(adapter);
    }

    public static void work(PowerA a) {
        System.out.println("正在连接");
        a.insert();
        System.out.println("工作结束");
    }
}

// 适配器
class Adapter implements PowerA {
    private PowerB powerB;

    public Adapter(PowerB powerB) {
        this.powerB = powerB;
    }

    public void insert() {
        powerB.connect();
    }
}

interface PowerB {
    public void connect();
}

class PowerBImpl implements PowerB {
    public void connect() {
        System.out.println("电源B开始工作");
    }
}

interface PowerA {
    public void insert();
}

class PowerAImpl implements PowerA {
    public void insert() {
        System.out.println("电源A开始工作");
    }
}

在这里插入图片描述

  1. 适配器的另一种用法
interface Animal {
    public void sing();

    public void cry();

    public void run();

    public void swim();
}

class Dog implements Animal {
    public void sing() {
    }

    public void cry() {
    }

    public void run() {
        System.out.println("我是一只修勾,最喜欢奔跑的感觉");
    }

    public void swim() {
    }
}

每回实现类都要四个方法都写一遍很麻烦,如下

// 中间类:(适配器类)
abstract class AnimalFunction {
    public void sing() {
    }

    public void cry() {
    }

    public void run() {
    }

    public void swim() {
    }
}

interface Animal {
    public void sing();

    public void cry();

    public void run();

    public void swim();
}

class Dog extends AnimalFunction {
    public void run() {
        System.out.println("我是一只修勾,最喜欢奔跑的感觉");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值