Code covered by the BSD License  

Highlights from
Strategy Design Pattern in Matlab 2008b

Be the first to rate this file! 12 Downloads (last 30 days) File Size: 3.78 KB File ID: #22193

Strategy Design Pattern in Matlab 2008b

by Paul Williamson

 

20 Nov 2008

Strategy Pattern defined by "Gang of Four" in: "Design Patterns: Elements of ...," Gamma, et al.

| Watch this File

File Information
Description

The Strategy Pattern is just one of many patterns defined by the "Gang of Four" that are commonly used in many other object-oriented programming languages.

Now with Matlab 2008b we can define Interfaces and Abstract classes that can make use of many of these patterns. Not only is this good coding practice, but they make code much more reusable, easier to maintain and change over time. In addition they provide us with a common language to discuss various programming structures.

The GoF define the Strategy Pattern as: Defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

In this simple example we have time series data stored in a TimeSeries class (to keep it simple I have just given it a name property). Then, suppose that we have a number of technical indicators that we wish to use on this time series data, but we’re not told which one until runtime. In addition we need to be able to add new technical indicators easily in the future.

By using this structure we’re passing around the TimeSeries() class. By having an Abstract Strategy Interface we can add as many more technical indicators as we like. All we need to do is add to the switch statement in StrategyType class and implement the RunStrategy method in our new class which inherits from StrategyType.

Note also how easy it would be to completely change behaviour at runtime by setting a new Strategy in the Main.m.

Usage: Extract the separate files and type Main in the command window

MATLAB release MATLAB 7.7 (R2008b)
Other requirements Need a version with OOP capability i.e. > 2008a
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
strategy pattern Paul Williamson 21 Nov 2008 15:04:21
design patterns Paul Williamson 21 Nov 2008 15:04:21
interfaces Paul Williamson 21 Nov 2008 15:04:21
abstract class Paul Williamson 21 Nov 2008 15:04:21
2008b Paul Williamson 21 Nov 2008 15:04:21
gang of four Paul Williamson 21 Nov 2008 15:04:21
gof Paul Williamson 21 Nov 2008 15:04:21
oop Paul Williamson 21 Nov 2008 15:04:21
objectoriented programming Paul Williamson 21 Nov 2008 15:04:21
willip Paul Williamson 21 Nov 2008 15:04:21

Contact us at files@mathworks.com