自学内容网 自学内容网

Java实现中介者模式

一、简介

1、定义

  中介者模式(Mediator Pattern)是一种行为设计模式,它允许不同对象之间通过一个中介对象进行通信,从而减少不同对象之间的直接交互。通过将对象间的交互集中在中介者中,中介者可以集中处理复杂的交互逻辑,从而使程序降低耦合、易于维护。

2、中介者模式的结构

中介者模式涉及以下几个角色:
中介者 (Mediator)接口:定义了中介者接口,用于不同对象之间的通信。
具体中介者 (ConcreteMediator)类:实现中介者接口的方法,协调不同对象的信息交互。
抽象组件 (Component)类:持有中介者引用,处理消息接口
具体组件 (ConcreteComponent)类:实现抽象组件类定义的方法,当需要和其它组件对象通信时,将请求发送给中介者对象。
客户端(Client)类:初始化中介者、组件类,协调组件之间的通讯。

二、Java实现案例

1、组件间通讯案例(组件固定,没用反射实现)

中介接口:包含通知抽象方法
具体中介类:持有不同组件引用 根据组件类型判断对应组件处理相应事件
组件抽象类:持有中介接口引用、处理事件抽象方法
具体组件A、具体组件B:通过触发事件方法将自己传给中介者,并调用中介者通知方法
客户端:初始化中介者、组件类。通过A组件触发事件,通知B组件;通过B组件触发事件,通知A组件。
说明:中介者一个,组件只有固定两个。组件包含中介者的引用、中介者也包含组件的引用,组件中有接受通知的方法。

/**
 * @Description: 中介者接口
 * @Date: 2025-01-14 8:46
 * @Author: gaoyufei
 **/
public interface Mediator {
    void notify(Component sender,String event);
}

/**
 * @Description: 具体中介者
 * @Date: 2025-01-14 8:52
 * @Author: gaoyufei
 **/
public class ConcreteMediator implements Mediator{
    private ComponentA componentA;
    private ComponentB componentB;
    public void setComponentA(ComponentA componentA){
        this.componentA=componentA;
    }
    public void setComponentB(ComponentB componentB){
        this.componentB=componentB;
    }
    @Override
    public void notify(Component sender, String event) {
        if(sender==componentA){
            componentB.handleEvent(event);
        }else if(sender == componentB){
            componentA.handleEvent(event);
        }
    }
}

/**
 * @Description: 组件接口
 * @Date: 2025-01-14 8:47
 * @Author: gaoyufei
 **/
public abstract class Component {
    protected Mediator mediator;
    public Component(Mediator mediator){
        this.mediator=mediator;
    }
    public abstract void handleEvent(String event);
}

/**
 * @Description: 具体组件A
 * @Date: 2025-01-14 8:49
 * @Author: gaoyufei
 **/
public class ComponentA extends Component{
    public ComponentA(Mediator mediator) {
        super(mediator);
    }
    @Override
    public void handleEvent(String event) {
        System.out.println("ComponentA handling enernt:"+event);
    }
    public void triggerEvent(String event){
        mediator.notify(this,event);
    }
}

/**
 * @Description: 具体组件B
 * @Date: 2025-01-14 8:49
 * @Author: gaoyufei
 **/
public class ComponentB extends Component{
    public ComponentB(Mediator mediator) {
        super(mediator);
    }
    @Override
    public void handleEvent(String event) {
        System.out.println("ComponentB handling enernt:"+event);
    }
    public void triggerEvent(String event){
        mediator.notify(this,event);
    }
}

/**
 * @Description: 客户端代码
 * @Date: 2025-01-14 8:56
 * @Author: gaoyufei
 **/
public class Client {
    public static void main(String[] args) {
        ConcreteMediator mediator=new ConcreteMediator();
        ComponentA componentA=new ComponentA(mediator);
        ComponentB componentB=new ComponentB(mediator);
        mediator.setComponentA(componentA);
        mediator.setComponentB(componentB);
        componentA.triggerEvent("Event A");
        componentB.triggerEvent("Event B");
    }
}

