From 505454cbae5fbb58bfd2df51beb48b3307c4f6e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aitor=20Fidalgo=20S=C3=A1nchez?= <64830228+aitorfi@users.noreply.github.com> Date: Sun, 16 Oct 2022 18:25:36 +0200 Subject: [PATCH] translation: Closes #2077 Translate the active-object readme to Spanish (#2078) --- localization/es/active-object/README.md | 130 ++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 localization/es/active-object/README.md diff --git a/localization/es/active-object/README.md b/localization/es/active-object/README.md new file mode 100644 index 000000000..617d07b77 --- /dev/null +++ b/localization/es/active-object/README.md @@ -0,0 +1,130 @@ +--- +layout: pattern +title: Active Object +folder: active-object +permalink: /patterns/active-object/ +categories: Concurrency +language: es +tags: + - Performance +--- + + +## Propósito +El patrón de diseño de objeto activo desacopla la ejecución del método de la invocación del método para los objetos que residen en su propio hilo de control. El objetivo es introducir la concurrencia mediante el uso de la invocación de métodos asíncronos y un planificador para manejar solicitudes. + +## Explicación + +La clase que implementa el patrón de diseño de objeto activo contendrá un mecanismo de autosincronización sin utilizar métodos sincronizados (synchronized). + +Ejemplo del mundo real + +> Los orcos son conocidos por su salvajismo y filosofía de no hacer equipo. Basandonos en este comportamiento se podría decir que tienen su propio hilo de control. + +Podemos usar el patrón Active Object para implementar una criatura que tiene su propio hilo de control y exponer su API pero no la ejecución como tal. + + +**Ejemplo Programático** + +```java +public abstract class ActiveCreature{ + private final Logger logger = LoggerFactory.getLogger(ActiveCreature.class.getName()); + + private BlockingQueue requests; + + private String name; + + private Thread thread; + + public ActiveCreature(String name) { + this.name = name; + this.requests = new LinkedBlockingQueue(); + thread = new Thread(new Runnable() { + @Override + public void run() { + while (true) { + try { + requests.take().run(); + } catch (InterruptedException e) { + logger.error(e.getMessage()); + } + } + } + } + ); + thread.start(); + } + + public void eat() throws InterruptedException { + requests.put(new Runnable() { + @Override + public void run() { + logger.info("{} is eating!",name()); + logger.info("{} has finished eating!",name()); + } + } + ); + } + + public void roam() throws InterruptedException { + requests.put(new Runnable() { + @Override + public void run() { + logger.info("{} has started to roam the wastelands.",name()); + } + } + ); + } + + public String name() { + return this.name; + } +} +``` + +Podemos ver que cualquier clase que extienda de ActiveCreature tendrá su propio hilo de control para invocar y ejecutar métodos. + +Por ejemplo, la clase Orc: + +```java +public class Orc extends ActiveCreature { + + public Orc(String name) { + super(name); + } + +} +``` + +Ahora podemos crear múltiples criaturas como los Orcos, diles que coman y deambulen y lo harán en su propio hilo de control: + +```java + public static void main(String[] args) { + var app = new App(); + app.run(); + } + + @Override + public void run() { + ActiveCreature creature; + try { + for (int i = 0;i < creatures;i++) { + creature = new Orc(Orc.class.getSimpleName().toString() + i); + creature.eat(); + creature.roam(); + } + Thread.sleep(1000); + } catch (InterruptedException e) { + logger.error(e.getMessage()); + } + Runtime.getRuntime().exit(1); + } +``` + +## Diagrama de clases + +![alt text](../../../active-object/etc/active-object.urm.png "Active Object class diagram") + +## Tutoriales + +* [Android and Java Concurrency: The Active Object Pattern](https://www.youtube.com/watch?v=Cd8t2u5Qmvc) \ No newline at end of file