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

Factory Method Design Pattern

This is one of the Creational Design Pattern,The use of this pattern is to make the classes loosely coupled.
Client Code which calls creation of computer and who only knows which object to create ,In this case It asks to create an Apple Computer and Give it using ComputerFactoryIF.
Computer aComputer = ComputerFactoryIF.createComputer("apple");

The ComputerFactoryIF the interface of all the concrete factories checks as to which type of Computer is to be created and delegates to the appropriate ComputerFactory in this case AppleComputerFac

public class ComputerFactoryIF {

public static Computer createComputer(String string) {
Computer aComp = null;
if (string.equals("apple"))
aComp = AppleComputerFac.createComputer();
else if (string.equals("dell"))
aComp = DellComputerFac.createComputer();
return aComp;

There After the corresponding factory creates an instance of the computer and returns(In this case AppleComputer Object).

public class AppleComputerFac extends ComputerFactoryIF {
public static Computer createComputer() {
return new AppleComputer();

public class AppleComputer extends Computer {
AppleComputer() { = "AppleComputer";


Search 24 Bytes