Observer Uml & Java - java

I really need your help for a question: the right implementation of pattern Observer. The fact is that I have no guarantees about doing right so I just would like to know if there are inconsistencies between UML and code below, please report me any minimum mistake/improvable detail in both parties. I would be so much grateful to anyone, thanks in advance.
UML:
JAVA CODE:
public interface Subject {
public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyObservers();
}
public class ConcreteSubject implements Subject {
private int my_state;
private ArrayList<Observer> observers;
public ConcreteSubject(){
observers=new ArrayList<Observer>();
}
public void registerObserver(Observer o){
observers.add(o);
}
public void removeObserver(Observer o){
observers.remove(o);
}
public void notifyObservers(){
for(Observer o : observers)
o.update();
}
//PULL Mode -> Observer musts grab the state of the subject
public int getState(){
return my_state;
}
public void setState(int state){
my_state=state;
}
}
public interface Observer {
public void update(); //In PULL mode update don't need the state of the subject in argument
}
public class ConcreteObserver implements Observer {
private ConcreteSubject my_subject;
public ConcreteObserver(ConcreteSubject subject){
my_subject=subject;
}
public void update(){
System.out.println("The state is: "+my_subject.getState());
}
}

You can consider to implement Subject as abstract class, so ConcreteSubject will care only about change of the state and notification of the observers.
With this approach you can have more ConcreteSubjects.
Check example of the Observer pattern.

Related

Deduce derived types polymorphically

Whats the recommended design approach/alternative to the situation below:
BaseCalculator:
BaseType prepareData()
useData(BaseType)
Derived calculators use derived type to override base functionality -
DerivedCalculator1:
BaseType prepareData(){ return DerivedType1}
useData(BaseType t1){ DerivedType1 t=(DerivedType1)t1 //typecast down and proceed....}
DerivedCalculator2
BaseType prepareData(){ return DerivedType2}
useData(BaseType t1){ DerivedType2 t=(DerivedType2)t1 //typecast down and proceed....}
Is there a design approach to avoid typecasting by the derived classes - as it always leaves the gate open for a run-time mishap?
One alternative is to move the polymorphic behavior into the implementations of the BaseType rather than in the implementations of BaseCalculator. For example:
public interface BaseType {
public void process(Calculator calc);
}
public class DerivedType1 implements BaseType {
#Override
public void process(Calculator calc) {
// Do something specific to derived type 1
}
}
public class DerivedType2 implements BaseType {
#Override
public void process(Calculator calc) {
// Do something specific to derived type 2
}
}
public class Calculator {
public void doSomething(BaseType bt) {
bt.process(this);
}
}
If that type of solution is insufficient, a more complex solution is the Visitor Pattern. The Visitor Pattern allows any arbitrary BaseType object to be handled by any arbitrary BaseCalculator using double-dispatch. The catch is that all BaseCalculator implementations must have an method to handle each of the BaseType implementations. For example:
public interface BaseType {
public void process(Calculator calc);
}
public class DerivedType1 implements BaseType {
#Override
public void process(Calculator calc) {
// Do something specific to derived type 1
}
}
public class DerivedType2 implements BaseType {
#Override
public void process(Calculator calc) {
// Do something specific to derived type 2
}
}
public interface BaseCalculator {
public void handle(DerivedType1 dt);
public void handle(DerivedType2 dt);
}
public class DerviedCalculator1 implements BaseCalculator {
#Override
public void handle(DerivedType1 dt) {
dt.process(this);
}
#Override
public void handle(DerivedType2 dt) {
dt.process(this);
}
}
public class DerviedCalculator2 implements BaseCalculator {
#Override
public void handle(DerivedType1 dt) {
dt.process(this);
}
#Override
public void handle(DerivedType2 dt) {
dt.process(this);
}
}

java.util.ConcurrentModificationException while using ExecutorService

I am working on some project in which I am using Observer design Pattern.
Subject class is :
public class Subject {
private List<Observer> observers = new ArrayList<Observer>();
private int state;
public void setState(int state) {
this.state = state;
notifyAllObservers();
}
public void attach(Observer observer){
observers.add(observer);
}
public void notifyAllObservers(){
for (Observer observer : observers) {
observer.update();
}
}
public void deattach(Observer observer) {
observers.remove(observer);
}
}
Observer Interface is:
public abstract class Observer implements Runnable{
protected Subject subject;
public abstract void update();
public abstract void process();
}
One of the Observer Named as HexObserver is:
public class HexaObserver extends Observer {
private ExecutorService threadpool = Executors.newFixedThreadPool(10);
public HexaObserver(Subject subject) {
this.subject = subject;
this.subject.attach(this);
}
#Override
public void update() {
System.out.println("Hex String: "
+ Integer.toHexString(subject.getState()));
Future future = threadpool.submit(new HexaObserver(subject));
}
#Override
public void run() {
// TODO Auto-generated method stub
System.out.println("In run :D :D :D");
}
#Override
public void process() {
// TODO
}
}
Class to test this is:
public class Demo {
public static void main(String[] args) {
Subject subject = new Subject();
HexaObserver hob = new HexaObserver(subject);
System.out.println("First state change: 15");
subject.setState(15);
}
}
When I tried to run this this is giving some Error:
First state change: 15
Hex String: f
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859)
at java.util.ArrayList$Itr.next(ArrayList.java:831)
at Observer.Subject.notifyAllObservers(Subject.java:23)
at Observer.Subject.setState(Subject.java:15)
at Observer.Demo.main(Demo.java:12)
In run :D :D :D
I didn't get why I am getting this error as ConcurrentModificationException is thrown out when we try to modify some Object concurrently when it is not permissible.
Am I missing something ?
Two things are happening at the same time: you are iterating over observers and adding an element to observers. That causes your ConcurrentModificationException
In general there are at least three things you can do:
use a synchronized collection
thread-safely copy the collection and iterate on a copy
manually synchronize all access to observers with synchronized block:
 
public void attach(Observer observer){
synchronized(observers){
observers.add(observer);
}
}
public void notifyAllObservers(){
synchronized(observers){
for (Observer observer : observers) {
observer.update();
}
}
}
public void deattach(Observer observer) {
synchronized(observers){
observers.remove(observer);
}
}
you can also mark whole methods as synchronized, but then they will synchronize on an instance of Subject and not on the collection instance.
However, in your case the problem is connected to what your update() does.
Are you sure your HexaObserver's update should create a new HexaObserver? Because you are adding a new instance of the same class to a collection which already contains that instance.
ConcurrentModificationException is usually the sign that you used an Iterator on a Collection and that while iterating, you also modified the underlying Collection (remember that foreach-expression are actually a shortcut for using Iterator). The only way to solve this, is to iterate on a copy of the original collection. If you are in a multi-threaded environment, you also need to ensure that the copy of the collection is done in a thread-safe way.
So you could for example have:
public class Subject {
private List<Observer> observers = new Vector<Observer>();
private int state;
public void setState(int state) {
this.state = state;
notifyAllObservers();
}
public void attach(Observer observer){
observers.add(observer);
}
public void notifyAllObservers(){
for (Observer observer : ((List<Observer)observers.clone())) {
observer.update();
}
}
public void deattach(Observer observer) {
observers.remove(observer);
}
}

Java Observer Pattern - How to remove observers during update(notify) loop/iteration?

I am very new to java so sorry in advance if anything I say sounds newbish, be gentle.
I have implemented a basic Observer Pattern. Some observers should only listen to one update and then immediately remove themselves from the observers/listeners list. However, whenever I tried doing that I got the famous java.util.concurrentmodificationexception error.
I'm obviously getting this error because I'm changing the list while still iterating over it, yet I am still unsure what is the right solution. I'm wondering if I'm even doing this the right way. If I am, what would be the needed fix to make it work? And if I'm not, I'd like to get suggestions for a better way of achieving what I'm trying to do.
Here's my code:
public interface Listener {
public void onValueChange(double newValue);
}
public class Observed {
private int value;
List<Listener> listeners = new ArrayList<>();
public void addListener(Listener toAdd) {
listeners.add(toAdd);
}
public void removeListener(Listener toRemove) {
listeners.remove(toRemove);
}
public void changeValue(double newValue) {
value = newValue;
for (Listener l : listeners) l.onValueChange(newValue);
}
}
public class SomeClassA implements Listener{
private Observed observed;
SomeClassA(Observed observed) {
this.observed = observed;
}
#Override
public void onValueChange(double newValue) {
System.out.println(newValue);
observed.removeListener(this);
}
}
public class SomeClassB implements Listener{
#Override
public void onValueChange(double newValue) {
System.out.println(newValue);
}
}
public class ObserverTest {
public static void main(String[] args) {
Observed observed = new Observed();
SomeClassA objectA = new SomeClassA(observed);
SomeClassB objectB = new SomeClassB();
observed.addListener(objectB);
observed.addListener(objectA);
observed.changeValue(4);
}
}
one ways is to go fo CopyOnWriteArraylist instead of ArrayList .
CopyOnWriteArraylist is a thread-safe variant of ArrayList in which
all mutative operations (add, set, and so on) are implemented by
making a fresh copy of the underlying array.
Reason why its thrown in your case
you are modifying a collection directly while it is iterating over the collection under method changeValue()
You can not remove items from a collection while you are iterating over it. That is, unless you use the Iterator#remove method. Since that is not a possibility in this case, an alternative is make a copy of your listener list and iterate over that instead. In that case the original listener list is free to be manipulated by the individual listeners:
public void changeValue(double newValue) {
value = newValue;
List<Listener> copyOfListeners = new ArrayList<Listener>(listeners);
for(Listener l : copyOfListeners) {
l.onValueChange(newValue);
}
}
the code below works, so you can try whatever it does.
import java.util.Observable;
import java.util.Observer;
class Model extends Observable {
public void setX(double x) {
this.x=x;
System.out.println("setting x to "+x);
setChanged();
notifyObservers();
}
double x;
}
class A implements Observer {
A(Model model) {
this.model=model;
}
#Override public void update(Observable arg0,Object arg1) {
System.out.println(getClass().getName()+" "+((Model)arg0).x);
((Model)arg0).deleteObserver(this);
}
Model model;
}
class B implements Observer {
#Override public void update(Observable arg0,Object arg1) {
System.out.println(getClass().getName()+" "+((Model)arg0).x);
}
}
public class So19197579 {
public static void main(String[] arguments) {
Model model=new Model();
model.addObserver(new A(model));
model.addObserver(new B());
model.setX(4);
model.setX(8);
}
}

Overloaded method calls redesign

I have an interface IAction that has one generic method:
public interface IAction {
void doAction(ISignal sig, IState state);
}
Another class IActionAbstract then implements the IAction interface and calls overloaded methods with instanceof clauses:
public abstract class IActionAbstract implements IAction
{
#Override
public void doAction(ISignal sig, IState state)
{
if(sig instanceof ISignal1 && state instanceof IState1)
{
doOther((ISignal1)sig, (IState1)state);
}
else if(sig instanceof ISignal2 && state instanceof IState1)
{
doOther((ISignal2)sig, (IState1)state);
}
else if(sig instanceof ISignal1 && state instanceof IState2)
{
doOther((ISignal1)sig, (IState2)state);
}
}
abstract void doOther(ISignal1 sig, IState1 state);
abstract void doOther(ISignal2 sig, IState1 state);
abstract void doOther(ISignal1 sig, IState2 state);
}
I would like to remove the instanceof checks and replace with generics or redesign, but without adding more methods to IAction. I see how to do this with reflection, but would like to avoid if possible.
Edit: Removed generics since they are not required. I will try and explain more to give a better idea of this approach. The IActionAbstract file might be generated with the developer making an impl to implement the methods. ISignal and IState together make the method unique and can be thought of as a state machine state and signal.
Usage of the classes would look like in pseudo-code:
List<IAction> actions;
actions.get(i).doAction(ISignal1, IState1);
actions.get(i).doAction(ISignal2, IState2);
and so on...
Looks to me like you want separate implementations of IAction, i.e.
// generic interface declaration
public interface IAction<T extends ISignal, S extends IState> {
void doAction(T sig, S state);
}
// typed implementations of the generic interface
public class Action1 implements IAction<Signal1, State1> {
doAction(Signal1 sig, State1 state) {
// impl
}
}
// another typed implementations of the generic interface
public class Action2 implements IAction<Signal2, State2> {
doAction(Signal2 sig, State2 state) {
// impl
}
}
...and so on. Otherwise you're not even using generics.
I'm not quite sure what you're looking for. I agree with #claesv that your approach probably isn't necessary. Here is my approach:
public class GenericsQuestion {
public static void main(String[] args) {
ISignal sig = new Signal1();
IState state = new State1();
Strategy.getStrategyForSignalAndState(sig.getClass(), state.getClass()).doOther(sig, state);
}
}
class SignalAndState {
private Class<? extends IState> state;
private Class<? extends ISignal> signal;
/**
*
*/
public SignalAndState(Class<? extends ISignal> signal, Class<? extends IState> state2) {
// save state and signal
}
// equals & hashcode
}
enum Strategy {
ONE {
#Override
public void doOther(ISignal sig, IState state) {
}
},
TWO {
#Override
public void doOther(ISignal sig, IState state) {
}
},
THREE {
#Override
public void doOther(ISignal sig, IState state) {
}
};
private static final Map<SignalAndState, Strategy> STRATEGIES = new HashMap<SignalAndState, Strategy>();
static {
STRATEGIES.put(new SignalAndState(Signal1.class, State1.class), ONE);
STRATEGIES.put(new SignalAndState(Signal1.class, State2.class), TWO);
STRATEGIES.put(new SignalAndState(Signal2.class, State1.class), THREE);
}
public static Strategy getStrategyForSignalAndState(Class<? extends ISignal> sig, Class<? extends IState> state) {
return STRATEGIES.get(new SignalAndState(sig, state));
}
public abstract void doOther(ISignal sig, IState state);
}
I my eyes, this would be more elegant and flexible than using instanceof.
You could probably improve this by using EnumMap, but I don't use that much and am unsure about the benefits and/or usage. Just a hint if you'd like to investigate further.

Having trouble getting the observer pattern working

I have been trying to no avail to get the observer pattern working in a relatively simple application.
I have 4 GUI classes
StarterClass (contains a CompositeWordLists and a CompositeWordListData)
CompositeWordLists (contains many CompositeListItem/s and a CompositeWordListData)
CompositeListItem
CompositeWordListData (Contains a DialogWordData)
DialogWordData
Here is my Observable
interface Observable<T> {
void addObserver(T o);
void removeObserver(T o);
void removeAllObservers();
void notifyObservers();
}
And I am creating Observers like this:
public class Observers {
private Observers(){};
interface WordListsObserver {
public void update(CompositeWordLists o);
}
interface ListItemObserver {
public void update(CompositeListItem o);
}
}
Basically I am having trouble with specifying the sort of event that occurred. For example, the CompositeWordLists class needs to know when a CompositeListItem is deleted, saved edited etc but I only have one update method ... my brain hurts now!
What is a better way of doing this?
UPDATE
Still having trouble with this, I added events and changed Observable and Observers but now I have type safety problems.
public class Observers {
private Observers(){};
/**
* #param <T> the object that is passed from the Observable
*/
interface ObservableEvent<T> {
T getEventObject();
}
/**
* Get notified about Authentication Attempts
*/
interface ObserverAuthenticationAttempt {
/**
* #param e true if authentication was successful
*/
public void update(ObservableEvent<Boolean> e);
}
/**
* Get notified about a Word Deletion
*/
interface ObserverWordDeleted {
/**
* #param e the id of the word that was deleted
*/
public void update(ObservableEvent<Integer> e);
}
}
The Observable Interface now looks like this
interface Observable<T> {
void addObserver(T o);
void removeObserver(T o);
void removeAllObservers();
<K> void notifyObservers(Observers.ObservableEvent<K> e);
}
The problem is that when I implement this I get and would have to cast K to the appropriate type, not really what I want to do.
#Override
public <K> void notifyObservers(ObservableEvent<K> e) {
for(Observers.ObserverAuthenticationAttempt o : this.observers)
o.update(e);
}
What am I doing wrong?
update 2
Actually it works better with an Observable like this, but I still need to specify the correct EventType in two different places.
interface Observable<T,K> {
void addObserver(T o);
void removeObserver(T o);
void removeAllObservers();
void notifyObservers(Observers.ObservableEvent<K> e);
}
You do not need to parametrise the Observers, but you need to parametrize the events.
public interface Observer<T> {
void notify(T event);
}
An example event:
public class WordListUpateEvent {
private final int changedIndex;
public WordListUpateEvent(int changedIndex) {
this.changedIndex = changedIndex;
}
public int getChangedIndex() {
return changedIndex;
}
}
Then you can have different interface of it for example:
public interface WordListObserver extends Observer<WordListUpateEvent> {}
and its implementations
public class ConcreteWordListObserverA implements WordListObserver {
#Override
public void notify(WordListUpateEvent event) {
System.out.println("update item at index: " + event.getChangedIndex());
}
}
on the other hand you need your Observable interface, i have splitted it in two interface in order ti make the notifyObservers method not public to the observers (you will see it later):
public interface Observable<T> extends ObservableRegistration<T> {
void notifyObservers(T event);
}
public interface ObservableRegistration<T> {
void addObserver(Observer<T> o);
void removeObserver(Observer<T> o);
void removeAllObservers();
}
If you would have several observables in a subject, you can not implemnt the Observalbe interface direct to your subject, so you need a seperate implementation class:
public class ObservableImpl<T> implements Observable<T>{
private final List<Observer<T>> observers = new ArrayList<Observer<T>>();
#Override
public void addObserver(Observer<T> o) {
this.observers.add(o);
}
#Override
public void removeObserver(Observer<T> o) {
this.observers.remove(o);
}
#Override
public void removeAllObservers() {
this.observers.clear();
}
#Override
public void notifyObservers(T event) {
for(Observer<T> observer : observers) {
observer.notify(event);
}
}
}
Now you can use the implementation in your subject:
public class Subject {
private Observable<WordListUpateEvent> wordListObservable = new ObservableImpl<WordListUpateEvent>();
//private Subject<OtherEvent> otherObservable = new ObservableImpl<WordListUpateEvent>();
public ObservableRegistration<WordListUpateEvent> getWordListObservableRegistration() {
return this.wordListObservable;
}
// public ObservableRegistration<OtherEvent> getOtherRegistration() {
// return this.otherObservable;
// }
public void doSomething() {
this.wordListObservable.notifyObservers(new WordListUpateEvent(42));
}
}
And this is how you can connect the observer and the subject:
public class Start {
public static void main(String[] args) {
Subject subject = new Subject();
subject.getWordListObservableRegistration().addObserver(new ConcreteWordListObserverA());
subject.getWordListObservableRegistration().addObserver(new ConcreteWordListObserverA());
subject.doSomething();
}
}
I would create an Observer interface, containing a public void update(ObservableEvent oe) method, and an ObserverEvent interface. After that, you can create specific class for each of your events.
http://download.oracle.com/javase/1.4.2/docs/api/java/util/Observer.html
http://www.java2s.com/Code/Java/Design-Pattern/Observableandobserver.htm
The Java Observer's update method has the Object argument. You can pass any Object, thus you can create your own "UpdateMessage" Object that can contain the updated object and additional information about what happend (deleted, saved etc.).

Categories

Resources