Behavioral Design Patterns: Mediator
Sometimes you just need to make peace.
Join the DZone community and get the full member experience.
Join For FreePreviously, we had a look at the iterator pattern. The mediator pattern is very different in what it tries to achieve. It is one of the behavioral patterns and its purpose is to alter the way objects communicate with each other. Instead of the objects communicating with each other directly, the mediator will handle the objects' interactions.
For example, imagine the scenario of a financial exchange. You do want to trade and buy but you don't buy directly from the one that makes the offer. Instead, the exchange is in the middle, in order for you to make the transaction.
People would like to sell and buy. This shall be facilitated by the exchange. First, you have the Order
object.
package com.gkatzioura.design.behavioural.mediator;
public class Order {
private String stock;
private Integer quantity;
private Double price;
public String getStock() {
return stock;
}
public void setStock(String stock) {
this.stock = stock;
}
public Integer getQuantity() {
return quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
}
The next object will be the FinancialEntity
that sells the stocks.
package com.gkatzioura.design.behavioural.mediator;
public class FinancialEntity {
public boolean sell(Order order) {
/**
* Supposing the sale was successful return true
*/
return true;
}
}
Then, we create the Exchange
object. We won't get further into commissions but imagine that things can be way more complex. The Exchange
is actually our mediator.
package com.gkatzioura.design.behavioural.mediator;
public class Exchange {
private FinancialEntity financialEntity;
public Exchange(FinancialEntity financialEntity) {
this.financialEntity = financialEntity;
}
public void serve(Order order) {
/**
* Choose the financial entity suitable for the order
*/
financialEntity.sell(order);
}
}
And the last step is creating the Trader
object.
package com.gkatzioura.design.behavioural.mediator;
public class Trader {
private Exchange exchange;
public Trader(Exchange exchange) {
this.exchange = exchange;
}
public void buy(String stock,Integer quantity,Double price) {
Order order = new Order();
order.setStock(stock);
order.setQuantity(quantity);
order.setPrice(price);
exchange.serve(order);
}
}
As you can see, the Trader
object is not interacting directly with the financial entity that provides the stocks.
Let's put them all together in the main class.
package com.gkatzioura.design.behavioural.mediator;
public class Mediator {
public static void main(String[] args) {
final FinancialEntity financialEntity = new FinancialEntity();
final Exchange exchange = new Exchange(financialEntity);
Trader trader = new Trader(exchange);
trader.buy("stock_a",2,32.2d);
}
}
That's it, you just used the mediator pattern for an exchange application! You can also find the source code on GitHub.
Published at DZone with permission of Emmanouil Gkatziouras, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments