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

Strategy

Also known as Policy

Pattern Properties

Type: Behavioral

Level: Component

Purpose

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.

Introduction

Suppose the PIM contains a list of contacts. As the number of contacts grows, you might want to provide a way to sort entries and summarize the contact information.

To do this, you could make a collection class to store contacts in memory, sort the objects, and summarize their information. While this would provide a solution in the short term, a number of problems could surface (that is, rear their hideous, slime-drenched heads) over time. The most serious drawback is that the solution cannot be easily modified or extended. Any time you want to add a new variation of sorting or summarizing functionality, you would need to change the collection class itself. What’s more, as the number of sorting or summarizing options increases, the size and complexity of the code in the collection grows, making it harder to debug and maintain.

What if you developed a series of classes instead, in which each class handles a specific way to sort or summarize the contact data? The collection class delegates the tasks to one of these classes, and so has different approaches

or strategies to perform its task without the complex code of the other approach.

 

 

 

Y

The Strategy pattern relies on objects having state and behavior. By replacingLone object with another you can change behavior. And

although this produces more classes, each class is easy to maintain andFthe overall solution is very extensible.

Applicability

 

 

M

 

A

 

 

Use the Strategy pattern when:

E

 

T

 

 

You have a variety of ways to perform an action.

You might not know which approach to use until runtime.

You want to easily add to the possible ways to perform an action.

You want to keep the code maintainable as you add behaviors.

Description

There are often many ways to perform the same task. Sorting, for example, can be performed with a number of well-documented algorithms such as quick-sort and bubble sort, or by using multiple fields, or according to different criteria. When an object has a number of possible ways to accomplish its goals, it becomes complex and difficult to manage. Imagine the coding overhead required to produce a class to represent a document and save it in a variety of formats: a plain text file, a StarOffice document, and a Postscript file, for instance. As the number and complexity of the formats increase, the effort of managing the code in a single class becomes prohibitive.

In such cases, you can use the Strategy pattern to maintain a balance between flexibility and complexity. The pattern separates behaviors from an object, representing them in a separate class hierarchy. The object then uses the behavior that satisfies its requirements at a given time. For the document example, you could develop a class to save the document in each format, and their collectively defined by a superclass or interface.

The Strategy pattern manages sets of basic algorithms,TEAM FLYsuchPRESENTSas searching and sorting. You can also use it effectively with database queries, defining different approaches to perform queries, organize results, or manage

81