gpt4 book ai didi

java适配器模式之万物拟人化

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 26 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章java适配器模式之万物拟人化由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

什么是适配器模式

以下是百科的解释.

在计算机编程中,适配器模式(有时候也称包装样式或者包装)将一个类的接口适配成用户所期待的。一个适配允许通常因为接口不兼容而不能在一起工作的类工作在一起,做法是将类自己的接口包裹在一个已存在的类中.

共有两类适配器模式:

  • 类适配器模式:

这种适配器模式下,适配器继承自已实现的类(一般多重继承).

  • 对象适配器模式:

在这种适配器模式中,适配器容纳一个它包裹的类的实例。在这种情况下,适配器调用被包裹对象.

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


类的适配器模式

java适配器模式之万物拟人化

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

源(Adapee)角色

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.secondgod.adapter;
 
/**
  * 狗狗
  *
  * @author 二当家的白帽子 https://le-yi.blog.csdn.net/
  */
public class Dog {
     /**
      * 发出声音
      */
     public void makeSound() {
         System.out.println( "狗狗:汪汪汪。。。。。。" );
     }
}

目标(Target)角色

我们会和朋友聊天说话.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.secondgod.adapter;
 
/**
  * 朋友
  *
  * @author 二当家的白帽子 https://le-yi.blog.csdn.net/
  */
public interface IFriend {
     /**
      * 说话
      */
     void speak();
}

适配器(Adaper)角色

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.secondgod.adapter;
 
/**
  * 狗狗朋友
  *
  * @author 二当家的白帽子 https://le-yi.blog.csdn.net/
  */
public class DogFriend extends Dog implements IFriend {
     /**
      * 说话了
      */
     @Override
     public void speak() {
         super .makeSound();
     }
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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);
     }
}

java适配器模式之万物拟人化

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


增加源(Adapee)角色的后果

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.secondgod.adapter;
 
/**
  * 猫猫
  *
  * @author 二当家的白帽子 https://le-yi.blog.csdn.net/
  */
public class Cat {
     /**
      * 发出声音
      */
     public void makeSound() {
         System.out.println( "猫猫:喵喵喵。。。。。。" );
     }
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.secondgod.adapter;
 
/**
  * 猫猫朋友
  *
  * @author 二当家的白帽子 https://le-yi.blog.csdn.net/
  */
public class CatFriend extends Cat implements IFriend {
     /**
      * 说话了
      */
     @Override
     public void speak() {
         super .makeSound();
     }
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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);
     }
}

java适配器模式之万物拟人化

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


对象的适配器模式

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

java适配器模式之万物拟人化


增加一个动物接口

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.secondgod.adapter;
 
/**
  * 动物
  *
  * @author 二当家的白帽子 https://le-yi.blog.csdn.net/
  */
public interface IAnimal {
 
     /**
      * 发出声音
      */
     void makeSound();
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.secondgod.adapter;
 
/**
  * 狗狗
  *
  * @author 二当家的白帽子 https://le-yi.blog.csdn.net/
  */
public class Dog implements IAnimal {
     /**
      * 发出声音
      */
     public void makeSound() {
         System.out.println( "狗狗:汪汪汪。。。。。。" );
     }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.secondgod.adapter;
 
/**
  * 猫猫
  *
  * @author 二当家的白帽子 https://le-yi.blog.csdn.net/
  */
public class Cat implements IAnimal {
     /**
      * 发出声音
      */
     public void makeSound() {
         System.out.println( "猫猫:喵喵喵。。。。。。" );
     }
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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();
     }
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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));
     }
}

java适配器模式之万物拟人化

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


缺省适配模式

java适配器模式之万物拟人化


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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.secondgod.adapter;
 
/**
  * 朋友
  *
  * @author 二当家的白帽子 https://le-yi.blog.csdn.net/
  */
public interface IFriend {
     /**
      * 说话
      */
     void speak();
 
     /**
      * 码起来
      */
     void coding();
}

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

修改后的万物拟人适配器 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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( "动物:笑而不语摇摇头。。。。。。" );
     }
}

缺省适配器

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.secondgod.adapter;
 
/**
  * 缺省适配器
  *
  * @author 二当家的白帽子 https://le-yi.blog.csdn.net/
  */
public abstract class FriendAdaper implements IFriend {
     @Override
     public void speak() {
 
     }
 
     @Override
     public void coding() {
 
     }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.secondgod.adapter;
 
/**
  * 万物拟人适配器
  *
  * @author 二当家的白帽子 https://le-yi.blog.csdn.net/
  */
public class AnimalFriendAdaper extends FriendAdaper {
     /**
      * 被拟人化的动物朋友
      */
     private IAnimal animal;
 
     public AnimalFriendAdaper(IAnimal animal) {
         this .animal = animal;
     }
 
     @Override
     public void speak() {
         animal.makeSound();
     }
}

由于多了一个默认实现,我们就不需要为万物适配器实现码砖行为了.

适配器模式的用意是要改变源的接口,以便于目标接口相容。缺省适配的用意稍有不同,它是为了方便建立一个不平庸的适配器类而提供的一种平庸实现.

在任何时候,如果不准备实现一个接口的所有方法时,就可以使用“缺省适配模式”制造一个抽象类,给出所有方法的平庸的具体实现。这样,从这个抽象类再继承下去的子类就不必实现所有的方法了.

到此这篇关于java适配器模式之万物拟人化的文章就介绍到这了,更多相关java适配器模式内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。

原文链接:https://blog.csdn.net/leyi520/article/details/119730567 。

最后此篇关于java适配器模式之万物拟人化的文章就讲到这里了,如果你想了解更多关于java适配器模式之万物拟人化的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com