Skip to Content.
Sympa Menu

gang-of-4-patterns - [gang-of-4-patterns] Altering a Strategy....does this seem right?

gang-of-4-patterns AT lists.cs.illinois.edu

Subject: Design Patterns discussion

List archive

[gang-of-4-patterns] Altering a Strategy....does this seem right?


Chronological Thread 
  • From: "Chris Finlayson" <cfinlayson AT vls-inc.com>
  • To: <gang-of-4-patterns AT cs.uiuc.edu>
  • Subject: [gang-of-4-patterns] Altering a Strategy....does this seem right?
  • Date: Sat, 15 Nov 2003 16:58:15 -0700
  • Importance: Normal
  • List-archive: <http://mail.cs.uiuc.edu/pipermail/gang-of-4-patterns/>
  • List-id: Design Patterns discussion <gang-of-4-patterns.cs.uiuc.edu>

Hi All:

My question for you all is to tell me what you think about a particular
exercise below, and how you'd rate my proposed solution (be honest...it
won't hurt my feelings if you think it's no good!). Any better solutions
are more than welcome.

The background is that I've been learning about design patterns from the GO4
book on my own for a bit, and have been proselytizing it at work, because
I've found them quite helpful. I was using the sorting algorithm as an
example of the Strategy pattern, then someone asked me what I'd do if this
new requirement came along. Questions sure do test your knowledge! I said
I honestly wasn't exactly sure. Below is my idea. I'd like for someone to
know what they think of it (be honest!), and if I'm wrong, where I'm wrong
and what a better alternative may be.


The Original Requirements
====================
OK, let's say that you've followed proper design patterns and have created a
sorting library that sorts integers following the Strategy design pattern.
E.g., you have something like:


1. ISortStrategy - the interface to the concrete strategies
2. QuickSortStrategy - A concrete sort strategy that implements
ISortStrategy
3. MergeSortStrategy - Another concrete sort strategy that implements
ISortStrategy
etc....

Now, in the context of the program that's calling the concrete strategies,
it instantiaties an ISortStrategy (maybe via a factory with the proper
argument as to whether to use a QuickSort, MergeSort, etc.).

(I discussed the same example in a previous email when I was questioning the
difference between Bridge and Strategy).

So far so good.


The New Requirements (what a coworker of mine proposed)
===========================================
Now, let's say you have the requirements that

1. A user can request whether he/she wants to make use of a stable or
non-stable sort.
2. A user can request a specific stable or non-stable sort algorithm.



What I'd do to Satisfy the requirements
=======================================

existing code in the system may look like:

ISortStrategy oSort = SortFactoryStrategy.Create("QuickSort");

I'd alter the sort factory to allow for something like:

ISortStrategy oSort = SortFactoryStrategy.Create("Stable"); // returns a
concrete stable sorting algorithm

ISortStrategy oSort = SortFactoryStrategy.Create("NonStable"); //returns a
concrete non-stable sorting algorithm

ISortStrategy oSort = SortFactoryStrategy.Create("Stable", "MergeSort") //
returns a stable version of the mergesort algorithm, if one is found.


So, more specifically, what I'd propose to satisfy these requirements are:

1. Change the SortFactoryStrategy method by adding two new Create methods.
One that takes in a request of whether or not the user wants a stable or
nonstable algorithm, and a default one is created and returned. Another
that allows the user to request a specific type of stable or nonstable sort.

2. The factory then returns it appropriately. If one is not found, a Null
object is returned and a client has to check for that appropriately.


This allows for the ability to create stable and non-stable versions of
sorting routines (i.e. traditionally quicksort is non-stable, but a stable
version can be created).


What do you think?



Many thanks!

--Chris















Archive powered by MHonArc 2.6.16.

Top of Page