java 设计模式实战,适配器模式之万物拟人化_java平庸实现模式

  • 类适配器模式:
    这种适配器模式下,适配器继承自已实现的类(一般多重继承)。
  • 对象适配器模式:
    在这种适配器模式中,适配器容纳一个它包裹的类的实例。在这种情况下,适配器调用被包裹对象。

设计模式和编程语言无关,但是二当家的依然用Java语言去实战举例。


类的适配器模式

img

  • 源(Adapee)角色:现在需要适配的接口。
  • 目标(Target)角色:这就是所期待得到的接口。注意:由于这里讨论的是类适配器模式,因此目标不可以是类。
  • 适配器(Adaper)角色:适配器类是本模式的核心。适配器把源接口转换成目标接口。显然,这一角色不可以是接口,而必须是具体类。

源(Adapee)角色

二当家喜欢狗狗,所以养了一只狗狗,他有时候会发出叫声。

package com.secondgod.adapter;

/\*\*
 \* 狗狗
 \*
 \* @author 二当家的白帽子 https://le-yi.blog.csdn.net/
 \*/
public class Dog {
    /\*\*
 \* 发出声音
 \*/
    public void makeSound() {
        System.out.println("狗狗:汪汪汪。。。。。。");
    }
}


目标(Target)角色

我们会和朋友聊天说话。

package com.secondgod.adapter;

/\*\*
 \* 朋友
 \* 
 \* @author 二当家的白帽子 https://le-yi.blog.csdn.net/
 \*/
public interface IFriend {
    /\*\*
 \* 说话
 \*/
    void speak();
}


适配器(Adaper)角色

过了一段时间,二当家把狗狗当成了朋友,觉得它不是在叫,而是在说话。

package com.secondgod.adapter;

/\*\*
 \* 狗狗朋友
 \*
 \* @author 二当家的白帽子 https://le-yi.blog.csdn.net/
 \*/
public class DogFriend extends Dog implements IFriend {
    /\*\*
 \* 说话了
 \*/
    @Override
    public void speak() {
        super.makeSound();
    }
}

我们测试一下和狗狗朋友的说话。

package com.secondgod.adapter;

/\*\*
 \* 人
 \*
 \* @author 二当家的白帽子 https://le-yi.blog.csdn.net/
 \*/
public class Person {
    /\*\*
 \* 和朋友聊天
 \*
 \* @param friend
 \*/
    public void speakTo(IFriend friend) {
        System.out.println("人:朋友,你干什么呢?");
        friend.speak();
    }

    public static void main(String[] args) {
        Person  person = new Person();
        IFriend friend = new DogFriend();
        person.speakTo(friend);
    }
}

在这里插入图片描述

二当家的说一句,狗狗叫一声,我们真的像是在聊天。


增加源(Adapee)角色的后果

有一天,二当家的又养了一只猫猫。

package com.secondgod.adapter;

/\*\*
 \* 猫猫
 \*
 \* @author 二当家的白帽子 https://le-yi.blog.csdn.net/
 \*/
public class Cat {
    /\*\*
 \* 发出声音
 \*/
    public void makeSound() {
        System.out.println("猫猫:喵喵喵。。。。。。");
    }
}

过了几天,二当家的和猫猫也成了朋友。这时候只好再多增加一个猫朋友类。

package com.secondgod.adapter;

/\*\*
 \* 猫猫朋友
 \*
 \* @author 二当家的白帽子 https://le-yi.blog.csdn.net/
 \*/
public class CatFriend extends Cat implements IFriend {
    /\*\*
 \* 说话了
 \*/
    @Override
    public void speak() {
        super.makeSound();
    }
}

二当家的和狗朋友,猫朋友聊天。

package com.secondgod.adapter;

/\*\*
 \* 人
 \*
 \* @author 二当家的白帽子 https://le-yi.blog.csdn.net/
 \*/
public class Person {
    /\*\*
 \* 和朋友聊天
 \*
 \* @param friend
 \*/
    public void speakTo(IFriend friend) {
        System.out.println("人:朋友,你干什么呢?");
        friend.speak();
    }

    public static void main(String[] args) {
        Person  person = new Person();
        IFriend dogFriend = new DogFriend();
        IFriend catFriend = new CatFriend();
        person.speakTo(dogFriend);
        person.speakTo(catFriend);
    }
}

