sexta-feira, 27 de novembro de 2020

Pattern Mediator

 Today accomplish the course of design pattern lynda.com.


I decide to make a post about it because is one way to not forget what I learn.


The design pattern that I show here is Mediator 


The actual dentition of a mediator is someone who tries to make people in conflict come to an  agreement.


In JAVA the mediator pattern is a way for objects to communicate so that they loosely coupled


Mediator is a behavioral design pattern that lets you reduce chaotic dependencies between objects. The pattern restricts direct communications between the objects and forces them to collaborate only via a mediator object.


let's see now this piece of code 


public class Customer{
  private String address;
  private ECommerceSite site;

  public Customer(String address) {
    this.address = address;
    site = new ECommerceSite(this);
  }

  public String getAddress() {
    return address;
  }

  public void buy(String item, int quantity) {
    if(site.checkInStock(item, quantity)) {
      site.sell(item, quantity);
    }
  }

}

In this class we have a Customer constructor and inside one call to e-commerce. Yes we have a code smell, but move on in this post we have some tips for that.


Here we have class Driver and one implementation of the method deliver who has some responsibility

and that's okay but need a micro reafactor.


public class Driver {

  public void deliver(String item, int quantity, Customer customer) {
    System.out.println(quantity + " " + item + " out for delivery to " + customer.getAddress());
  }

}
import java.util.HashMap;

public class ECommerceSite {

  private Customer customer;
  private Driver driver;
  private HashMap stock;


  public ECommerceSite(Customer customer) {
    this.customer = customer;
    this.driver = new Driver();
    stock = new HashMap();
    stock.put("pens", 100);
    stock.put("pencils", 50);
    stock.put("erasers", 75);
  }

  public boolean checkInStock(String item, int quantity) {
    if (stock.containsKey(item) && stock.get(item) > quantity) {
      return true;
    } else {
      return false;
    }
  }

  public void sell(String item, int quantity) {

    int newQuantity = stock.get("pens") - quantity;
    stock.put(item, newQuantity);

    driver.deliver(item, quantity, customer);
  }

}
There is way to make this code better. The final version of this code wich simplified a lot is rigth here below:
public class Customer {

  private String address;

  public Customer(String address) {
    this.address = address;
  }

  public String getAddress() {
    return address;
  }

}
public class Driver {
  // for this example we make this function loaded of parameters
  public void deliver(String item, int amount, Customer customer) {
    System.out.println(amount + " " + item + " out for delivery to " + customer.getAddress());
  }

}

public class Main {

  public static void main(String[] args) {

    Mediator mediator = new Mediator();
    mediator.buy("pens", 3);
  }

}
import java.util.HashMap;

public class ECommerceSite {

  private HashMap stock;

//in this version we use only attributes
//strictly from the class 
  public ECommerceSite() {
    stock = new HashMap();
    stock.put("pens", 100);
    stock.put("pencils", 50);
    stock.put("paper", 500);
  }

  public void sell(String item, int amount) {
    int newAmount = stock.get("pens") - amount;
    stock.put(item, newAmount);
  }

  public boolean checkInStock(String item, int amount) {
    if (stock.containsKey(item) && stock.get(item) > amount) {
      return true;
    } else {
      return false;
    }
  }

}
public class Mediator {

  private Customer customer;
  private ECommerceSite site;
  private Driver driver;

  public Mediator() {
    customer = new Customer("123 Sunny Street");
    site = new ECommerceSite();
    driver = new Driver();
  }

  public void buy(String item, int amount) {
    if(site.checkInStock(item, amount)) {
      site.sell(item, amount);
      driver.deliver(item, amount, customer);
    }
  }

}
And thats it one more pattern or a tool for make our code less messy.




What solution does the Mediator design pattern describe?

  • Define a separate (mediator) object that encapsulates the interaction between a set of objects.
  • Objects delegate their interaction to a mediator object instead of interacting with each other directly.

The objects interact with each other indirectly through a mediator object that controls and coordinates the interaction.

This makes the objects loosely coupled. They only refer to and know about their mediator object and have no explicit knowledge of each other.



interface python