Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

14. Design Patterns > The Strategy Pattern

The Strategy Pattern

image

I have a surprise for you. (I hope it’s a good one.) You already know the first of the design patterns we’re going to be exploring.

When we were examining DEPENDENCY INVERSION in the last chapter, we saw that it’s often implemented by a technique called “programming to interfaces”, which looks like this:

image

That’s the basic structure of the STRATEGY PATTERN. There is one little twist, though: The pattern is technically only the STRATEGY when it represents some behavior of the ClientClass (not just an object the ClientClass works with.)

image

On Your Own

image

Looks like our Recipe hierarchy is getting cluttered up with a bunch of classes that only differ in one method. Can you see a way to use Strategy to make things simpler? (Hint: The interface in the Strategy pattern represents a kind of behavior.)


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial