Déportation de l'observable dans une sous-classe pour le déclenchement de la commande
All checks were successful
Gradle Test / test (push) Successful in 52s
All checks were successful
Gradle Test / test (push) Successful in 52s
This commit is contained in:
parent
446fbbf89b
commit
d0a348f19f
2 changed files with 32 additions and 16 deletions
|
|
@ -2,12 +2,37 @@ package org.example;
|
|||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Cart implements Observable<String> {
|
||||
public class Cart {
|
||||
private String content;
|
||||
private ArrayList<Observer<String>> observers;
|
||||
|
||||
private class OrderInitiationNotifier implements Observable<String> {
|
||||
private ArrayList<Observer<String>> observers;
|
||||
|
||||
public OrderInitiationNotifier() {
|
||||
this.observers = new ArrayList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addObserver(Observer<String> obs) {
|
||||
observers.add(obs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeObserver(Observer<String> obs) {
|
||||
observers.remove(obs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notifyObserver(String content) {
|
||||
for (Observer<String> obs : observers) {
|
||||
obs.process(content);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public final OrderInitiationNotifier orderInitiationNotifier = new OrderInitiationNotifier();
|
||||
|
||||
public Cart() {
|
||||
this.observers = new ArrayList<>();
|
||||
this.content = "Contenu du panier";
|
||||
}
|
||||
|
||||
|
|
@ -16,23 +41,14 @@ public class Cart implements Observable<String> {
|
|||
}
|
||||
|
||||
public void initiateOrder() {
|
||||
notifyObserver();
|
||||
orderInitiationNotifier.notifyObserver(this.content);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addObserver(Observer<String> obs) {
|
||||
observers.add(obs);
|
||||
orderInitiationNotifier.addObserver(obs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeObserver(Observer<String> obs) {
|
||||
observers.remove(obs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notifyObserver() {
|
||||
for (Observer<String> obs : observers) {
|
||||
obs.process(this.content);
|
||||
}
|
||||
orderInitiationNotifier.removeObserver(obs);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,5 +3,5 @@ package org.example;
|
|||
public interface Observable<T> {
|
||||
public void addObserver(Observer<T> obs);
|
||||
public void removeObserver(Observer<T> obs);
|
||||
public void notifyObserver();
|
||||
public void notifyObserver(T value);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue