结构型设计模式-适配器 Adapter
date
Apr 13, 2021
slug
design-pattern-adapter
status
Published
tags
设计模式
summary
type
Page
简介
适配器模式是一种结构型设计模式, 它能使接口不兼容的对象能够相互合作
角色
- Client 接口 / Target 目标接口
用户使用的接口
- Adaptee
被适配的对象,原有方法不能直接给 client 使用
- Adapter
适配器,实现 Target 接口,将 Adaptee 的方法改造成兼容 client 的形式,供 client使用
类图
图示,Adapter 持有 Service 对象实例,method 方法实现自 Client Interface,使 client 可以调用,method 内部逻辑则将 client 的入参转换后交给 service 处理
![notion image](https://www.notion.so/image/https%3A%2F%2Fdeb-gmi.oss-cn-beijing.aliyuncs.com%2Fimg%2F20210408184306.png?table=block&id=39bf4728-54a4-412c-94a0-6d9ee877028e&cache=v2)
代码
output: