Link to home
Start Free TrialLog in
Avatar of cofactor
cofactor

asked on

what is an Observer pattern ?

what is an Observer pattern ?

please explain  with  a simple example.

though i have seen some tuts but it seems observers observes the subjects.....may be in multithreading enviornment an observer is responsible whats happening or status of the individual threds.

BTW, i am looking for an example......i have seen some example from the net but not satisfied.
looking for a nice  example...can you help ?
SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cofactor
cofactor

ASKER

>Think about a BankAccount that belongs to someone who is a bad payer

is it a loan account ? the payer has to pay ? or savings account ?
i guess its a loan account ....because you are saying "bad payer"


>he wants to observe the account and be notified when the money in the account go below a >credit limit

what do  u mean by below credit ? say the payer paid 3 months  but did not pay the  next 3 months ....so do you mean thats a below credit for the bank account ?

please explain a bit .
cofactor,

> i guess its a loan account ....because you are saying "bad payer"

It does not matter :) For the sake of clarity think about *any* bank acount. The manager wants to know if the credit has gone below zero and if it has, then he needs to send a reminding letter to the customer.
your  code looks interesting.....i know its a pseudocode

however i would like to ask few questions on your code.

here are those...

Q1.>public class BankAccount extends Observable

is this class  *observable*  defined in the java API ?  or its a mere class that developer have to code ?


Q2>public class BankManager implements Observer

same question for *observer* too.

Q3>see, you have   *notifyObeservers(new Double(this.amount));* which notifies manager....  but  this  method is not present in the BankManager class....did you forget it to code in the BankManager class ?

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks for the nice input.
you told..(though this is really not important but just to make sure..)
>. That is, if a transaction is larger than a defined amount, it ensures that the profile of the >customer matches certain criteria.

whats that security check ?  i dont  know really bacnk checks anything like that ....of course  if you are transacting more than available in your debit account, then you will get some error message in the internet banking.

BTW, really bank does match   certain criteria ? why ?

again this is not java question though.

It seems bankng applications are more robust and complex.....so many checks....so many observers...

but your explanation is nice to read.

thanks
Observer pattern _ One more Example

Consider a Shopping Cart UI, you have a panel diplaying the items in the shoppingcart.
you have ShoppingCart class which holds list of items in the cart.
your panel gets items list from the ShoppingCart object and displays it,

Normally what we would do is when ever a user adds an item to the shoppingList we could call the status changed on the shopingList panel to show the updated shoppingList.

Lets see how we can do it using Observer pattern,

make the ShoppingList class extends Observable
and your UI panel implements Observer

add observer to shoppingList class ( Observable) --   addObserver ( ur Panel )

Now if the customer adds an item to the shoppingList ( the observable) , you will notify the observer about your event ( here it is your panel),

so in the panel you can get the event details and display the updated items list.


U might not be able to appreciate Observer pattern for this scenario since there is a single observer and u might ask why should you do that??

But assume the real scenario, when ever the customer adds the item,

-->UI has to update the display
-->You have to compute the total price ( suppose in a different class )
-->Update your store database
-->compute the loyalty points for the customer
--> ...
..


what you can do is make all these action classes observer for the ShoppingList ( observable ),

so you no need to call the methods on these classes ( like status changed(), itemAdded() ... ) instead you will add them as observer,

there can be any number of observers for a observable object,

so in future if you decide to add one more action to your shoppingcart application for addingItem ( writing the new item to a file ),
you can create a new action class for that and make it observer for the shoppinglist. so your data holder shoppinglist will remain independent of the observers. so you need not modify it when ever you decide to add more actions
>BTW, really bank does match   certain criteria ? why ?

I don't really know how banks operate internally, no. It is just an example of what could be done.

One thing I know about a bank I deal with is that signatures on checks are normally not actually checked. They only (manually) actually check signatures if the check is over a certain "limit". If the check is for an amount below that limit, and the signature turns out to be invalid, then they eat the cost. This is cheaper for them than expending the labour to visually verify every signature on every check they clear.

Did any of that help?

If so, it is now time to choose an answer and grade it.

If not, perhaps a clarifying question would help.
yes...sorry....i was bit busy  in the meantime.....i  want  share the points  imaldris and  girionis