Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Applied Java™ Patterns - Stephen Stelting, Olav Maassen.pdf
Скачиваний:
202
Добавлен:
24.05.2014
Размер:
2.84 Mб
Скачать

Example

Application users want the option of undoing previous commands. To support that functionality, a history list is needed. That history list has to be accessible from everywhere in the PIM and only one instance of it is needed. Therefore, it’s a perfect candidate for the Singleton pattern.

Example 1.20 HistoryList.java

1.import java.util.ArrayList;

2.import java.util.Collections;

3.import java.util.List;

4.public class HistoryList{

5.private List history = Collections.synchronizedList(new ArrayList());

6.private static HistoryList instance = new HistoryList();

7.

8. private HistoryList(){ }

9.

10.public static HistoryList getInstance(){

11.return instance;

12.}

13.

14.public void addCommand(String command){

15.history.add(command);

16.}

17.

18.public Object undoCommand(){

19.return history.remove(history.size() - 1);

20.}

21.

22.public String toString(){

23.StringBuffer result = new StringBuffer();

24.for (int i = 0; i < history.size(); i++){

25.

result.append(" ");

26.

result.append(history.get(i));

27.

result.append("\n");

28.}

29.return result.toString();

30.}

31.}

The HistoryList maintains a static reference to an instance of itself, has a private constructor, and uses a static method getInstance to provide a single history list object to all parts of the PIM. The additional variable in HistoryList, history, is a List object used to track the command strings. The HistoryList provides two methods, addCommand and undoCommand to support adding and removing commands from the list.

33

Chapter 2. Behavioral Patterns

Introduction to Behavioral Patterns

Behavioral patterns are concerned with the flow of control through a system. Some ways of organizing control within a system can yield great benefits in both efficiency and maintainability of that system. Behavioral patterns distill the essence of proven practices into readily understood, well known, and easy-to-apply heuristics.

Behavioral patterns covered in this chapter are as follows:

Chain of Responsibility – To establish a chain within a system, so that a message can either be handled at the level where it is first received, or be directed to an object that can handle it.

Command – To wrap a command in an object so that it can be stored, passed into methods, and returned like any other object.

Interpreter – To define an interpreter for a language.

Iterator – To provide a consistent way to sequentially access items in a collection that is independent of and separate from the underlying collection.

Mediator – To simplify communication among objects in a system by introducing a single object that manages message distribution among the others.

Memento – To preserve a “snapshot” of an object’s state, so that the object can return to its original state without having to reveal its content to the rest of the world.

Observer – To provide a way for a component to flexibly broadcast messages to interested receivers.

State – To easily change an object’s behavior at runtime.

Strategy – To define a group of classes that represent a set of possible behaviors. These behaviors can then be flexibly plugged into an application, changing the functionality on the fly.

Visitor – To provide a maintainable, easy way to perform actions for a family of classes. Visitor centralizes the behaviors and allows them to be modified or extended without changing the classes they operate on.

Template Method – To provide a method that allows subclasses to override parts of the method without rewriting it.

Note:

MVC, or Model-View-Controller, can be considered a behavioral pattern. However, because of its wide-ranging implications for entire systems, particular in view of the J2EE specification recommendations for servlets and JSPs, we included it in the “ System Patterns ” chapter on page 208.

34