algorithms (22) Design Patterns (20) java (19) linux (14) Snippet (13) service mix (6) soa (4)



Observer Design Pattern

This pattern is used in publisher-consumer kind of situations


This pattern is used where a change in data which is observable by one or many observers,The pattern is such that no matter what kind of Observers ATMObserver , OnlineAcObsever)  come the Observable or the subject(BankAccount) has no impact.So tomorrow if there is a need to add MobileObserver for BankAccount, that can just be added without any changes and implementing AccountObserver.

The Subject or Observable concrete class,in this case a bank account which is observable.The Observers OnlineACObserver ATMACObserver register with this class and get updates about the bank account.

public class BankAccount implements SubjectObservable {
public ArrayList observers = new ArrayList();

String balance;

String account;

public void deregisterObserver(AccountObserver o) {

public void notifyObservers() {
for (int i = 0; i < this.observers.size(); i++) {
AccountObserver observer = (AccountObserver) this.observers.get(i);
observer.update("ACCOUNTNUM", "BALANCE < 200");

public void registerObserver(AccountObserver o) {

public void setBalance(String balance) {
this.balance = balance;

The Concrete implementation of Observer take the case of ATMACObserver this observer registers with the BankAccount and gets invoked by the Subject when there is a change or update in the Subject.

public class ATMObserver implements AccountObserver {

String message;

SubjectObservable bankAccount;

public ATMObserver(SubjectObservable bankAccount) {
this.bankAccount = bankAccount;

public void update(String accountNumber, String balance) {
this.message = "welcome to ATM " + accountNumber + " " + balance;

The client code can be as follows.

class Client {
public static void main(String[] args) {
// creating observable Bank Account
SubjectObservable bankAccount = new BankAccount();
// Observer 1 onlin
OnlineAcObserver online = new OnlineAcObserver(bankAccount);
// Observer 2 ATM
ATMObserver atm = new ATMObserver(bankAccount);
//update account values which updates the observer


  1. This is why a lot of credible fashion blogs are religiously followed by the fashion industry generating thousands in advertising revenue. This makes fashion a lucrative blogging topic. Starting a fashion blog required first choosing the right WordPress fashion theme.

  2. It is in truth a commendable perfect work of art.

  3. something behind me, similar to a stick-up while throwing out. The XS1000 has a lightweight magnesium amalgam outline that is harder than any reel I have ever 손오공게임

  4. I agree with a lot of the points you made in this article. If you are looking for the Driveway Resurfacing Sydney, then visit Sydney Wide Spray Pave. I love your content, they are very nice and very useful to us and this text is worth everyone’s attention.


Search 24 Bytes