Date post: | 22-Jun-2015 |
Category: |
Documents |
Upload: | aren-zomorodian |
View: | 147 times |
Download: | 3 times |
Instructor: Dr. R. Zavodnik
T.A.: Aren Zomorodian
CIS 280: Advanced OOP Design and Development
Lab Session #3:
Patterns Review – Part 1
I•Mediator Pattern
II•Observer Pattern
Outline
Mediator Pattern• Defines an object that encapsulates how a set of objects
interact. It is a behavioral pattern due to the way it can alter the program's
running behavior
Mediator Pattern• Create an “intermediary” that decouples “senders” from
“receivers”
• Producers and Consumers are coupled only to the Mediator
• Consumers are coupled only to the Mediator
• The Mediator arbitrates the storing and retrieving of messages
Mediator Pattern
VS.
http://sourcemaking.com/design_patterns/mediator
Mediator Pattern - Java Implementation/*** StockExchange – this is the mediator class*/public class StockExchange {
public static void doTransaction (String typeOfTransaction, int quantity, Scrip scrip, Trader trader) { Transaction transaction = new Transaction(typeOfTransaction, quantity, scrip, trader);// try and match the current transaction// with the ones saved in DB and find out// whether a counter transaction is there or// are there many such transactions which could// fulfill requirement of this transaction.matchTransaction(transaction)}
public static getPrice (Scrip scrip) { // try and match this transaction with all// the saved ones. If they match till whatever extent// trade for that. Then save, with status Traded for// number of shares traded and save the rest as New. } }// End of class
Mediator Pattern - Java Implementation/*** Trader1 – this trader wants to sell 100 shares of company XYZ*/
public class Trader1 { public void doTransaction (String typeOfTransaction, int quantity) { int expectedPrice = 320;Scrip scrip = new Scrip(“XYZ”);int price = StockExchange.getPrice(scrip); if(typeOfTransaction.equals(“SELL”)){
if(price >= expectedPrice){StockExchange.doTransaction(“SELL”, 100, scrip, trader1);} }else if(typeOfTransaction.equals(“BUY”)){ if(price <= expectedPrice){StockExchange.doTransaction(“BUY”, 100, scrip, trader1);} } } }// End of class
Mediator Pattern - Java Implementation/*** Trader2 – this trader wants to buyl 100 shares of company XYZ*/
public class Trader2 { public void doTransaction (String typeOfTransaction, int quantity) { int expectedPrice = 320;Scrip scrip = new Scrip(“XYZ”);int price = StockExchange.getPrice(scrip);
if(typeOfTransaction.equals(“SELL”)){
if(price >= expectedPrice){StockExchange.doTransaction(“SELL”, 100, scrip, trader2);} }else if(typeOfTransaction.equals(“BUY”)){ if(price <= expectedPrice){StockExchange.doTransaction(“BUY”, 100, scrip, trader2);} } } }// End of class
Observer Pattern• Observer Pattern is defined as:
Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically
Simply it is used when changes in objects should be monitored and specific action should be taken after those changes!
Observer Pattern – Java Implementati on
interface Observer {public void update(float interest);}
interface Subject { public void registerObserver(Observer observer);
public void removeObserver(Observer observer);
public void notifyObservers();}
Observer and Subject Interfaces
Observer Pattern – Java Implementati on
class Loan implements Subject { private ArrayList<Observer> observers = new ArrayList<Observer>(); private String type; private float interest; private String bank;
public Loan(String type, float interest, String bank) { this.type = type;this.interest = interest; this.bank = bank; }
public float getInterest() {return interest;}
public void setInterest(float interest) {this.interest = interest; notifyObservers(); }
public String getBank() {return this.bank;}
public String getType() {return this.type;}
Subject
Observer Pattern – Java Implementati on
@Override public void registerObserver(Observer observer) {observers.add(observer);}
@Override public void removeObserver(Observer observer) {observers.remove(observer);}
@Override public void notifyObservers() {
for (Observer ob : observers) {System.out.println("Notifying Observers on change in Loan interest
rate");ob.update(this.interest);}
}
}
Subject (cont.)
Observer Pattern – Java Implementati on
class Newspaper implements Observer { @Override public void update(float interest) {
System.out.println("Newspaper: Interest Rate updated, new Rate is: " + interest);
}}
class Internet implements Observer {@Overridepublic void update(float interest) {System.out.println("Internet: Interest Rate updated, new Rate is: "+ interest); }
}
Observer
Observer Pattern – Java Implementati on
public class ObserverTest {
public static void main(String args[]) { // this will maintain all loans information
Newspaper printMedia = new Newspaper();
Internet onlineMedia = new Internet();
Loan personalLoan = new Loan("Personal Loan", 12.5, "Standard Charterd");
personalLoan.registerObserver(printMedia);
personalLoan.registerObserver(onlineMedia);
personalLoan.setInterest(3.5);}
}
Main