Strategy Pattern Code Review
Define the Context Object
Code Review
- Strategy Pattern Code Review: Define the Pattern Interface and Handler Objects
- Strategy Pattern Code Review: Define the Context Object
- Strategy Pattern Code Review: Use the Strategy Pattern
Code Walkthrough
The Context class (object) stores the application data and applies the sort strategy selected against the application data.
class Context
{
private List<String> list = new List<String>();
private IStrategy sortStrategy;
public Context(IStrategy sortStrategy) { this.sortStrategy = sortStrategy; }
public void AddItem(String name) { list.Add(name); } public void SetSortStrategy(IStrategy sortStrategy) { this.sortStrategy = sortStrategy; } public void Sort() { this.sortStrategy.Sort(list);
foreach (String name in list) { Console.WriteLine(name); } } }
public Context(IStrategy sortStrategy) { this.sortStrategy = sortStrategy; }
public void AddItem(String name) { list.Add(name); } public void SetSortStrategy(IStrategy sortStrategy) { this.sortStrategy = sortStrategy; } public void Sort() { this.sortStrategy.Sort(list);
foreach (String name in list) { Console.WriteLine(name); } } }
Printer Friendly Version
Add to Favourites
DotNet Kicks
Digg
del.icio.us
Live Favourites
ma.gnolia
reddit
Slashdot
Technorati
Yahoo!