- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章java适配器模式之万物拟人化由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
以下是百科的解释.
在计算机编程中,适配器模式(有时候也称包装样式或者包装)将一个类的接口适配成用户所期待的。一个适配允许通常因为接口不兼容而不能在一起工作的类工作在一起,做法是将类自己的接口包裹在一个已存在的类中.
共有两类适配器模式:
- 类适配器模式:
这种适配器模式下,适配器继承自已实现的类(一般多重继承).
- 对象适配器模式:
在这种适配器模式中,适配器容纳一个它包裹的类的实例。在这种情况下,适配器调用被包裹对象.
设计模式和编程语言无关,但是二当家的依然用Java语言去实战举例.
- 源(Adapee)角色:现在需要适配的接口。
- 目标(Target)角色:这就是所期待得到的接口。注意:由于这里讨论的是类适配器模式,因此目标不可以是类。
- 适配器(Adaper)角色:适配器类是本模式的核心。适配器把源接口转换成目标接口。显然,这一角色不可以是接口,而必须是具体类。
二当家喜欢狗狗,所以养了一只狗狗,他有时候会发出叫声.
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(
"狗狗:汪汪汪。。。。。。"
);
}
}
|
我们会和朋友聊天说话.
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();
}
|
过了一段时间,二当家把狗狗当成了朋友,觉得它不是在叫,而是在说话.
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);
}
}
|
二当家的说一句,狗狗叫一声,我们真的像是在聊天.
有一天,二当家的又养了一只猫猫.
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);
}
}
|
以后要是二当家的再有其他动物朋友,就需要再去增加适配器类。有没有办法通用一点呢?
二当家的希望可以有一个和各种动物做朋友的办法,而不是每次有了新的动物朋友都需要增加一个适配器.
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();
}
|
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(
"猫猫:喵喵喵。。。。。。"
);
}
}
|
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));
}
}
|
太好了。和动物做朋友轻松多了。因为有了万物拟人的适配器.
有一天,朋友的标准变了。必须得会码砖才行.
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();
}
|
修改后的万物拟人适配器 。
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的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我有 3 个 AutoCompleteTextView,我想在它们上面注册 2 个 String[] 适配器。目前,我正在这样做: atw_from.setAdapter(new ArrayAdapt
我需要实现一个 recyclerView 来显示我对 Parse 的查询,所以我已经做到了: private class Pagination extends RecyclerView.OnScro
我对 BizTalk 相当陌生,目前我只是探索它的功能并了解不同部分(架构、编排、端口等)如何协同工作。我对其适配器有疑问: 不同的适配器是否已经随 BizTalk 服务器安装一起预装并准备好配置,或
我在 BizTalk 中测试 MQSC 适配器以与 Z/OS 主机上的队列通信时遇到问题。 测试场景:通过 Biztalk 发送消息时,我(强制)停止并启动主机 channel ,以模拟主机 IPL。
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我想用我的音频单元在iPhone上录制一条音频信号,该信号来自一条普通的3.5毫米音频电缆(例如,另一部iPhone作为声源)。 由于iPhone具有4端口耳机插孔,因此无法直接插入。 我尝试了不同种
[请参阅下面的更新] 我很难定义模式。我的同事说这是适配器模式。我不知道。我们陷入困境主要是因为我们想要正确命名我们的组件。 问题:是适配器模式吗?如果不是的话是什么?如果是其他事情,这是实现这个想法
我有点不熟悉Java KeyAdapter有效,并且使用 KeyAdapter 使用以下代码得到了意想不到的结果。当按下一个键而另一个键已按下时,就会出现此问题,无论 isKeyPressed() 是
我想知道如何通过 ORM 适配器使用 Node.js 在 MySQL 中创建多个表。我通过模型创建了一个表,即“us.js” module.exports = { identity: 'us'
我有一个 JavaFx 客户端。我正在使用一个具有 ObservableSet 作为字段的 bean 作为模型。我想将这些数据显示到 ListView 中,但我无法将我的字段类型更改为 Observa
我正在尝试在 native iOS 应用程序中实现基于表单的身份验证,但我需要在没有收到质询的情况下登录,我想打开一个表单并登录。我实现了一个包含 isCustomResponse 函数的 Chall
我正在尝试为我的迭代器和 const_iterator 类实现反向迭代器适配器,但遇到了一些麻烦。如果有人可以指导我解决这个问题,将不胜感激! 我的想法是我应该能够从我的 rbegin() 和 ren
使用 spring-integration-sftp,创建任意数量的入站 channel 适配器对象的推荐方法是什么?我的应用程序需要监视多个远程目录(1 到 n),直到运行时才知道。 最佳答案 当前
我正在尝试为我们自己的框架创建适配器。我们的框架使用自己的断言机制,因此我需要编写适配器。 适配器类非常简单,如下所示: public class AllureReportListener {
有没有什么方法可以使用命令行而不是使用 Worklight 控制台来部署 Worklight 适配器? (因为我的 worklight 服务器安装在 WAS 上,wsadmin 命令或类似的命令...
我想构建自己的自定义 log4j(网络)适配器来解决我的问题 that I posted here. 我查看了 log4j 的文档,但看不到开发人员在哪里/是否讨论如何执行此操作。 有人能给我指出正确
我使用消息驱动 channel 适配器从 weblogic JMS 队列接收作为字符串的 xml 消息,然后将此消息传递到 spring 集成 channel 以存储到数据库中,转换为不同的 xml,
有没有什么方法可以使用命令行而不是使用 Worklight 控制台来部署 Worklight 适配器? (因为我的 worklight 服务器安装在 WAS 上,wsadmin 命令或类似的命令...
我试图为 Android 制作一个聊天应用程序,所以我使用了 RecyclerView 。我的适配器有问题,我的聊天室收到的 JSON 响应显示为空白。我的代码是否遗漏了某些内容? 这是我的适配器类
如果这是重复的,我提前道歉。我对 Android 开发还是新手,并尝试寻找解决方案,但找不到有效的解决方案。 我正在创建一个待办事项应用程序并在我的适配器中收到此错误。 java.lang.NullP
我是一名优秀的程序员,十分优秀!