mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-15 06:58:41 +00:00
157 lines
5.2 KiB
Markdown
157 lines
5.2 KiB
Markdown
---
|
|
title: Observer
|
|
shortTitle: Observer
|
|
category: Behavioral
|
|
language: en
|
|
tag:
|
|
- Gang Of Four
|
|
- Reactive
|
|
---
|
|
|
|
## También conocido como
|
|
|
|
Dependents, Publish-Subscribe
|
|
|
|
## Propósito
|
|
|
|
Defina una dependencia de uno a muchos entre objetos para que cuando un objeto cambie de estado, todos sus dependientes sean notificados y actualizados automáticamente.
|
|
|
|
## Explicación
|
|
|
|
Ejemplo real
|
|
|
|
> En una tierra muy lejana viven las razas de los hobbits y los orcos. Ambos viven principalmente al aire libre, por lo que siguen de cerca los cambios meteorológicos. Se podría decir que observan constantemente el tiempo.
|
|
|
|
En palabras simples
|
|
|
|
> Se registran como observadores para recibir los cambios de estado del objeto.
|
|
|
|
Wikipedia dice
|
|
|
|
> El patrón observador es un patrón de diseño de software en el que un objeto, llamado sujeto, mantiene una lista de sus dependientes, llamados observadores, y les notifica automáticamente cualquier cambio de estado, normalmente llamando a uno de sus métodos.
|
|
|
|
**Ejemplo programático**
|
|
|
|
Presentemos primero la interfaz `WeatherObserver` y nuestras razas, `Orcs` y `Hobbits`.
|
|
|
|
```java
|
|
public interface WeatherObserver {
|
|
|
|
void update(WeatherType currentWeather);
|
|
}
|
|
|
|
@Slf4j
|
|
public class Orcs implements WeatherObserver {
|
|
|
|
@Override
|
|
public void update(WeatherType currentWeather) {
|
|
LOGGER.info("The orcs are facing " + currentWeather.getDescription() + " weather now");
|
|
}
|
|
}
|
|
|
|
@Slf4j
|
|
public class Hobbits implements WeatherObserver {
|
|
|
|
@Override
|
|
public void update(WeatherType currentWeather) {
|
|
switch (currentWeather) {
|
|
LOGGER.info("The hobbits are facing " + currentWeather.getDescription() + " weather now");
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|
|
Luego está el clima `Weather`, que cambia constantemente.
|
|
|
|
```java
|
|
@Slf4j
|
|
public class Weather {
|
|
|
|
private WeatherType currentWeather;
|
|
private final List<WeatherObserver> observers;
|
|
|
|
public Weather() {
|
|
observers = new ArrayList<>();
|
|
currentWeather = WeatherType.SUNNY;
|
|
}
|
|
|
|
public void addObserver(WeatherObserver obs) {
|
|
observers.add(obs);
|
|
}
|
|
|
|
public void removeObserver(WeatherObserver obs) {
|
|
observers.remove(obs);
|
|
}
|
|
|
|
/**
|
|
* Makes time pass for weather.
|
|
*/
|
|
public void timePasses() {
|
|
var enumValues = WeatherType.values();
|
|
currentWeather = enumValues[(currentWeather.ordinal() + 1) % enumValues.length];
|
|
LOGGER.info("The weather changed to {}.", currentWeather);
|
|
notifyObservers();
|
|
}
|
|
|
|
private void notifyObservers() {
|
|
for (var obs : observers) {
|
|
obs.update(currentWeather);
|
|
}
|
|
}
|
|
}
|
|
```
|
|
Aquí está el ejemplo completo en acción.
|
|
|
|
```java
|
|
var weather = new Weather();
|
|
weather.addObserver(new Orcs());
|
|
weather.addObserver(new Hobbits());
|
|
weather.timePasses();
|
|
weather.timePasses();
|
|
weather.timePasses();
|
|
weather.timePasses();
|
|
```
|
|
|
|
Salida del programa:
|
|
|
|
```
|
|
The weather changed to rainy.
|
|
The orcs are facing rainy weather now
|
|
The hobbits are facing rainy weather now
|
|
The weather changed to windy.
|
|
The orcs are facing windy weather now
|
|
The hobbits are facing windy weather now
|
|
The weather changed to cold.
|
|
The orcs are facing cold weather now
|
|
The hobbits are facing cold weather now
|
|
The weather changed to sunny.
|
|
The orcs are facing sunny weather now
|
|
The hobbits are facing sunny weather now
|
|
```
|
|
|
|
## Diagrama de clases
|
|
|
|

|
|
|
|
## Aplicabilidad
|
|
|
|
Utilice el patrón Observador en cualquiera de las siguientes situaciones:
|
|
|
|
* Cuando una abstracción tiene dos aspectos, uno dependiente del otro. Encapsular estos aspectos en objetos separados te permite variarlos y reutilizarlos independientemente.
|
|
* Cuando un cambio en un objeto requiere cambiar otros, y no sabes cuántos objetos necesitan ser cambiados.
|
|
* Cuando un objeto debe ser capaz de notificar a otros objetos sin hacer suposiciones sobre quiénes son estos objetos. En otras palabras, no quieres que estos objetos estén estrechamente acoplados.
|
|
|
|
## Usos conocidos
|
|
|
|
* [java.util.Observer](http://docs.oracle.com/javase/8/docs/api/java/util/Observer.html)
|
|
* [java.util.EventListener](http://docs.oracle.com/javase/8/docs/api/java/util/EventListener.html)
|
|
* [javax.servlet.http.HttpSessionBindingListener](http://docs.oracle.com/javaee/7/api/javax/servlet/http/HttpSessionBindingListener.html)
|
|
* [RxJava](https://github.com/ReactiveX/RxJava)
|
|
|
|
## Créditos
|
|
|
|
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://www.amazon.com/gp/product/0201633612/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0201633612&linkCode=as2&tag=javadesignpat-20&linkId=675d49790ce11db99d90bde47f1aeb59)
|
|
* [Java Generics and Collections](https://www.amazon.com/gp/product/0596527756/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596527756&linkCode=as2&tag=javadesignpat-20&linkId=246e5e2c26fe1c3ada6a70b15afcb195)
|
|
* [Head First Design Patterns: A Brain-Friendly Guide](https://www.amazon.com/gp/product/0596007124/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596007124&linkCode=as2&tag=javadesignpat-20&linkId=6b8b6eea86021af6c8e3cd3fc382cb5b)
|
|
* [Refactoring to Patterns](https://www.amazon.com/gp/product/0321213351/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0321213351&linkCode=as2&tag=javadesignpat-20&linkId=2a76fcb387234bc71b1c61150b3cc3a7)
|