Class c implements some generally useful

This preview shows page 13 - 20 out of 24 pages.

Class C implements some generally useful functionality C does most of the work, but some aspects of its behavior can be customized by clients Clients customize C’s behavior by passing in “algorithms” that are invoked by C at appropriate times The “algorithms” passed in by clients are called “strategies” C invokes the client-provided strategies as needed to perform its work A “strategy” is an object that implements an algorithm This is an alternative to using inheritance to customize a class’s behavior
Image of page 13

Subscribe to view the full document.

14 Strategy pattern : customize a class’s functionality with plug-in algorithms +ContextOperation() Context +AlgorithmInterface() «interface» Strategy +AlgorithmInterface() Concrete Strategy A +AlgorithmInterface() Concrete Strategy B +AlgorithmInterface() Concrete Strategy C -strategy ContextOperation() { .... strategy->AlgorithmInterface(); .... strategy->AlgorithmInterface(); .... }
Image of page 14
15 Strategy pattern : customize a class’s functionality with plug-in algorithms The following examples of reusable code use the Strategy pattern
Image of page 15

Subscribe to view the full document.

16 Examples of reusable code Sort(Person[]); How could we make this sorting algorithm more reusable?
Image of page 16
17 Examples of reusable code Sort(Person[]); How could we make this sorting algorithm more reusable? Sort(Comparable[]) interface Comparable { int compareTo(Object o); }
Image of page 17

Subscribe to view the full document.

18 Examples of reusable code Sort(Person[]); How could we make this sorting algorithm more reusable? Sort(Comparable[]) interface Comparable { int compareTo(Object o); } Sort(Object[], Comparator) interface Comparator { int compare(Object o1, Object o2); }
Image of page 18
19 Examples of reusable code Sort(Person[]); How could we make this sorting algorithm more reusable? Sort(Comparable[]) interface Comparable { int compareTo(Object o); } Sort(Object[], Comparator) interface Comparator { int compare(Object o1, Object o2); } Anybody remember qsort from 240?
Image of page 19

Subscribe to view the full document.

Image of page 20
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern