Created
May 21, 2011 19:48
-
-
Save andersondias/984836 to your computer and use it in GitHub Desktop.
An Observer design pattern implementation example based on Ruby's Observable API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
public class Price { | |
private int value; | |
public Price(int value) { | |
this.value = value; | |
} | |
public int getValue() { | |
return value; | |
} | |
public boolean equals(Price otherPrice) { | |
if(otherPrice != null) { | |
return this.getValue() == otherPrice.getValue(); | |
} else { | |
return false; | |
} | |
} | |
public static Price fetch() { | |
return new Price(new Random().nextInt(300)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Observable; | |
public class Ticker extends Observable implements Runnable { | |
public void run() { | |
Price lastPrice = null; | |
while(true) { | |
Price price = Price.fetch(); | |
System.out.format("\nCurrent price: %d", price.getValue()); | |
if(!price.equals(lastPrice)) { | |
this.setChanged(); | |
lastPrice = price; | |
notifyObservers(lastPrice); | |
try { | |
Thread.sleep(1000); | |
} catch( InterruptedException e ) { | |
break; | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TickerRunner { | |
public static void main(String[] args) throws InterruptedException { | |
Ticker ticker = new Ticker(); | |
new WarnLow(ticker, 80); | |
new WarnHigh(ticker, 120); | |
ticker.run(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Observer; | |
public abstract class Warner implements Observer { | |
protected int limit; | |
protected Price price; | |
protected Ticker ticker; | |
Warner(Ticker ticker, int limit) { | |
this.ticker = ticker; | |
this.limit = limit; | |
ticker.addObserver(this); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Observer; | |
import java.util.Observable; | |
public class WarnHigh extends Warner { | |
public WarnHigh(Ticker ticker, int limit) { | |
super(ticker, limit); | |
} | |
public void update(Observable observable, Object obj) { | |
if (obj instanceof Price) { | |
price = (Price) obj; | |
if(price.getValue() > this.limit) { | |
System.out.format("\n+++ Price above %d: %d", limit, price.getValue() ); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Observer; | |
import java.util.Observable; | |
public class WarnLow extends Warner { | |
public WarnLow(Ticker ticker, int limit) { | |
super(ticker, limit); | |
} | |
public void update(Observable observable, Object obj) { | |
if (obj instanceof Price) { | |
price = (Price) obj; | |
if(price.getValue() < this.limit) { | |
System.out.format("\n--- Price bellow %d: %d", limit, price.getValue() ); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment