【GOF23设计模式】_中介者模式_同事协作类_内部类实现JAVA245

本文通过一个具体的例子介绍了中介者模式的实现方式。该模式通过引入一个中介者对象来协调同事对象之间的交互,使得各个同事对象不需要直接相互引用,降低系统的耦合度。文章展示了如何使用中介者模式来组织不同部门间的沟通。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

来源:http://www.bjsxt.com/
一、S03E245_01【GOF23设计模式】_中介者模式、同事协作类、内部类实现

场景

核心

中介者模式类图

package com.test.mediator;
/**
 * 同事类的接口
 */
public interface Department {
    void selfAction();//做本部门的事情
    void outAction();//向总经理发出申请
}
package com.test.mediator;

public class Development implements Department{
    private Mediator m;//持有中介者(总经理)的引用

    public Development(Mediator m) {
        super();
        this.m = m;
        m.register("development", this);
    }

    @Override
    public void selfAction() {
        System.out.println(getClass().getName()+"专心科研,开发项目!");
    }

    @Override
    public void outAction() {
        System.out.println(getClass().getName()+"汇报工作!没钱了,需要资金支持!");
    }
}
package com.test.mediator;

public class Finacial implements Department{
    private Mediator m;//持有中介者(总经理)的引用

    public Finacial(Mediator m) {
        super();
        this.m = m;
        m.register("finacial", this);
    }

    @Override
    public void selfAction() {
        System.out.println(getClass().getName()+"数钱!");
    }

    @Override
    public void outAction() {
        System.out.println(getClass().getName()+"汇报工作!没钱了,钱太多了!怎么花?");
    }
}
package com.test.mediator;

public class Market implements Department{
    private Mediator m;//持有中介者(总经理)的引用

    public Market(Mediator m) {
        super();
        this.m = m;
        m.register("market", this);
    }

    @Override
    public void selfAction() {
        System.out.println(getClass().getName()+"跑去接项目!");
    }

    @Override
    public void outAction() {
        System.out.println(getClass().getName()+"汇报工作!项目承接的进度,需要资金支持!");

        m.command("finacial");
    }
}
package com.test.mediator;

public interface Mediator {
    void register(String dname,Department d);
    void command(String dname);
}
package com.test.mediator;

import java.util.HashMap;
import java.util.Map;

public class President implements Mediator{
    private Map<String, Department> map = new HashMap<String, Department>();

    @Override
    public void register(String dname, Department d) {
        map.put(dname, d);
    }

    @Override
    public void command(String dname) {
        map.get(dname).selfAction();
    }
}
package com.test.mediator;

public class Client {
    public static void main(String[] args) {
        Mediator m = new President();

        Market market = new Market(m);
        Development devp = new Development(m);
        Finacial f = new Finacial(m);

        market.selfAction();
        market.outAction();
    }
}
控制台输出:
com.test.mediator.Market跑去接项目!
com.test.mediator.Market汇报工作!项目承接的进度,需要资金支持!
com.test.mediator.Finacial数钱!

课堂代码类图

中介者模式的本质

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值