Python 官方文档:入门教程 => 点击学习
目录基本介绍工作原理类适配器模式对象适配器模式接口适配器模式适配器模式的注意事项和细节基本介绍 适配器模式(Adapter Pattern)将某个类的接口转换成客户端期望的另一个接口
背景介绍:在中国电源大都是220伏的,而日常生活中手机的充电确是5伏1安(苹果),通过类适配器模拟电源适配器实现手机充电功能
Voltage220V(被适配电源)
public class Voltage220V {
public int output220V() {
int src = 220;
System.out.println("电压 = " + src + "伏");
return src;
}
}
Voltage5V(目标输出)
public interface Voltage5V {
int output5V();
}
VoltageAdapter(电源适配器)
public class VoltageAdapter extends Voltage220V implements Voltage5V {
@Override
public int output5V() {
int src = output220V();
int dst = src / 44;
System.out.printf("适配器将%d伏====>%d伏\n", src, dst);
return dst;
}
}
Phone(手机充电)
public class Phone {
public void charging(Voltage5V v) {
int src = v.output5V();
if (src <= 5 && src > 0) {
System.out.println("电压 = " + src + "伏,可以充电...");
} else if (src > 5) {
System.out.println("电压过高");
} else {
System.out.println("没有充电...");
}
}
}
Client
public class Client {
public static void main(String[] args) {
System.out.println("====类适配器模式====");
Phone phone = new Phone();
phone.charging(new VoltageAdapter());
}
}
实现结果:
类适配器模式注意事项和细节
优点:
UML类图:
其他代码与上一致,修改电源适配器即可:
public class VoltageAdapter implements Voltage5V {
private Voltage220V voltage220V = null;
public VoltageAdapter(Voltage220V voltage220V) {
this.voltage220V = voltage220V;
}
@Override
public int output5V() {
int src = 0;
if (voltage220V != null) {
src = voltage220V.output220V();
}
int dst = src / 44;
System.out.printf("适配器将%d伏====>%d伏\n",src,dst);
return dst;
}
}
Client(测试)
public class Client {
public static void main(String[] args) {
System.out.println("====对象适配器模式====");
Phone phone = new Phone();
phone.charging(new VoltageAdapter(new Voltage220V()));
System.out.println("--------------------");
phone.charging(new VoltageAdapter(null));
}
}
实现结果:
简介:
UML类图
接口适配器和实现类
///定义一个Adapter接口
public interface HandlerAdapter {
public boolean support(Controller handler);
public void handle(Controller handler);
}
// 多种适配器类
class SimpleHandlerAdapter implements HandlerAdapter {
public void handle(Controller handler) {
handler.doHandler();
}
public boolean support(Controller handler) {
return (handler instanceof SimpleController);
}
}
class HttpHandlerAdapter implements HandlerAdapter {
public void handle(Controller handler) {
handler.doHandler();
}
public boolean support(Controller handler) {
return (handler instanceof HttpController);
}
}
class AnnotationHandlerAdapter implements HandlerAdapter {
public void handle(Controller handler) {
handler.doHandler();
}
public boolean support(Controller handler) {
return (handler instanceof AnnotationController);
}
}
模拟Controller
//多种Controller实现
public interface Controller {
void doHandler();
}
class HttpController implements Controller {
@Override
public void doHandler() {
System.out.println("http...");
}
}
class SimpleController implements Controller {
@Override
public void doHandler() {
System.out.println("simple...");
}
}
class AnnotationController implements Controller {
@Override
public void doHandler() {
System.out.println("annotation...");
}
}
模拟DispatchServlet
public class DispatchServlet {
public static List<HandlerAdapter> handlerAdapters = new ArrayList<HandlerAdapter>();
public DispatchServlet() {
handlerAdapters.add(new AnnotationHandlerAdapter());
handlerAdapters.add(new HttpHandlerAdapter());
handlerAdapters.add(new SimpleHandlerAdapter());
}
public void doDispatch(Controller controller) {
// 得到对应适配器
HandlerAdapter adapter = getHandler(controller);
// 通过适配器执行对应的controller对应方法
if (adapter != null) {
adapter.handle(controller);
} else {
System.out.println("没有该适配器...");
}
}
public HandlerAdapter getHandler(Controller controller) {
//遍历:根据得到的controller(handler), 返回对应适配器
for (HandlerAdapter adapter : this.handlerAdapters) {
if (adapter.support(controller)) {
return adapter;
}
}
return null;
}
public static void main(String[] args) {
new DispatchServlet().doDispatch(new SimpleController() );
new DispatchServlet().doDispatch(new HttpController() );
new DispatchServlet().doDispatch(new AnnotationController());
}
}
实现结果:
到此这篇关于Java设计模式中的适配器模式的文章就介绍到这了,更多相关Java适配器模式内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Java设计模式中的适配器模式
本文链接: https://www.lsjlt.com/news/164580.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0