Reply to topic  [ 3 posts ] 
How to do this..? 
Author Message
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
I want to create an app that uses a series of algorithms (increasing in complexity each time).

The way the app will work is like so...

1. Set the first algorithm as the current algorithm.
2. Check to see if the current algorithm has an effect.
----- a. No effect so set the next algorithm to be the current algorithm and run step 2 again.
----- b. Has an effect so continue.
3. Run the current algorithm and make the changes.
4. Changes made so stop running and wait to be triggered at the beginning again.

I can write the program fairly easliy by coding all the algorithms in to a class and hard coding the order.

However, I was wondering if there is some design pattern that can be used to make expandability easier.

How can I add another algorithm without having to hard code an additional bit into the loop would it be possible to store the function names in an array and use the strings as a reference to the fuction?

Also how can I change the order in which the algorithms run in the app itself (i.e. allow the user to define what order they run in)?

Whilst writing I've been thinking it may be possible to store them as objects that inherit a "go" method. Store the objects in an array and then retrieve them in order to first run a "check" method and then a "go" method.

So...
Code:
Algorithm algorithms[];
EasyAlgorithm easy;
HardAlgorithm hard;

algorithms.push_back(easy);
algorithms.push_back(hard);

for (int i = 0 ; i < algorithms.size ; i++);
{
  if (algorithm[i].check)
  {
    algorithm[i].run;
    break;
  }
}

(N.B. I know the syntax is a bit all over th place language wise but take it as pseudo code).

The classes EasyAlgorithm and HardAlgorithm would both subclass Algorithm and override the "check" and "go" methods.

This way I could rearrange the order of the array quite easily by allowing the user to move them around. (not quite sure how exactly though).
All I need to do is create the algorithm and add a line of code to add it to the end of array.

Is that the best way?

Any other ideas?

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Wed Aug 31, 2011 3:54 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 6:36 pm
Posts: 5150
Location: /dev/tty0
Reply with quote
Would the Factory pattern be what your after? It's too early for me to think much more than that atm...


Thu Sep 01, 2011 7:26 am
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
Well, you set me off exploring and BOOM! http://en.wikipedia.org/wiki/Strategy_pattern

Strategy Pattern works perfectly and is almost exactly what I explained above.

Not sure why it needs to put the Strategy inside the Context though?

Surely you could just have...
Code:
class StrategyExample {
 
    public static void main(String[] args) {
 
        Strategy strategy;
 
        // Three contexts following different strategies
        strategy= new ConcreteStrategyAdd();
        int resultA = strategy.execute(3,4);
 
        strategy = new ConcreteStrategySubtract();
        int resultB = strategy.execute(3,4);
 
        strategy = new ConcreteStrategyMultiply();
        int resultC = strategy.execute(3,4);
    }
}


Would that not work just as well and also remove the need for the Context class altogether?

Ah, nevermind. Just read the "Strategy vs Bridge" bit.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Thu Sep 01, 2011 9:14 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.