当前位置:网站首页>Adapter mode
Adapter mode
2022-06-26 05:46:00 【Green water monster 12138】
When is the adapter mode
Adapter pattern (Adapter): Convert the interface of a class into another interface that the client wants .Adapter Patterns enable classes that cannot work together because of interface incompatibility to work together .
UML

Adapter patterns are divided into object adapters and class adapters , Object adapters implement adaptation functions by composing aggregations , Class adapters implement functions by inheriting target classes and working classes .
Example code
package adapter;
public class test {
public static void main(String[] args) {
Target a=new Adapter();
a.request();
}
}
package adapter;
public abstract class Target {
public abstract void request();
}
package adapter;
public class Adapter extends Target{
private Adaptee adaptee;
public Adapter() {
// TODO Auto-generated constructor stub
adaptee=new Adaptee();
}
@Override
public void request() {
// TODO Auto-generated method stub
adaptee.request();
}
}
package adapter;
public class Adaptee {
public void request(){
System.out.println(" Real request !");
}
}
The following is from this blog
Use scenarios
1、 When you want to use a class , But when its interface is not compatible with other code , You can use adapter classes .
The adapter pattern allows you to create a middle tier class , It can be used as code and legacy classes 、 Converters between third-party classes or classes that provide weird interfaces .
2、 If you need to reuse such classes , They are in the same inheritance system , And they have some extra common methods , But these common methods are not common to all subclasses in this inheritance system .
You can extend each subclass , Add missing functionality to new subclasses . however , You have to add this code repeatedly in all new subclasses , This will make the code smell bad .
Adding missing functionality to an adapter class is a much more elegant solution . Then you can encapsulate the missing objects and functions in the adapter , Add adapter objects to this inheritance system , So as to dynamically obtain the required functions . If this is to work properly , The target class must have a common interface , The member variables of the adapter should follow this common interface . This way is the same as Decoration mode Very similar .
The relationship with other design patterns
The bridge mode is usually designed in the early stage of development , Enables you to separate parts of the program for development . On the other hand , Adapter mode is usually used in existing programs , Let incompatible classes cooperate well .
The adapter can modify the interface of existing objects , Decoration mode It can strengthen the object function without changing the object interface . Besides , Decoration also supports recursive composition , The adapter cannot achieve .
Adapters can provide different interfaces for encapsulated objects , Proxy mode can provide the same interface for objects , Decoration can provide an enhanced interface for objects .
Appearance mode Defines a new interface for an existing object , The adapter will try to use the existing interface . Adapters typically encapsulate only one object , Appearances usually act on the entire object subsystem .
The bridge 、 The state pattern And strategy patterns ( To some extent, it includes adapters ) The interface of the pattern is very similar . actually , They are all based on Portfolio model —— Delegate work to other objects , But they also solved different problems . Patterns are not just recipes for organizing code in a particular way , You can also use them to discuss problems solved by patterns with other developers .
边栏推荐
猜你喜欢
从新东方直播来探究下小程序音视频通话及互动直播

pytorch(环境、tensorboard、transforms、torchvision、dataloader)

冒泡排序(Bubble Sort)

pytorch(网络模型)

原型模式,咩咩乱叫

Red team scoring method statistics

Installation and deployment of alluxio

Gram 矩阵

Supplementary course on basic knowledge of IM development (II): how to design a server-side storage architecture for a large number of image files?

When was the autowiredannotationbeanpostprocessor instantiated?
随机推荐
写在父亲节前
[arm] add desktop application for buildreoot of rk3568 development board
Uni app ceiling fixed style
423-二叉树(110. 平衡二叉树、257. 二叉树的所有路径、100. 相同的树、404. 左叶子之和)
[upsampling method opencv interpolation]
国务院发文,完善身份认证、电子印章等应用,加强数字政府建设
使用Jedis監聽Redis Stream 實現消息隊列功能
[arm] build boa based embedded web server on nuc977
转帖——不要迷失在技术的海洋中
pytorch(网络模型训练)
The use of loops in SQL syntax
Sql语法中循环的使用
机器学习 07:PCA 及其 sklearn 源码解读
REUSE_ ALV_ GRID_ Display event implementation (data_changed)
Kolla ansible deploy openstack Yoga version
Pre-Sale Analysis
旧情书
Leetcode513. Find the value in the lower left corner of the tree
Recursively traverse directory structure and tree presentation
怎么把平板作为电脑的第二扩展屏幕