Java Design Patterns--Mediator Pattern

1 Mediator Pattern Mediator Pattern

Purpose: Use an intermediary object to encapsulate a series of object interactions. The intermediary makes the objects do not need to refer to each other explicitly, so that the coupling is loose, and the interaction between them can be changed independently;
Implementation: Define an object, This object encapsulates the way a set of objects interact.

1. It is used when multiple classes are coupled with each other to form a network structure, and the network structure is separated into a star structure;
2. The mediator class is used to coordinate calls between other objects/classes;

2 Implementation

Code scenario: Buyers and sellers trade on Taobao, and Taobao acts as an intermediary;

2.1 Code Implementation

abstract colleague role

public abstract class AbstractCustomer {
    // 客户名
    protected String name;
    // 账户默认100元
    protected int account = 100;
    // 心情
    protected String mood;

    // 事件方法
    public abstract void deal(AbstractMediator taoBao, String doSomeThing, int money);

    public String getName() {
        return name;
    }
    public String getMood() {
        return mood;
    }
    public void setMood(String mood) {
        this.mood = mood;
    }
    public int getAccount() {
        return account;
    }
}

Specific colleague role: Taobao buyer

public class Buyer extends AbstractCustomer {
    public Buyer() {
        name = "淘宝买家";
    }

    @Override
    public synchronized  void  deal(AbstractMediator taoBao, String doSomeThing, int money) {
        if ("购买".equals(doSomeThing)) {
            // 事件只对对象本身造成影响
            mood = "good";
            account = account - money;
            // 通过中介者影响对方
            taoBao.buyerAffectSeller(doSomeThing, money);
        }

        if ("退货".equals(doSomeThing)) {
            // 事件只对对象本身造成影响
            mood = "bad";
            account = account + money;
            // 通过中介者影响对方
            taoBao.buyerAffectSeller(doSomeThing, money);
        }
    }
}

Specific colleague roles: Taobao seller

public class Seller extends AbstractCustomer {
    public Seller() {
        name = "淘宝卖家";
    }
    @Override
    public synchronized void deal(AbstractMediator taoBao, String doSomeThing, int money) {
        if ("购买".equals(doSomeThing)) {
            // 事件只对对象本身造成影响
            mood = "good";
            account = account + money;
            // 通过中介者影响对方
            taoBao.sellerAffectBuyer(doSomeThing, money);
        }

        if ("退货".equals(doSomeThing)) {
            // 事件只对对象本身造成影响
            mood = "bad";
            account = account - money;
            // 通过中介者影响对方
            taoBao.sellerAffectBuyer(doSomeThing, money);
        }
    }
}

abstract mediator role

public abstract class AbstractMediator {
    protected AbstractCustomer buyer;
    protected AbstractCustomer seller;

    public AbstractMediator(AbstractCustomer buyer, AbstractCustomer seller) {
        this.buyer = buyer;
        this.seller = seller;
    }

    // 买家影响卖家
    public abstract void buyerAffectSeller(String doSomeThing, int money);
    // 卖家影响买家
    public abstract void sellerAffectBuyer(String doSomeThing, int money);
}

Specific intermediary role: Taobao

public class TaoBao extends AbstractMediator {

    public TaoBao(AbstractCustomer buyer, AbstractCustomer seller) {
        super(buyer, seller);
    }

    @Override
    public void buyerAffectSeller(String doSomeThing, int money) {
        // 买家对卖家的关系
        seller.setMood(buyer.getMood());
        if ("购买".equals(doSomeThing)) {
            seller.account = seller.account + money;
            // 打印展示相互影响的结果
            System.out.println(buyer.name + "进行了[" + doSomeThing + "],花了[" + money + "元],心情很[" + buyer.mood
                    + "],当前账户余额[" + buyer.account + "元]");
            System.out.println(seller.getName() + "收到[" + doSomeThing + "]请求,收到[" + money + "元],心情很[" + seller.mood
                    + "],当前账户余额[" + seller.getAccount() + "元]");
        }
        if ("退货".equals(doSomeThing)) {
            seller.account = seller.account - money;
            // 打印展示相互影响的结果
            System.out.println(buyer.name + "进行了[" + doSomeThing + "],退了[" + money + "元],心情很[" + buyer.mood
                    + "],当前账户余额[" + buyer.account + "元]");
            System.out.println(seller.getName() + "收到[" + doSomeThing + "]请求,收到[" + money + "元],心情很[" + seller.mood
                    + "],当前账户余额[" + seller.getAccount() + "元]");
        }

    }

