📜  中介者设计模式

📅  最后修改于: 2021-09-10 02:50:54             🧑  作者: Mango

中介者设计模式定义了一个对象,该对象封装了一组对象如何交互。
Mediator 是一种行为模式(类似于观察者或访问者模式),因为它可以改变程序的运行行为。
我们习惯于看到由大量类组成的程序。但是,随着程序中添加更多类,这些类之间的通信问题可能会变得更加复杂。
因此,维护成为我们需要以这种方式解决的一个大问题。
像在许多其他设计模式中一样,中介者模式来解决这个问题。它使对象之间的通信被一个中介对象封装。
对象之间不直接通信,而是通过中介进行通信。

Java的中介模式实现

该程序说明了拍卖。 Auction Mediator 负责添加买家,当每个买家出价一定金额后,中介就知道谁赢得了拍卖。
类图:

中介者设计模式

Java
// Java code to illustrate Mediator Pattern
// All public class codes should be put in
// different files.
 
public interface Mediator {
 
    // The mediator interface
    public void addBuyer(Buyer buyer);
    public void findHighestBidder();
}
 
public class AuctionMediator implements Mediator {
 
    // this class implements the interface and holds
    // all the buyers in a Array list.
    // We can add buyers and find the highest bidder
    private ArrayList buyers;
 
    public AuctionMediator()
    {
        buyers = new ArrayList<>();
    }
 
    @Override
    public void addBuyer(Buyer buyer)
    {
        buyers.add(buyer);
        System.out.println(buyer.name + " was added to" +
                "the buyers list.");
    }
 
    @Override
    public void findHighestBidder()
    {
        int maxBid = 0;
        Buyer winner = null;
        for (Buyer b : buyers) {
            if (b.price > maxBid) {
                maxBid = b.price;
                winner = b;
            }
        }
        System.out.println("The auction winner is " + winner.name +
        ". He paid " + winner.price + "$ for the item.");
    }
}
 
public abstract class Buyer {
     
    // this class holds the buyer
    protected Mediator mediator;
    protected String name;
    protected int price;
 
    public Buyer(Mediator med, String name)
    {
        this.mediator = med;
        this.name = name;
    }
 
    public abstract void bid(int price);
 
    public abstract void cancelTheBid();
}
 
public class AuctionBuyer extends Buyer {
 
    // implementation of the bidding process
    // There is an option to bid and an option to
    // cancel the bidding
    public AuctionBuyer(Mediator mediator,
                                String name)
    {
        super(mediator, name);
    }
 
    @Override
    public void bid(int price)
    {
        this.price = price;
    }
 
    @Override
    public void cancelTheBid()
    {
        this.price = -1;
    }
}
 
public class Main {
 
    /* This program illustrate an auction. The AuctionMediator
    is responsible for adding the buyers, and after each
    buyer bid a certain amount for the item, the mediator
    know who won the auction. */
    public static void main(String[] args)
    {
 
        AuctionMediator med = new AuctionMediator();
        Buyer b1 = new AuctionBuyer(med, "Tal Baum");
        Buyer b2 = new AuctionBuyer(med, "Elad Shamailov");
        Buyer b3 = new AuctionBuyer(med, "John Smith");
 
        // Create and add buyers
        med.addBuyer(b1);
        med.addBuyer(b2);
        med.addBuyer(b3);
 
        System.out.println("Welcome to the auction. Tonight " +
                        "we are selling a vacation to Vegas." +
                        " please Bid your offers.");
        System.out.println("--------------------------------" +
                                        "---------------");
        System.out.println("Waiting for the buyer's offers...");
 
        // Making bids
        b1.bid(1800);
        b2.bid(2000);
        b3.bid(780);
        System.out.println("---------------------------------" +
                                            "--------------");
        med.findHighestBidder();
 
        b2.cancelTheBid();
        System.out.print(b2.name + " Has canceled his bid!, " +
                                            "in that case ");
        med.findHighestBidder();
    }
}


输出:

Tal Baum was added to the buyers list.
Elad Shamailov was added to the buyers list.
John Smith was added to the buyers list.
Welcome to the auction. Tonight we are
 selling a vacation to Vegas. please Bid your offers.
-----------------------------------------------
Waiting for the buyer's offers...
-----------------------------------------------
The auction winner is Elad Shamailov.
He paid 2000$ for the item.
Elad Shamailov Has canceled his bid!, In that 
case The auction winner is Tal Baum.
He paid 1800$ for the item.

优点

  • 简单
  • 您可以用不同的对象替换结构中的一个对象,而不会影响类和接口。

缺点

  • Mediator 通常需要非常熟悉所有不同的类,这让它变得非常复杂。
  • 会使维护变得困难。

作者:http://designpattern.co.il/