Observer Pattern:Observer Pattern defines a one-to-many dependency between objects so that when on object changes state, all its dependents are notified and updated accordingly.
Example 1 using custom Observable and Observer Interface
Observable.java
package com.observerpattern.example; public interface Observable { public void registerObserver(Observer observer); public void notifyObsevers(); }
Observer.java
package com.observerpattern.example; public interface Observer { public void update(float salary); }
Account.java
package com.observerpattern.example; import java.util.ArrayList; import java.util.List; public class Account implements Observable { float salary; ListlistObserver = new ArrayList<>(); public void setSalary(float salary) { this.salary = salary; System.out.println("Salary Credited successfully into Account"); salaryCredited(); } public void salaryCredited() { notifyObsevers(); } @Override public void registerObserver(Observer observer) { listObserver.add(observer); } @Override public void notifyObsevers() { for (Observer observer : listObserver) { observer.update(this.salary); } } public float getSalary() { return salary; } }
Bank.java
package com.observerpattern.example; public class Bank implements Observer { Observable account; float salary; public Bank(Observable account) { this.account = account; account.registerObserver(this); } @Override public void update(float salary) { this.salary = salary; if(this.salary > 0){ System.out.println("EMI Amount successfully deducted from your Account"); }else{ System.out.println("Account doesn't have sufficient balance for emi deduction"); } } }
AccountTestDrive.java
package com.observerpattern.example; public class AccountTestDrive { public static void main(String[] args) { Account account = new Account(); Bank bank = new Bank(account); //when salary is credited into Account(Observable),Bank(Observer) will be notified account.setSalary(100000); } }Example 2 using java.util Observable Interface and Observer class
Account.java
package com.observerpattern.example2; import java.util.Observable; public class Account extends Observable { float salary; public void setSalary(float salary) { this.salary = salary; System.out.println("Salary Credited successfully into Account"); salaryCredited(); } public void salaryCredited(){ setChanged(); notifyObservers(); } public float getSalary() { return salary; } }
Bank.java
package com.observerpattern.example2; import java.util.Observable; import java.util.Observer; public class Bank implements Observer { Observable account; public Bank(Observable account) { this.account = account; account.addObserver(this); } @Override public void update(Observable account, Object object) { if(account instanceof Observable){ Account salaryAccount = (Account) account; if(salaryAccount.getSalary() > 0){ System.out.println("EMI Amount successfully deducted from your Account"); }else{ System.out.println("Account doesn't have sufficient balance for emi deduction"); } } } }
AccountTestDrive.java
package com.observerpattern.example; public class AccountTestDrive { public static void main(String[] args) { Account account = new Account(); Bank bank = new Bank(account); //when salary is credited into Account(Observable),Bank(Observer) will be notified account.setSalary(100000); } }Home
No comments:
Post a Comment