IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

设计模式之—适配器模式

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

1.适配器模式的定义

Adapter Pattern:Convert the interface of a class into another interface clients expect.Adapter lets classes work together that couldn’t otherwise because of incompatible interface.

将一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够一起工作。

2.通用类图

适配器模式中有三个主要对象。

Target:

转换成的目标接口,即将其他类转换为此Target接口

Adaptee:

被转换的类

Adapter:

适配器,通过实现Target接口并继承Adaptee类,并重写Target接口方法来实现适配

类图如下: 76_1.png

实现代码如下: Target类:

public interface Target {

    /**
     * request
     */
    void request();
}

Adaptee类:

public class Adaptee {

    public void doSomething(){
        System.out.println("Do something!");
    }
}

Adapter类:

public class Adapter extends Adaptee implements Target {

    @Override
    public void request() {
        doSomething();
    }
}

Client类:

public class Client {

    public static void main(String[] args) throws Exception {
        Target target = new Adapter();
        target.request();
    }
}

3.优点

1、可以使两个无关联的类一起运行 2.灵活性好,当不需要此适配器的时候,只需要将此类摘除掉即可

4.总结

适配器模式通常用来解决在功能变动时,解决接口不相容的问题,在系统设计阶段不应该使用此中模式

文章永久链接:https://tech.souyunku.com/?p=32243


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(73) 打赏



未经允许不得转载:搜云库技术团队 » 设计模式之—适配器模式

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367