DesignPattern : Observer

1. Introduction

    1. We can infer Observer Design Pattern from JDK GUI part.

    2. The AWT and Swing GUI components are using Observer Design Pattern which we will discuss later.

2. A simple example for showing the effect of using Observer Design Pattern

package edu.xmu.designPattern.DesignPattern_Observer;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ButtonTest
{
	public static void main(String[] args)
	{
		Frame frame = new Frame("Button Test");

		Button button = new Button("Press Me");

		button.addActionListener(new ButtonHandler());
		button.addActionListener(new ButtonHandler2());

		frame.add(button, BorderLayout.CENTER);

		frame.pack();
		frame.setVisible(true);
	}
}

class ButtonHandler implements ActionListener
{

	public void actionPerformed(ActionEvent e)
	{
		String label = e.getActionCommand();
		System.out.println(label);
	}

}

class ButtonHandler2 implements ActionListener
{

	public void actionPerformed(ActionEvent e)
	{
		String label = e.getActionCommand() + "2";
		System.out.println(label);
	}

}

    Comments:

        1) Function of this app is that every time we click the button, the console will print the label of the button.

        2) Pay attention to the button.addActionListener(new ButtonHandler());

            This means every time we click the button, the method called actionPerformed in ButtonHandler() will be executed automatically.

             How did JDK achieve this? Why this method will be executed AUTOMATICALLY?

        3) Observer Design Pattern Lays behind.

3. Observer Design Pattern

    1) Observer Design Pattern defines a relationship of One To Many.

        Several listeners observe the state of the target at the same time.

        Whenever the state of the target changed, all the listeners will get informed.

    2) The target in the example above can be seen as the button.

        The listeners in the example above can be seen as the instance of ButtonHandler and ButtonHandlers.

    3) Roles in Observer Design Pattern

        1) Abstract Subject:

                1. Includes a collection which contains all the reference to its Observer.

                2. Every Abstract Subject can have serveral listeners/observers.

                3. Abstract Subject exposes interfaces which can add and remove Listeners/Observers.

        2) Abstract Observer:

                1. An interface for all the Observer so that they can get informed every time the state of its subject changed.

        3) Concrete Subject:

                1. Every time its state changed, it should inform all the observer that stored in its observer collection.

        4) Concrete Observer:

                1. To perform the real action every time they get informed.

     4) Class diagram is as below


4. A simple example for explaining Observer Design Pattern

    1) AbstractSubject

package edu.xmu.designPattern.DesignPattern_Observer;

public interface AbstractSubject
{
	public void addListener(AbstractObserver abstractObserver);

	public void removeListener(AbstractObserver abstractObserver);

	public void notifyListeners(String info);
}

    2) AbstractObserver

package edu.xmu.designPattern.DesignPattern_Observer;

public interface AbstractObserver
{
	public void actionPerformed(String info);
}

    3) ConcreteSubject --> Regard this as the Button in previous example

package edu.xmu.designPattern.DesignPattern_Observer;

import java.util.ArrayList;
import java.util.List;

public class ConcreteSubject implements AbstractSubject
{
	private List<AbstractObserver> observerList = new ArrayList<AbstractObserver>();

	public void addListener(AbstractObserver observer)
	{
		observerList.add(observer);
	}

	public void removeListener(AbstractObserver observer)
	{
		observerList.remove(observer);
	}

	public void notifyListeners(String info)
	{
		for (AbstractObserver observer : observerList)
		{
			observer.actionPerformed(info);
		}
	}

}

    4) ConcreteObserverOne --> Regard this as ButtonHander in previous example

package edu.xmu.designPattern.DesignPattern_Observer;

public class ConcreteObserverOne implements AbstractObserver
{

	public void actionPerformed(String info)
	{
		System.out.println("ConcreteObserverOne ---> " + info);
	}

}

    5) ConcreteObserverTwo --> Regard this as ButtonHandler2 in previous example

package edu.xmu.designPattern.DesignPattern_Observer;

public class ConcreteObserverTwo implements AbstractObserver
{

	public void actionPerformed(String info)
	{
		System.out.println("ConcreteObserverTwo ---> " + info);
	}

}

    6) Test Case

package edu.xmu.designPattern.DesignPattern_Observer;

import org.junit.Test;

public class ObserverTest
{
	@Test
	public void test()
	{
		AbstractSubject subject = new ConcreteSubject();

		AbstractObserver observerOne = new ConcreteObserverOne();
		AbstractObserver observerTwo = new ConcreteObserverTwo();

		subject.addListener(observerOne);
		subject.addListener(observerOne);
		subject.addListener(observerTwo);

		subject.notifyListeners("Hello world!");
	}
}

    7) Output

ConcreteObserverOne ---> Hello world!
ConcreteObserverOne ---> Hello world!
ConcreteObserverTwo ---> Hello world!

    Comments:

        1) Pay attention to Line 16 in ObserverTest.java, we add one instance of observer two times to a subject.

            This instance of observer will be added two times and called two times every time the state of the subject changed.

            Because the subject keep a List to store its listeners/observers. If it keep a Set(HashSet), then oberverOne will not be added two times.

            Please pay attention to the diffence of List and Set!!!

        2) And in Swing or AWT implementation of JDK, the underlying of all kinds of listeners are stored in ArrayList and not HashSet

 Reference Links:

    1) http://en.wikipedia.org/wiki/Observer_pattern 

猜你喜欢

转载自davyjones2010.iteye.com/blog/1882980