    @Override
    public void sellerAffectBuyer(String doSomeThing, int money) {
        // 卖家对买家的关系
        buyer.setMood(seller.getMood());
        if ("购买".equals(doSomeThing)) {
            buyer.account = buyer.account - money;
            // 打印展示相互影响的结果
            System.out.println(
                    seller.name + "推销成功,收到[" + money + "元],心情很[" + seller.mood + "],当前账户余额[" + seller.account + "元]");
            System.out.println(buyer.getName() + "进行了[" + doSomeThing + "],花了[" + money + "元],心情很[" + seller.mood
                    + "],当前账户余额[" + buyer.getAccount() + "元]");
        }
        if ("退货".equals(doSomeThing)) {
            buyer.account = buyer.account + money;
            // 打印展示相互影响的结果
            System.out.println(seller.name + "收到[" + doSomeThing + "]请求,退了[" + money + "元],心情很[" + seller.mood
                    + "],当前账户余额[" + seller.account + "元]");
            System.out.println(buyer.getName() + "进行了[" + doSomeThing + "],收到[" + money + "元],心情很[" + buyer.mood
                    + "],当前账户余额[" + buyer.getAccount() + "元]");
        }
    }

}

2.2 Involving roles

The intermediary pattern structure diagram includes the following roles:

Mediator (Abstract Mediator): It defines an interface that is used to communicate with various colleague objects.

ConcreteMediator (concrete mediator): It is a subclass of abstract mediator, which implements cooperative behavior by coordinating various colleague objects, and it maintains a reference to each colleague object.

Colleague (abstract colleague class): It defines the public methods of each colleague class, and declares some abstract methods for subclasses to implement, and it maintains a reference to the abstract mediator class, which subclasses can use this reference to communicate with Intermediary Communications.

ConcreteColleague (concrete colleague class): It is a subclass of the abstract colleague class; each colleague object communicates with the intermediary first when it needs to communicate with other colleague objects, and indirectly completes the communication with other colleague classes through the intermediary; in the concrete The abstract method declared in the abstract colleague class is implemented in the colleague class.
The core of the mediator mode is the introduction of the mediator class. In the mediator mode, the mediator class undertakes two responsibilities:

(1) Relay function (structural): Through the relay function provided by the intermediary, each colleague object no longer needs to explicitly refer to other colleagues. When it needs to communicate with other colleagues, indirect calls can be realized through the intermediary. This transit role belongs to the structural support of the intermediary.

(2) Coordination (behavioral): The intermediary can further encapsulate the relationship between colleagues, and colleagues can interact with the intermediary consistently without specifying what the intermediary needs to do. The coordination logic within itself further processes the requests of colleagues, and separates and encapsulates the relationship behavior between colleagues. This coordinating role belongs to the mediator's behavioral support.

2.3 call

caller:

public class Client {
    public static void main(String[] args) {
        // 买家
        Buyer buyer = new Buyer();
        // 卖家
        Seller seller = new Seller();
        // 中介者
        AbstractMediator taoBao = new TaoBao(buyer, seller);
        System.out.println("剧情一--------买家发起购买----------");
        buyer.deal(taoBao, "购买", 20);

        System.out.println("剧情二--------买家发起退货----------");
        buyer.deal(taoBao, "退货", 20);

        System.out.println("剧情三--------卖家发起推销----------");
        seller.deal(taoBao, "购买", 30);

        System.out.println("剧情四--------卖家发起退货----------");
        seller.deal(taoBao, "退货", 30);
    }
}

result:

剧情一--------买家发起购买----------
淘宝买家进行了[购买],花了[20元],心情很[good],当前账户余额[80元]
淘宝卖家收到[购买]请求,收到[20元],心情很[good],当前账户余额[120元]
剧情二--------买家发起退货----------
淘宝买家进行了[退货],退了[20元],心情很[bad],当前账户余额[100元]
淘宝卖家收到[退货]请求,收到[20元],心情很[bad],当前账户余额[100元]
剧情三--------卖家发起推销----------
淘宝卖家推销成功,收到[30元],心情很[good],当前账户余额[130元]
淘宝买家进行了[购买],花了[30元],心情很[good],当前账户余额[70元]
剧情四--------卖家发起退货----------
淘宝卖家收到[退货]请求,退了[30元],心情很[bad],当前账户余额[100元]
淘宝买家进行了[退货],收到[30元],心情很[bad],当前账户余额[100元]

Code address: click to jump

References:
[1] Graphical Design Patterns/(Japanese) Hiroshi Yuki; translated by Yang Wenxuan. – Beijing: People’s Posts and Telecommunications Press, 2017.1.
[ 2 ] Wikipedia Design
Patterns [ 3 ] Geek Academy WIKI – Design Patterns .
[ 4 ] Rookie Tutorial – Design Patterns .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325568961&siteId=291194637