1. Trang chủ
  2. » Giáo án - Bài giảng

PatternStrategypdf

10 1 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 559,33 KB

Nội dung

Software Development and Test Laboratory ◆ Department of Computer Science and Information Engineering ◆ National Taipei University of Technology. / 20 • Strategy (Compositor)[r]

(1)

Software Development and Test Laboratory ◆Department of Computer Science and Information Engineering ◆National Taipei University of Technology

/ 20

國立台北科技大學 陳偉凱

• Define a family of algorithms

–Encapsulate each one (as an object).

–Make them interchangeable(through polymorphism).

Vary independentlyfrom clients that use it

(2)

Software Development and Test Laboratory ◆Department of Computer Science and Information Engineering ◆National Taipei University of Technology

/ 20Compositionkeeps a list of words and handles

line breaking of the words

3

Composition

Repair()

Simple

Tex

Object-Oriented Programming Strategy Pattern

• A possible design

Composition

Repair()

void Composition::Repair() {

switch (CompositionType) {

case SIMPLE: …

break; case TEX:

… break; case ARRAY:

… break; }

}

Line breaking algorithm Line breaking

(3)

Software Development and Test Laboratory ◆Department of Computer Science and Information Engineering ◆National Taipei University of Technology

/ 20

• A better design

5 Composition SimpleComposition() TexComposition() ArrayComposition() Repair() void Composition::Repair() { switch (CompositionType) { case SIMPLE: SimpleComposition(); break; case TEX: TexComposition(); break; case ARRAY: ArrayComposition(); break; } }

Line breaking algorithm Line breaking algorithm

Object-Oriented Programming Strategy Pattern

Problems

Hard-wiringline breaking algorithms

• Clients (Composition) get biggerand harder to maintain.

• Different algorithms will be appropriate at different times

–The previous design requires all algorithms to exist

simultaneously

• Difficult to add new algorithms and vary existing ones (modify algorithm modify Composition)

(4)

Software Development and Test Laboratory ◆Department of Computer Science and Information Engineering ◆National Taipei University of Technology

/ 20 7

編號Q41:

好的軟體設計者必須考慮軟體如何成長改變 以及何種因素最可能成為改變的焦點

A good (software) designer will consider how software will grow and change and what elements are most likely to be focal points for change

Rebecca Wirfs-Brock and Alan McKean (2003)

Object-Oriented Programming Strategy Pattern

• Solution: apply Strategy pattern Composition

Traverse () Repair ()

compositor

Compose ()

Compositor

Compose ()

SimpleCompositor

Compose ()

TexCompositor

Compose ()

ArrayCompositor

Compositor -> Compose ()

(5)

Software Development and Test Laboratory ◆Department of Computer Science and Information Engineering ◆National Taipei University of Technology

/ 20

• Suppose we would like to evaluate the efficiency of sorting algorithms, a design is:

9 Client (main) +Sort() +InsertionSort() +SelectionSort() +BubbleSort() +QuickSort() + () + () + () + () + () Array void Array::Sort() { switch (sorter) { case INSERTION: InsertionSort(); break; case SELECTION: SelectionSort(); break; . } } The same problems The same problems

Object-Oriented Programming Strategy Pattern

• Solution: apply strategy pattern

(6)

Software Development and Test Laboratory ◆Department of Computer Science and Information Engineering ◆National Taipei University of Technology

/ 20

• Many related classes differonly in their behavior.

• You need different variants of an algorithm. • An algorithm uses datathat clients shouldn't

know about Avoid exposing complex, algorithm-specific data structure to clients. • A class defines many behaviors (use of

multiple conditional (switch) statements).

11 Object-Oriented Programming Strategy Pattern

Context ContextInterface()

Strategy

AlgorithmInterface()

ConcreteStrategyA

AlgorithmInterface()

ConcreteStrategyB

AlgorithmInterface()

ConcreteStrategyC

AlgorithmInterface() Client

(7)

Software Development and Test Laboratory ◆Department of Computer Science and Information Engineering ◆National Taipei University of Technology

/ 20 • Strategy (Compositor)

– Declares an interface common to all supported algorithms.

• ConcreteStrategy (SimpleCompositor )

– Implements the algorithm using the strategy interface.

• Context (Composition)

– Is configured with a ConcreteStrategy object. – Maintains a reference to a Strategy object.

May define an interface that lets Strategy access its data.

13 Object-Oriented Programming Strategy Pattern

• Passing data (discussed later)

–A context may pass all data required by the algorithm

–Alternatively, the context can pass itself as an argument to Strategy operations.

(8)

Software Development and Test Laboratory ◆Department of Computer Science and Information Engineering ◆National Taipei University of Technology

/ 20

• Benefits:

–Families of related algorithms

–An alternative to subclassing

• subclassing context

–Strategies eliminate conditional statements.

• no switch statements

–A choice of implementations

• client can choose different strategies with different time and space trade-offs

15 Object-Oriented Programming Strategy Pattern

• Drawbacks

–Clients must be aware of different Strategies.

• So that a client can select the appropriate strategy

–Communication overhead between Strategy and Context

–Increased number of objects.

(9)

Software Development and Test Laboratory ◆Department of Computer Science and Information Engineering ◆National Taipei University of Technology

/ 20

• Defining the Strategy and Context interfaces.

–Context pass data to Strategy operations.

Decoupled.

• Might pass unneeded data.

–Context passed itself as an argument.

• Strategy requests exactly what it needs. • Must define a more elaborate interface. • Tightly coupled.

• Making Strategy objects optional

–context implements a default Strategy

–client select Strategy only when default is no good

17 Object-Oriented Programming Strategy Pattern

• Flyweight

(10)

Software Development and Test Laboratory ◆Department of Computer Science and Information Engineering ◆National Taipei University of Technology

/ 20

• See “Sorter” sample code

19 Object-Oriented Programming Strategy Pattern

Client Client

Context Context

Ngày đăng: 13/05/2021, 15:39

TÀI LIỆU CÙNG NGƯỜI DÙNG