Tải bản đầy đủ (.pdf) (10 trang)

PatternStrategypdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (559.33 KB, 10 trang )

<span class='text_page_counter'>(1)</span><div class='page_container' data-page=1>

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


<b>/ 20</b>


<b>國立台北科技大學 陳偉凱</b>



Define a family of algorithms.



Encapsulate each one (as an

<b>object</b>

).



Make them

<b>interchangeable</b>

(through


polymorphism).



<b>Vary independently</b>

from clients that use it



</div>
<span class='text_page_counter'>(2)</span><div class='page_container' data-page=2>

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


<b>/ 20</b>

<b>Composition</b>

keeps a list of words and handles



<b>line breaking </b>

of the words



<b>3</b>


<i><b>Composition</b></i>



<b>Repair()</b>



<b>Simple</b>



<b>Tex</b>




<b>…</b>


<b>Object-Oriented Programming Strategy Pattern</b>


A possible design



<i><b>Composition</b></i>



<b>Repair()</b>



void Composition::Repair()


{



switch (CompositionType)


{



case SIMPLE:




break;


case TEX:




break;


case ARRAY:




break;


}




}



<b>Line breaking </b>


<b>algorithm</b>


<b>Line breaking </b>



</div>
<span class='text_page_counter'>(3)</span><div class='page_container' data-page=3>

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


<b>/ 20</b>


A better design



<b>5</b>

<i><b>Composition</b></i>


<b>SimpleComposition()</b>


<b>TexComposition()</b>


<b>ArrayComposition()</b>


<b>Repair()</b>


void Composition::Repair()


{


switch (CompositionType)


{


case SIMPLE:


SimpleComposition();


break;


case TEX:


TexComposition();


break;


case ARRAY:


ArrayComposition();



break;


}


}



<b>Line breaking algorithm</b>


<b>Line breaking algorithm</b>



<b>Object-Oriented Programming Strategy Pattern</b>


<b>Problems</b>



<b>Hard-wiring</b>

line breaking algorithms



Clients (Composition) get

<b>bigger</b>

and harder to


maintain.



Different algorithms will be appropriate at different


times



The previous design requires

<b>all algorithms </b>

to exist



<b>simultaneously </b>



Difficult to add new algorithms and vary existing ones


(modify algorithm

modify Composition).



</div>
<span class='text_page_counter'>(4)</span><div class='page_container' data-page=4>

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


<b>/ 20</b>
<b>7</b>



<b>編號</b>

<b>Q41:</b>



<b>好的軟體設計者必須考慮軟體如何</b>

<b>成長</b>

<b>與</b>

<b>改變</b>

<b>,</b>


<b>以及何種因素最可能成為改變的</b>

<b>焦點</b>

<b>。</b>



<b>A good (software) designer will consider how software will </b>


<b>grow and change and what elements are most likely to be </b>


<b>focal points for change. </b>



<b>Rebecca Wirfs-Brock and Alan McKean (2003)</b>



<b>Object-Oriented Programming Strategy Pattern</b>


Solution: apply Strategy pattern


<b>Composition</b>



Traverse ()


Repair ()



compositor



<i>Compose ()</i>



<i><b>Compositor</b></i>



Compose ()



<b>SimpleCompositor</b>




Compose ()



<b>TexCompositor</b>



Compose ()



<b>ArrayCompositor</b>



<b>Compositor -> Compose ()</b>



</div>
<span class='text_page_counter'>(5)</span><div class='page_container' data-page=5>

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


<b>/ 20</b>


Suppose we would like to evaluate the


efficiency of sorting algorithms, a design is:



<b>9</b>

<b>Client (main)</b>


+Sort()


+InsertionSort()


+SelectionSort()


+BubbleSort()


+QuickSort()


+...()


+...()


+...()


+...()


+...()


<b>Array</b>



void Array::Sort()


{


switch (sorter)


{


case INSERTION:


InsertionSort();


break;


case SELECTION:


SelectionSort();


break;


...


}


}


<b>The same </b>


<b>problems</b>


<b>The same </b>


<b>problems</b>



<b>Object-Oriented Programming Strategy Pattern</b>


Solution: apply strategy pattern



</div>
<span class='text_page_counter'>(6)</span><div class='page_container' data-page=6>

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


<b>/ 20</b>


Many related classes

<b>differ</b>

only in their


<b>behavior</b>

.



You need different

<b>variants</b>

of an algorithm.



An algorithm uses

<b>data</b>

that clients

<b>shouldn't </b>



<b>know</b>

about. Avoid exposing complex,


algorithm-specific data structure to clients.


A class defines many behaviors (use of



<b>multiple conditional </b>

(switch)

<b>statements</b>

).



<b>11</b>
<b>Object-Oriented Programming Strategy Pattern</b>


<b>Context</b>


<b>ContextInterface()</b>



<i><b>Strategy</b></i>



<i><b>AlgorithmInterface()</b></i>



<b>ConcreteStrategyA</b>



<b>AlgorithmInterface()</b>



<b>ConcreteStrategyB</b>



<b>AlgorithmInterface()</b>



<b>ConcreteStrategyC</b>



<b>AlgorithmInterface()</b>


Client




</div>
<span class='text_page_counter'>(7)</span><div class='page_container' data-page=7>

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


<b>/ 20</b>

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.



<b>May define an interface that lets Strategy access its </b>


<b>data.</b>



<b>13</b>
<b>Object-Oriented Programming Strategy Pattern</b>


Passing data (discussed later)



A context may

<b>pass all </b>

data required by the


algorithm.



Alternatively, the context can

<b>pass itself </b>

as an



argument to Strategy operations.



</div>
<span class='text_page_counter'>(8)</span><div class='page_container' data-page=8>

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


<b>/ 20</b>


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



<b>15</b>
<b>Object-Oriented Programming Strategy Pattern</b>


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.



</div>
<span class='text_page_counter'>(9)</span><div class='page_container' data-page=9>

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


<b>/ 20</b>


Defining the Strategy and Context interfaces.



Context pass data to Strategy operations.



<b>Decoupled.</b>



Might pass unneeded data.



Context passed itself as an argument.



Strategy requests exactly what it needs.


Must define a more elaborate interface.


<b>Tightly coupled.</b>



Making Strategy objects optional



context implements a default Strategy



client select Strategy only when default is no good




<b>17</b>
<b>Object-Oriented Programming Strategy Pattern</b>


Flyweight



</div>
<span class='text_page_counter'>(10)</span><div class='page_container' data-page=10>

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


<b>/ 20</b>


See “Sorter” sample code



<b>19</b>
<b>Object-Oriented Programming Strategy Pattern</b>


<b>Client</b>


<b>Client</b>



<b>Context</b>


<b>Context</b>



</div>

<!--links-->

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×