2、组件间通讯案例(组件可以多个,使用反射实现)

中介接口:包含通知抽象方法
具体中介类:具体通知方法,根据入参的组件Class反射实例化组件对象,调用组件对象的处理事件方法
组件抽象类:持有中介接口引用、处理事件抽象方法
具体组件A、具体组件B、具体组件C:通过触发事件方法调用中介者,拥有自身处理事件的方法
客户端:初始化中介者、组件类,通过A组件触发事件,通知B组件;通过B组件触发事件,通知C组件;通过C组件触发事件,通知A组件。
说明:中介者只有一个,组件可以多个。组件包含中介者的引用、中介者通过反射调用组件处理事件的方法

import java.lang.reflect.InvocationTargetException;

/**
 * @Description: 类描述
 * @Date: 2025-01-14 9:45
 * @Author: gaoyufei
 **/
public interface Mediator {
    void notify(Class cls,String event) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException;
}

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @Description: 具体中介者
 * @Date: 2025-01-14 9:46
 * @Author: gaoyufei
 **/
public class ConcreteMediator implements Mediator{
    @Override
    public void notify(Class cls,String event) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        // 获取带有参数Mediator的构造方法
        Constructor<?> constructor = cls.getConstructor(Mediator.class);
        // 通过构造方法实例化组件对象
        Object componentInstance = constructor.newInstance(this);
        // 调用组件对象的方法handleEvent,传入event参数
        cls.getMethod("handleEvent",String.class).invoke(componentInstance,event);
    }
}

/**
 * @Description: 组件
 * @Date: 2025-01-14 9:47
 * @Author: gaoyufei
 **/
public abstract class Component {
    public Mediator mediator;
    public Component(Mediator mediator){
        this.mediator=mediator;
    }
    public abstract void handleEvent(String event);
}

import java.lang.reflect.InvocationTargetException;

/**
 * @Description: 组件A
 * @Date: 2025-01-14 9:49
 * @Author: gaoyufei
 **/
public class ComponentA extends Component {
    public ComponentA(Mediator mediator) {
        super(mediator);
    }
    public void handleEvent(String event){
        System.out.println("ComponentA handling:"+event);
    }
    public void triggerEvent(Class cls,String event) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        mediator.notify(cls,event);
    }
}

import java.lang.reflect.InvocationTargetException;

/**
 * @Description: 组件B
 * @Date: 2025-01-14 9:49
 * @Author: gaoyufei
 **/
public class ComponentB extends Component {
    public ComponentB(Mediator mediator) {
        super(mediator);
    }
    public void handleEvent(String event){
        System.out.println("ComponentB handling:"+event);
    }
    public void triggerEvent(Class cls,String event) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        mediator.notify(cls,event);
    }
}

import java.lang.reflect.InvocationTargetException;

/**
 * @Description: 组件C
 * @Date: 2025-01-14 9:49
 * @Author: gaoyufei
 **/
public class ComponentC extends Component {
    public ComponentC(Mediator mediator) {
        super(mediator);
    }
    public void handleEvent(String event){
        System.out.println("ComponentC handling:"+event);
    }
    public void triggerEvent(Class cls,String event) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        mediator.notify(cls,event);
    }
}

import java.lang.reflect.InvocationTargetException;

/**
 * @Description: 客户端
 * @Date: 2025-01-14 10:13
 * @Author: gaoyufei
 **/
public class Client {
    public static void main(String[] args) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        ConcreteMediator concreteMediator=new ConcreteMediator();
        ComponentA componentA=new ComponentA(concreteMediator);
        ComponentB componentB=new ComponentB(concreteMediator);
        ComponentC componentC=new ComponentC(concreteMediator);
        componentA.triggerEvent(ComponentB.class,"Event B");
        componentB.triggerEvent(ComponentC.class,"Event C");
        componentC.triggerEvent(ComponentA.class,"Event A");
    }
}

