PROGRAMMING

m9/ JAVA
REMEMBERS




Last update:   10-10-2021

Mediator

It is used to encapsulate comunications between related objects (named Colleagues). Colleagues don't need to know anything about each other. Collegues will send messages to the Mediator. Mediator send messages to the other colleques.
 
/**
 * Collegues will send messages to the Mediator.
 * Mediator will send back messages (if needeed) to the other colleques
 */
class StockMarket {

    public static void main(String[] args) {
        
        StockMediator nyse = new StockMediator();
        
        new FloorBroker("GormanSlacks", nyse);
        new ElectronicBroker("JTPoorman", nyse);
    }
}

// Mediator

class StockMediator implements Mediator {
    public void addColleague(Colleague colleague) {}
    public void saleOrder(String stock, int shares) {}
    public void buyOrder(String stock, int shares) {}
}

interface Mediator {
    public void addColleague(Colleague colleague);
    public void saleOrder(String stock, int shares);
    public void buyOrder(String stock, int shares);
}

// Collegue

class FloorBroker extends Colleague {
    public FloorBroker(String name, Mediator m) {
        super(name, m);
        System.out.println(
            "FloorBroker: " + name + " signed up with the stockexchange!"
        );
    }
}

class ElectronicBroker extends Colleague {
    public ElectronicBroker(String name, Mediator n) {
        super(name, n);
        System.out.println(
            "ElectronicBroker: " + name + " signed up with the stockexchange!"
        );
    }
}

abstract class Colleague {

    private Mediator mediator;

    public Colleague(String b, Mediator m) {
        mediator = m;
        mediator.addColleague(this);
    }

    public void saleOrder(String stock, int shares) {
        mediator.saleOrder(stock, shares); // Look Here
    }

    public void butyOrder(String stock, int shares) {
        mediator.buyOrder(stock, shares);
    }
}

/*

FloorBroker: GormanSlacks signed up with the stockexchange!
ElectronicBroker: JTPoorman signed up with the stockexchange!

*/
... 50 lines
 
 
import java.util.ArrayList;

/**
 * Stock Mediator sales and buys orders on behalf of brokers Colleague(s)
 * When a broker Colleague sales or buy shares ...
 * it lets the Meditor to decide and do the job
 */
class StockExchange {

    public static void main(String[] args) {

        StockMediator nyse = new StockMediator();
        FloorBroker b1 = new FloorBroker("GormanSlacks", nyse);
        ElectronicBroker b2 = new ElectronicBroker("JTPoorman", nyse);
        b1.orderBuy("MSFT", 100);
        b1.orderSell("GOOG", 50);
        b2.orderBuy("MSFT", 100);
        b2.orderSell("NRG", 10);
        b1.orderBuy("NRG", 10);
    }
}

class FloorBroker extends Colleague {
    public FloorBroker(String name, Mediator m) {
        super(name, m);
    }
}
class ElectronicBroker extends Colleague {
    public ElectronicBroker(String name, Mediator m) {
        super(name, m);
    }
}
abstract class Colleague {

    private Mediator mediator;
    public String name;

    public Colleague(String n, Mediator m) {
        name = n;
        mediator = m;
        mediator.addColleague(this);
    }
    public void orderSell(String sym, int siz) {
        mediator.orderSell(sym, siz, this); // Look Here
    }
    public void orderBuy(String sym, int siz) {
        mediator.orderBuy(sym, siz, this);
    }

}

class StockMediator implements Mediator {

    private ArrayList<Colleague> colleagues = new ArrayList<>();
    private ArrayList<Order> buyOrders = new ArrayList<>();
    private ArrayList<Order> sellOrders = new ArrayList<>();

    public void addColleague(Colleague collegue) {
        colleagues.add(collegue);
    }

    public void orderSell(String sym, int siz, Colleague colleague) {
        sellOrders.add(new Order(sym, siz, colleague.name));
        System.out.println(
            "Mediator: " + colleague.name + " / Sell / " + siz + " " + sym
        );
    }

    public void orderBuy(String sym, int siz, Colleague colleague) {
        buyOrders.add(new Order(sym, siz, colleague.name));
        System.out.println(
            "Mediator: " + colleague.name + " / Buy / " + siz + " " + sym
        );
    }
}
interface Mediator {
    public void addColleague(Colleague c);
    public void orderSell(String sym, int siz, Colleague c);
    public void orderBuy(String sym, int siz, Colleague c);
}

class Order {
    public Order(String sym, int siz, String n) {}
}

/*

Colleague GormanSlacks: Mediator signed me up
Colleague JTPoorman: Mediator signed me up
Mediator: GormanSlacks / Buy / 100 MSFT
Mediator: GormanSlacks / Sell / 50 GOOG
Mediator: JTPoorman / Buy / 100 MSFT
Mediator: JTPoorman / Sell / 10 NRG
Mediator: GormanSlacks / Buy / 10 NRG

*/
... 74 lines
 
Mediator v1.0 Basic Skeleton Mediator v1.2 Colleagues are signing up and Mediator assign broker Ids Mediator v1.4 Collegues sale and buy orders, Order object added Mediator v1.6 Add logic, buy/sell, error stock exceded

Differences

! The Mediator Pattern is used when you have two sub-systems with some dependencies, but you don't want them to comunicate directly. The Observer Pattern is used when a class wants to allow other classes to register themselves and receive notifications.
Questions    
Frame

        A B C D E F
🔔
1/4