O'Reilly logo

ActionScript 3.0 Design Patterns by Chandima Cumaranatunge, William Sanders

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Working with String Strategies

The best aspect of the Strategy design pattern is its ability to allow change and adaptation. The design pattern is a virtual poster child for composition, where change and flexibility are handled with aplomb. Also, it's a great tool for working with algorithms large and small.

With any application, you're likely to encounter recurring algorithms. For this application, we decided to include some little algorithms that we use with different applications that have to be rebuilt every time we create an application that uses them. However, we don't want to make them into classes where the main objective is to inherit the functionality. Rather, we want to use them in composition so that we have more flexibility, and use their functionality only where we need them.

For our example, we've taken two simple types of string and sorting issues that seem to come up in a lot of different applications. First, we built some simple strategies for checking strings. The first strategy is designed to see if the @ symbol is placed in an e-mail address, and if it's not in the first or last position in a string. The second strategy is a password-checking algorithm that knocks all entries to lowercase, and then sees if they match the correct password.

Our second strategy interface is for demonstrating different sorting possibilities using the Array class and sort() method. By changing the sort parameters, you will be able to see which constants to use with different types of ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required