+ All Categories
Home > Documents > Cis 280 patterns

Cis 280 patterns

Date post: 22-Jun-2015
Category:
Upload: aren-zomorodian
View: 147 times
Download: 3 times
Share this document with a friend
14
Instructo r: Dr. R. Zavodnik T.A.: Aren Zomorodian CIS 280: Advanced OOP Design and Development Lab Session #3: Patterns Review – Part 1
Transcript
Page 1: Cis 280   patterns

Instructor: Dr. R. Zavodnik

T.A.: Aren Zomorodian

CIS 280: Advanced OOP Design and Development

Lab Session #3:

Patterns Review – Part 1

Page 2: Cis 280   patterns

I•Mediator Pattern

II•Observer Pattern

Outline

Page 3: Cis 280   patterns

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

Page 4: Cis 280   patterns

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

Page 5: Cis 280   patterns

Mediator Pattern

VS.

http://sourcemaking.com/design_patterns/mediator

Page 6: Cis 280   patterns

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

Page 7: Cis 280   patterns

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

Page 8: Cis 280   patterns

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

Page 9: Cis 280   patterns

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!

Page 10: Cis 280   patterns

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

Page 11: Cis 280   patterns

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

Page 12: Cis 280   patterns

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.)

Page 13: Cis 280   patterns

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

Page 14: Cis 280   patterns

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


Recommended