Design Patterns

Table of Contents

Creational
Abstract Factory
Builder
Factory Method
Prototype
Singleton

Structural
Adapter
Bridge
Composite
Decorator
Facade
Flyweight
Proxy

Behavioral
Chain of Responsibility
Command
Interpreter
Iterator
Mediator
Memento
Observer
State
Strategy
Template Method
Visitor

Java EE
Model View Controller
Business Delegate
Composite Entity
Data Access Object
Front Controller
Intercepting Filter
Service Locator
Transfer Object

UML & OOD
Unified Modeling Language
SOLID

Observer


The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.



Observer or Publish/Subscribe in Java recognizable by behavioral methods which invokes a method on an instance of another abstract/interface type, depending on own state

import java.util.Observable;
import java.util.Scanner;

class EventSource extends Observable implements Runnable {
	public void run() {
		while (true) {
			String response = new Scanner(System.in).next();
			setChanged();
			notifyObservers(response);
		}
	}
}

import java.util.Observable;
import java.util.Observer;

public class MyApp {
	public static void main(String[] args) {
		System.out.println("Enter Text: ");
		EventSource eventSource = new EventSource();

		eventSource.addObserver(new Observer() {
			public void update(Observable obj, Object arg) {
				System.out.println("Received response: " + arg);
			}
		});

		new Thread(eventSource).start();
	}
}
/*
interface Observer{
	public void update(Observable obj, Object arg);
}
*/