java design pattern mediator pattern

Design pattern intermediary pattern
    A mediation object is used to encapsulate some object interactions. Cross references of objects that do not need to be displayed.
    Mediators are all mediators, which conform to the principle of minimum knowledge, and put the interaction functions on mediators.
    Advantage:
        The flexibility is high, because the colleague class is coupled, so that it does not have to be related;
        The complexity of class is reduced, and one to many is transformed into one to one;
    Disadvantages:
        Over centralization is a potential disadvantage of the mediator model. If there are more colleagues, the interaction is also complex.
        Then these interactions are all concentrated in the intermediary object, which will lead to the overstaffed intermediary object, which is difficult to manage and maintain.
    Typical example:
        The interaction between the computer motherboard (main line) and CPU, silver disk, memory, etc.

Class diagram:

The intermediary needs to know all colleagues, and colleagues also need to know the intermediary. The intermediary will coordinate the actions of other colleagues by telling them what they have.

 

Here is an example of a real-world house buying model to illustrate the intermediary model:

1: First declare the interface of real estate agency

This mediation interface defines many colleague classes and many actions

/**
 * Real estate intermediary
 * Intermediary is a function, communication
 */
public abstract class RealtyMediator {
    //All intermediaries have information about their colleagues
    //Colleagues - Bank
    protected BankColleague bankColleague;
    //Colleagues - buyers
    protected BuyerColleague buyerColleague;
    //Colleague - owner
    protected OwnerColleague ownerColleague;




    /**
     * Want to sell
     */
    abstract void wantToSale();

    /**
     * Want to buy a building
     */
    abstract void wantToBuy();

    /**
     * Agree to sell floor
     */
    abstract void agreeToSale();

    /**
     * Want a loan
     */
    abstract void wantToBorrow();

    /**
     * Consent loan
     */
    abstract void agreeToBorrow();

    //get and set

    public BankColleague getBankColleague() {
        return bankColleague;
    }

    public void setBankColleague(BankColleague bankColleague) {
        this.bankColleague = bankColleague;
    }

    public BuyerColleague getBuyerColleague() {
        return buyerColleague;
    }

    public void setBuyerColleague(BuyerColleague buyerColleague) {
        this.buyerColleague = buyerColleague;
    }

    public OwnerColleague getOwnerColleague() {
        return ownerColleague;
    }

    public void setOwnerColleague(OwnerColleague ownerColleague) {
        this.ownerColleague = ownerColleague;
    }

}

2: Specific intermediary - a chain J intermediary company

This class implements several actions. For example, the wantosale() method to sell the house is called by the owner. After calling, it is the wantobuy() method to notify the buyer

/**
 * A chain J real estate agency company
 */
public class LjRealtyMediator extends RealtyMediator {

    @Override
    void wantToSale() {
        System.out.println("Agent: there are houses available. Let's see if the buyer wants to buy them");
        buyerColleague.wantToBuy();
    }

    @Override
    void wantToBuy() {
        System.out.println("Agent: the buyer wants to buy the house, and then confirms the price to the owner to see whether it can be sold or not");
        ownerColleague.agreeToSale();
    }

    @Override
    void agreeToSale() {
        System.out.println("Agent: the owner agrees with the seller. Tell the buyer to prepare the money");
        buyerColleague.wantToBorrow();
    }

    @Override
    void wantToBorrow() {
        System.out.println("Agent: the buyer wants to borrow money. Let me talk to the bank");
        bankColleague.agreeToBorrow();
    }

    @Override
    void agreeToBorrow() {
        System.out.println("Intermediary: the bank has agreed to loan, tell the buyer to prepare the materials");
        buyerColleague.ready();
    }
}

3: Colleague class interface

This interface is just a constructor

/**
 * Intermediary colleagues
 */
public abstract class Colleague {
    protected RealtyMediator realtyMediator;
    Colleague(RealtyMediator realtyMediator){
        this.realtyMediator = realtyMediator;
    }

}

4: Specific colleagues:

Bank colleagues

There is a way to agree with the loan. Inform the intermediary bank that the loan has been agreed

/**
 * Bank intermediary model colleagues
 */