在这里插入图片描述

以后要是二当家的再有其他动物朋友,就需要再去增加适配器类。有没有办法通用一点呢?


对象的适配器模式

二当家的希望可以有一个和各种动物做朋友的办法,而不是每次有了新的动物朋友都需要增加一个适配器。

在这里插入图片描述


增加一个动物接口

package com.secondgod.adapter;

/\*\*
 \* 动物
 \*
 \* @author 二当家的白帽子 https://le-yi.blog.csdn.net/
 \*/
public interface IAnimal {

    /\*\*
 \* 发出声音
 \*/
    void makeSound();
}


让源(Adapee)角色的猫猫和狗狗实现动物接口

package com.secondgod.adapter;

/\*\*
 \* 狗狗
 \*
 \* @author 二当家的白帽子 https://le-yi.blog.csdn.net/
 \*/
public class Dog implements IAnimal {
    /\*\*
 \* 发出声音
 \*/
    public void makeSound() {
        System.out.println("狗狗:汪汪汪。。。。。。");
    }
}

package com.secondgod.adapter;

/\*\*
 \* 猫猫
 \*
 \* @author 二当家的白帽子 https://le-yi.blog.csdn.net/
 \*/
public class Cat implements IAnimal {
    /\*\*
 \* 发出声音
 \*/
    public void makeSound() {
        System.out.println("猫猫:喵喵喵。。。。。。");
    }
}


万物拟人适配器(Adaper)角色

package com.secondgod.adapter;

/\*\*
 \* 万物拟人适配器
 \*
 \* @author 二当家的白帽子 https://le-yi.blog.csdn.net/
 \*/
public class AnimalFriendAdaper implements IFriend {
    /\*\*
 \* 被拟人化的动物朋友
 \*/
    private IAnimal animal;

    public AnimalFriendAdaper(IAnimal animal) {
        this.animal = animal;
    }

    @Override
    public void speak() {
        animal.makeSound();
    }
}

测试我们的万物拟人适配器。

package com.secondgod.adapter;

/\*\*
 \* 人
 \*
 \* @author 二当家的白帽子 https://le-yi.blog.csdn.net/
 \*/
public class Person {
    /\*\*
 \* 和朋友聊天
 \*
 \* @param friend
 \*/
    public void speakTo(IFriend friend) {
        System.out.println("人:朋友,你干什么呢?");
        friend.speak();
    }

    public static void main(String[] args) {
        // 一个人
        Person  person = new Person();
        // 一只狗
        IAnimal dog = new Dog();
        // 一只猫
        IAnimal cat = new Cat();
        // 万物拟人
        person.speakTo(new AnimalFriendAdaper(dog));
        person.speakTo(new AnimalFriendAdaper(cat));
    }
}

在这里插入图片描述

太好了。和动物做朋友轻松多了。因为有了万物拟人的适配器。


缺省适配模式

在这里插入图片描述


目标(Target)角色增加行为声明

有一天,朋友的标准变了。必须得会码砖才行。

package com.secondgod.adapter;

/\*\*
 \* 朋友
 \*
 \* @author 二当家的白帽子 https://le-yi.blog.csdn.net/
 \*/
public interface IFriend {
    /\*\*
 \* 说话
 \*/
    void speak();

    /\*\*
 \* 码起来
 \*/
    void coding();
}


适配器(Adaper)角色必须跟着增加行为实现

修改后的万物拟人适配器

package com.secondgod.adapter;

/\*\*
 \* 万物拟人适配器
 \*
 \* @author 二当家的白帽子 https://le-yi.blog.csdn.net/
 \*/
public class AnimalFriendAdaper implements IFriend {
    /\*\*
 \* 被拟人化的动物朋友
 \*/
    private IAnimal animal;

    public AnimalFriendAdaper(IAnimal animal) {
        this.animal = animal;
    }

    @Override
    public void speak() {
        animal.makeSound();
    }

    @Override
    public void coding() {
        System.out.println("动物:笑而不语摇摇头。。。。。。");
    }
}


缺省适配器

二当家的想和动物做朋友,但是不想去考虑他们如何码砖,以后二当家的要是和植物做朋友,还得为植物朋友也实现码砖行为,烦哦。所以我们来个默认空实现。

package com.secondgod.adapter;

/\*\*
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值