3、同事间通知案例

中介接口:包含发送消息抽象方法
具体中介类:具体发送消息方法,根据入参同事Class的反射方法实例化同事对象,调用该同事的接受信息方法
组件接口=》同事接口:持有中介接口引用、接受消息抽象方法
具体组件=》具体同事A、具体同事B:通过发送信息方调用中介者,拥有自己接受信息的方法
客户端:初始化中介者、组件类,同事A发送消息,通过中介者,同事B接受消息;同事B发送消息,通过中介者,同事A接受消息;
说明:中介者只有一个,同事可以有多个。同事包含中介者的引用、中介者通过反射调用同事,实现对应同事接受信息的功能

import java.lang.reflect.InvocationTargetException;

/**
 * @Description: 中介接口
 * @Date: 2025-01-14 10:34
 * @Author: gaoyufei
 **/
public interface Mediator {
     void sendMsg(Class receiver,String msg) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException;
}

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @Description: 具体中介实现
 * @Date: 2025-01-14 10:34
 * @Author: gaoyufei
 **/
public class ConcreteMediator implements Mediator{
    @Override
    public void sendMsg(Class receiver, String msg) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        // 获取带有参数Mediator的构造方法
        Constructor<?> constructor = receiver.getConstructor(Mediator.class);
        // 通过构造方法实例化组件对象
        Object componentInstance = constructor.newInstance(this);
        // 调用组件对象的方法handleEvent,传入event参数
        receiver.getMethod("receiveMsg",String.class).invoke(componentInstance,msg);
    }
}

import java.lang.reflect.InvocationTargetException;

/**
 * @Description: 同事接口
 * @Date: 2025-01-14 10:35
 * @Author: gaoyufei
 **/
public abstract class Colleague {
    public Mediator mediator;
    public Colleague(Mediator mediator){
        this.mediator=mediator;
    }
    public abstract void receiveMsg(String msg);
    public abstract void sendMsg(Class receiver,String msg) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException;
}

import java.lang.reflect.InvocationTargetException;

/**
 * @Description: 同事A
 * @Date: 2025-01-14 10:36
 * @Author: gaoyufei
 **/
public class ColleagueA extends Colleague {
    public ColleagueA(Mediator mediator) {
        super(mediator);
    }
    @Override
    public void receiveMsg(String msg) {
        System.out.println("ColleagueA receiveMsg:"+msg);
    }
    @Override
    public void sendMsg(Class receiver,String msg) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        this.mediator.sendMsg(receiver,msg);
    }
}

import java.lang.reflect.InvocationTargetException;

/**
 * @Description: 同事B
 * @Date: 2025-01-14 10:36
 * @Author: gaoyufei
 **/
public class ColleagueB extends Colleague {
    public ColleagueB(Mediator mediator) {
        super(mediator);
    }
    @Override
    public void receiveMsg(String msg) {
        System.out.println("ColleagueB receiveMsg:"+msg);
    }
    @Override
    public void sendMsg(Class receiver,String msg) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        this.mediator.sendMsg(receiver,msg);
    }
}

import java.lang.reflect.InvocationTargetException;

/**
 * @Description: 客户端
 * @Date: 2025-01-14 10:41
 * @Author: gaoyufei
 **/
public class Client {
    public static void main(String[] args) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        ConcreteMediator concreteMediator=new ConcreteMediator();
        ColleagueA colleagueA=new ColleagueA(concreteMediator);
        ColleagueB colleagueB=new ColleagueB(concreteMediator);
        colleagueA.sendMsg(ColleagueB.class,"hello ColleagueB");//同事A通过中介给同事B发消息
        colleagueB.sendMsg(ColleagueA.class,"hello ColleagueA");//同事B通过中介给同事A发消息
    }
}

原文地址:https://blog.csdn.net/m0_37630138/article/details/145181286

免责声明:本站文章内容转载自网络资源,如侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!