public class BankColleague extends Colleague {
    BankColleague(RealtyMediator realtyMediator) {
        super(realtyMediator);
        System.out.println("Banks and intermediaries have always been partners. They have known each other for a long time");
        realtyMediator.setBankColleague(this);
    }

    /**
     * Consent loan
     */
    public void agreeToBorrow(){
        System.out.println("Bank: agreed loan");
        realtyMediator.agreeToBorrow();
    }
}

Employer colleagues

If you want to sell a house or agree to sell a house, you should tell the intermediary to take the next step

/**
 * The owner intermediary colleague class of house sale
 */
public class OwnerColleague extends Colleague{
    OwnerColleague(RealtyMediator realtyMediator) {
        super(realtyMediator);
        System.out.println("The owner wants to sell the house and knows the intermediary");
        realtyMediator.setOwnerColleague(this);
    }

    //Owner's actions

    /**
     *  Want to sell
     */
    public void wantToSale(){
        System.out.println("Owner: want to sell");
        realtyMediator.wantToSale();
    }

    /**
     * Agreed to sell
     */
    public void agreeToSale(){
        System.out.println("Owner: agree to sell");
        realtyMediator.agreeToSale();
    }

}

Colleagues of buyers

/**
 * Buyer intermediary model colleague class
 */
public class BuyerColleague extends Colleague {

    BuyerColleague(RealtyMediator realtyMediator) {
        super(realtyMediator);
        System.out.println("House buyer:Want to buy a house, know the agency");
        realtyMediator.setBuyerColleague(this);
    }

    /**
     * Want to buy
     */
    public void wantToBuy(){
        System.out.println("House buyer:Want to buy a house");
        realtyMediator.wantToBuy();
    }

    /**
     * Want a loan
     */
    public void wantToBorrow(){
        System.out.println("House buyer:Want a loan");
        realtyMediator.wantToBorrow();
    }

    /**
     * Get ready
     */
    public void ready(){
        System.out.println("House buyer:Materials in preparation...");
    }
}

 

All the above statements are finished. Next, it's the intense test. Let's see how to use it.

 public static void main(String[] args) {
        System.out.println("Role initialization...");
        //Declare a real estate agent
        RealtyMediator realtyMediator = new LjRealtyMediator();
        //Bank
        Colleague bank = new BankColleague(realtyMediator);
        //House buyer
        Colleague buyer = new BuyerColleague(realtyMediator);
        //Owner seller
        Colleague owner = new OwnerColleague(realtyMediator);
        System.out.println();
        System.out.println("------------The story is about to happen---------------");
        System.out.println();
        //Owners want to sell
        ((OwnerColleague) owner).wantToSale();
    }

Operation result:

Role initialization
 Banks and intermediaries have always been partners. They have known each other for a long time
 Buyer: want to buy a house, know the intermediary
 The owner wants to sell the house and knows the intermediary

------------The story is about to happen---------------

Owner: want to sell
 Agent: there are houses available. Let's see if the buyer wants to buy them
 Buyer: want to buy a house
 Agent: the buyer wants to buy the house, and then confirms the price to the owner to see whether it can be sold or not
 Owner: agree to sell
 Agent: the owner agrees with the seller. Tell the buyer to prepare the money
 Buyer: want a loan
 Agent: the buyer wants to borrow money. Let me talk to the bank
 Bank: agreed loan
 Intermediary: the bank has agreed to loan, tell the buyer to prepare the materials
 Buyer: materials are being prepared

The example has been demonstrated. Through this example, we can also see that the colleague class is actually our business class. He is only responsible for his own business. He knows that there is an intermediary there. He doesn't care about the operation behind the specific intermediary. When he is asked to do it, he will be asked to do it,

But this intermediary is really too hard. There are too many things to do. If they have more types of communication, the intermediary will be too bloated, which is also the disadvantage of the intermediary model.

Design pattern is a kind of thinking. It may not be perfect, but it is really beautiful.

Published 53 original articles, won praise 5, and visited 20000+
Private letter follow

Posted on Wed, 04 Mar 2020 02:26:01 -0800 by footiemadman007