Files
java-design-patterns/localization/es/delegation
Luis Mateo Hincapié Martinez ead6373765 translation: Translated all structural patterns category to Spanish (Task of issue #2277) (#2890)
* Typo corrections

* bridge pattern translated to spanish

* builder pattern translated to spanish

* context-object pattern translated to spanish

* converter pattern translated to spanish

* dependency injection pattern translated to spanish

* factory pattern translated to spanish

* factory-kit pattern translated to spanish

* factory-method pattern translated to spanish

* monostate pattern translated to spanish

* multiton pattern translated to spanish

* object mother pattern translated to spanish

* object pool pattern translated to spanish

* property pattern translated to spanish

* prototype pattern translated to spanish

* registry pattern translated to spanish

* step builder pattern translated to spanish

* value object pattern translated to spanish

* typo corrections and resources urls refactor

* grammar corrections

* Add image to each pattern

* translate all structural patterns to Spanish

---------

Co-authored-by: luismateoh <luismateohm@gmail.com>
Co-authored-by: luis.hincapie <luis.hincapie@blankfactor.com>
2024-04-04 19:54:16 +03:00
..

title, category, language, tag
title category language tag
Delegation Structural es
Decoupling

También conocido como

Proxy Pattern

Propósito

Es una técnica en la que un objeto expresa cierto comportamiento al exterior pero en realidad delega la responsabilidad de implementar ese comportamiento en un objeto asociado.

Explanation

Real-world example

Imaginemos que tenemos aventureros que luchan contra monstruos con diferentes armas dependiendo de sus habilidades y destrezas. Debemos ser capaces de equiparles con diferentes sin tener que modificar su código fuente para cada una de ellas. El patrón de delegación lo hace posible delegando el trabajo dinámico a un objeto específico que implementa una interfaz con métodos relevantes.

Wikipedia dice

En programación orientada a objetos, la delegación se refiere a la evaluación de un miembro (propiedad o método) de un objeto (el receptor) en el contexto de otro objeto original (el emisor). La delegación puede hacerse explícitamente, pasando el objeto emisor al objeto receptor, lo que puede hacerse en cualquier lenguaje orientado a objetos; o implícitamente, mediante las reglas de búsqueda de miembros del lenguaje, lo que requiere soporte del lenguaje para la función.

Ejemplo programático

Tenemos una interfaz Printer y tres implementaciones CanonPrinter, EpsonPrinter y HpPrinter.

public interface Printer {
  void print(final String message);
}

@Slf4j
public class CanonPrinter implements Printer {
  @Override
  public void print(String message) {
    LOGGER.info("Canon Printer : {}", message);
  }
}

@Slf4j
public class EpsonPrinter implements Printer {
  @Override
  public void print(String message) {
    LOGGER.info("Epson Printer : {}", message);
  }
}

@Slf4j
public class HpPrinter implements Printer {
  @Override
  public void print(String message) {
    LOGGER.info("HP Printer : {}", message);
  }
}

El PrinterController puede ser utilizado como un Printer delegando cualquier trabajo manejado por este a un objeto que la implemente.

public class PrinterController implements Printer {
  
  private final Printer printer;
  
  public PrinterController(Printer printer) {
    this.printer = printer;
  }
  
  @Override
  public void print(String message) {
    printer.print(message);
  }
}

Now on the client code printer controllers can print messages differently depending on the object they're delegating that work to.

private static final String MESSAGE_TO_PRINT = "hello world";

var hpPrinterController = new PrinterController(new HpPrinter());
var canonPrinterController = new PrinterController(new CanonPrinter());
var epsonPrinterController = new PrinterController(new EpsonPrinter());

hpPrinterController.print(MESSAGE_TO_PRINT);
canonPrinterController.print(MESSAGE_TO_PRINT);
epsonPrinterController.print(MESSAGE_TO_PRINT)

Salida del programa:

HP Printer : hello world
Canon Printer : hello world
Epson Printer : hello world

Diagrama de clases

alt text

Aplicabilidad

Utilice el patrón Delegate para conseguir lo siguiente

  • Reducir el acoplamiento de los métodos a su clase
  • Componentes que se comportan de forma idéntica, pero teniendo en cuenta que esta situación puede cambiar en el futuro.

Créditos