Java mediator pattern

Intermediary model

Overview

  • The intermediary model is a kind of behavioral model.
  • Using an intermediary object to encapsulate a series of object interactions is in line with Dimit's principle, turning the original one-to-many relationship into a one-to-one relationship, reducing the coupling between classes.

Character introduction

  • Abstract mediator class: defines the interface from colleague objects to mediator objects.
  • Specific mediator class: inherit the abstract mediator, implement the methods defined by the parent class, receive messages from specific colleagues, and send messages to specific colleagues.
  • Abstract colleague class: defines the interface of the intermediary object.
  • Specific colleague class: inherit and abstract colleague class, send messages to the intermediary object, and receive messages from the intermediary object.

Insert picture description here

Basic realization

Some time ago the disciples of the Wudang and Emei factions were killed by the Dali Jingangzhi. The Dali Jingangzhi refers to the Shaolin school’s peculiar learning. Because the matter is serious and the Shaolin school is powerful, the Wudang and Emei schools cannot directly go to the Shaolin school to inquire. It can only be reported to the leader of the martial arts, and the leader of the martial arts will mediate, as shown in the figure below.

Insert picture description here

Abstract intermediary class

//抽象中介者类
public abstract class Mediator {
    
    
    public abstract void notice(String message, Collegue collegue);
}

Abstract colleague class

//抽象同事类
public abstract class Collegue {
    
    
    protected Mediator mediator;

    public Collegue(Mediator mediator) {
    
    
        this.mediator = mediator;
    }
}

Specific colleague class

//具体同事类:武当派
public class Wudang extends Collegue {
    
    
    public Wudang(Mediator mediator) {
    
    
        super(mediator);
    }

    public void sendMessage(String message) {
    
    
        mediator.notice(message, this);
    }

    public void getNotice(String message) {
    
    
        System.out.println("武当派收到消息:" + message);
    }
}
//具体同事类:峨眉派
public class Emei extends Collegue {
    
    
    public Emei(Mediator mediator) {
    
    
        super(mediator);
    }

    public void sendMessge(String message) {
    
    
        mediator.notice(message, this);
    }

    public void getNotice(String message) {
    
    
        System.out.println("峨眉派收到消息:" + message);
    }
}
//具体同事类:少林寺
public class Shaolin extends Collegue {
    
    
    public Shaolin(Mediator mediator) {
    
    
        super(mediator);
    }

    public void sendMessage(String message) {
    
    
        mediator.notice(message, this);
    }

    public void getNotice(String message) {
    
    
        System.out.println("少林寺收到消息:" + message);
    }
}

Concrete intermediary class

//具体中介者类
public class Master extends Mediator {
    
    
    private Wudang wudang;
    private Emei emei;
    private Shaolin shaolin;

    public void setWudang(Wudang wudang) {
    
    
        this.wudang = wudang;
    }

    public void setEmei(Emei emei) {
    
    
        this.emei = emei;
    }

    public void setShaolin(Shaolin shaolin) {
    
    
        this.shaolin = shaolin;
    }

    @Override
    public void notice(String message, Collegue collegue) {
    
    
        if (collegue == wudang) {
    
    
            shaolin.getNotice(message);
        } else if (collegue == emei) {
    
    
            shaolin.getNotice(message);
        } else if (collegue == shaolin) {
    
    
            wudang.getNotice(message);
            emei.getNotice(message);
        }
    }
}

use

public class Demo {
    
    
    public static void main(String[] args) {
    
    
        Master master = new Master();
        Wudang wudang = new Wudang(master);
        Emei emei = new Emei(master);
        Shaolin shaolin = new Shaolin(master);
        master.setWudang(wudang);
        master.setEmei(emei);
        master.setShaolin(shaolin);
        wudang.sendMessage("武当弟子被少林所伤");
        emei.sendMessge("峨眉弟子被少林所伤");
        shaolin.sendMessage("误会,不可能,绝对不可能!");
    }
}
少林寺收到消息:武当弟子被少林所伤
少林寺收到消息:峨眉弟子被少林所伤
武当派收到消息:误会,不可能,绝对不可能!
峨眉派收到消息:误会,不可能,绝对不可能!

Guess you like

Origin blog.csdn.net/qq_14876133/article/details/112987795