Mediator design pattern

In software engineering, the mediator pattern defines an object that encapsulates how a set of objects interact. This pattern is considered to be a behavioral pattern due to the way it can alter the program's running behavior. Usually a program is made up of a large number of classes. Logic and computation are distributed among these classes. However, as more classes are added to a program, especially during maintenance and/or refactoring, the problem of communication between these classes may become more complex. This makes the program harder to read and maintain. Furthermore, it can become difficult to change the program, since any change may affect code in several other classes.

With the mediator pattern, communication between objects is encapsulated within a mediator object. Objects no longer communicate directly with each other, but instead communicate through the mediator. This reduces the dependencies between communicating objects.

[Wikipedia]

Intro

This program illustarte an auction. The AuctionMediator is responsible for adding the buyers, and afer each buyer bid a certain amount for the item, the mediator know who won the auction.

Mediator


public interface Mediator {

    public void addBuyer(Buyer buyer);
    public void findHighestBidder();

}

AuctionMediator


    public class AuctionMediator implements Mediator {
    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.");
    }

}

Buyer


    public abstract class 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 ();


}
    

AuctionBuyer


    public class AuctionBuyer extends Buyer {

   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;
    }
}

Main


public class Main {
    /*
    This program illustarte an auction. The AuctionMediator is responsible for adding the buyers,
    and afer 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");

        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...");
        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();
    }
}

Running Example



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.