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

大话设计模式之中介者模式

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

中介者模式

  用一个中介对象来封装一系列对象的交互。中介者使各个对象不需要显示的相互引用,从而使其耦合松散,而且可以独立的改变他们之间的交互。

84_1.png

涉及到的角色描述

  – Mediator:抽象中介者角色,定义了同事对象到中介者对象的接口。
  – ConcreteMediator:具体中介者角色,它从具体的同事对象接收消息,向具体同事发出命令。
  – Colleague:抽象同事角色,定义了中介者对象接口,它只知道中介者而不知道其他同事对象。
  – ConcreteColleague:具体同事角色,每个具体同事类都知道自己在小范围内的行为,而不知道它在大范围内的目的。

优点

  1、降低了类的复杂度,将一对多转化成了一对一。

  2、各个类之间的解耦。

  3、符合迪米特原则。

缺点

  中介者会庞大,变得复杂难以维护。

使用场景

  1、系统中对象之间存在比较复杂的引用关系,导致它们之间的依赖关系结构混乱而且难以复用该对象。

  2、想通过一个中间类来封装多个类中的行为,而又不想生成太多的子类。

具体代码实现

  先定义抽象中介Mediator

package com.chenpt.designModel.mediatorModel;

/**
 * @Author: chen
 * @Description: 中介者接口
 * @Date: created in 2018/8/28
 * @Modified By:
 */
public interface UniteNations {

    void declare(String message,Country country);

}

  抽象同事类Colleague

package com.chenpt.designModel.mediatorModel;

/**
 * @Author: chen
 * @Description: 抽象国家类持有一个中介者对象
 * @Date: created in 2018/8/28
 * @Modified By:
 */
public abstract class Country {

    UniteNations uniteNations;

    Country(UniteNations uniteNations){
        this.uniteNations = uniteNations;
    }

}

  具体同事1ConcreteColleague1

package com.chenpt.designModel.mediatorModel;

/**
 * @Author: chen
 * @Description:
 * @Date: created in 2018/8/28
 * @Modified By:
 */
public class USA extends Country {
    USA(UniteNations uniteNations) {
        super(uniteNations);
    }

    public void declare(String message){
        uniteNations.declare(message,this);
    }

    public void getMessage(String message){
        System.out.println("美国获得对方消息:"+message);
    }
}

  具体同事1ConcreteColleague2

package com.chenpt.designModel.mediatorModel;

/**
 * @Author: chen
 * @Description:
 * @Date: created in 2018/8/28
 * @Modified By:
 */
public class Iraq extends Country {
    Iraq(UniteNations uniteNations) {
        super(uniteNations);
    }

    public void declare(String message){
        uniteNations.declare(message,this);
    }

    public void getMessage(String message){
        System.out.println("伊拉克获得对方消息:"+message);
    }
}

  具体中介者

package com.chenpt.designModel.mediatorModel;

/**
 * @Author: chen
 * @Description: 具体中介者
 * @Date: created in 2018/8/28
 * @Modified By:
 */
public class UniteNationsSecurity implements UniteNations {

    private USA usa;
    private Iraq iraq;

    public void setUSA(USA usa){
        this.usa=usa;
    }

    public void setIraq(Iraq iraq){
        this.iraq=iraq;
    }


    @Override
    public void declare(String message, Country country) {
        if(country==usa){
            iraq.getMessage(message);
        }else if(country==iraq){
            usa.getMessage(message);
        }
    }
}

  客户端

package com.chenpt.designModel.mediatorModel;

/**
 * @Author: chen
 * @Description:
 * @Date: created in 2018/8/28
 * @Modified By:
 */
public class MainTest {

    public static void main(String[] as){

        UniteNationsSecurity security = new UniteNationsSecurity();

        USA usa = new USA(security);
        Iraq iraq = new Iraq(security);

        security.setUSA(usa);
        security.setIraq(iraq);

        usa.declare("不准研制核武器,否则发动战争");
        iraq.declare("我们没有核武器,也不怕侵略");

    }


}
//执行结果
伊拉克获得对方消息:不准研制核武器,否则发动战争
美国获得对方消息:我们没有核武器,也不怕侵略

  

#

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


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



未经允许不得转载:搜云库技术团队 » 大话设计模式之中介者模式

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