12,550,449 members (41,446 online)
Rate this:
See more:
need someone professional help me get the difference beteen

first :

```static class SimpleMathOperations
{
public static double add(double a, double b)
{
return a + b;
}

public static double subtract(double a, double b)
{
return a - b;
}

public static double multiply(double a, double b)
{
return a * b;
}

public static double divide(double a, double b)
{
return a / b;
}
}```

second : Implementing the strategy pattern

```public interface IMathOperation
{
double PerformOperation(double A, double B);
}
{
#region IMathOperation Members

public double PerformOperation(double A, double B)
{
return A + B;
}

#endregion
}

// Subtract -----------------------------------------------
class SubtractOperation: IMathOperation
{
#region IMathOperation Members

public double PerformOperation(double A, double B)
{
return A - B;
}

#endregion
}

// MULTILPY -----------------------------------------------
class MultiplyOperation: IMathOperation
{
#region IMathOperation Members

public double PerformOperation(double A, double B)
{
return A * B;
}

#endregion
}

// DIVIDE -----------------------------------------------
class DivideOperation: IMathOperation
{
#region IMathOperation Members

public double PerformOperation(double A, double B)
{
return A/B;
}

#endregion
}
```

I quoted strategy pattern from this website
Posted 7-Oct-11 4:54am
Mark Nischalke 7-Oct-11 11:05am

And what questions do you have?
EgyptianRobot 7-Oct-11 12:00pm

want to know why I need to Implement Strategy Pattern in this case ?
Is the static class better ? and why ?

Rate this:

## Solution 1

With the strategy pattern it is possible to change or extend the behaviour of a Class.
This sounds a bit abstract, but let's look at an example.

```public interface IMathOperation
{
double PerformOperation(double A, double B);
}

class DoMath
{
private double _A;
private double _B;

public DoMath(double A, double B)
{
_A = A;
_B = B;
}

public double DoMathOperation(IMathOperation operation)
{
return operation.PerformOperation(_A, _B);
}
}
```
The DoMath Class, simple as it may be, makes use of the IMathOperation. Now just assume that DoMath does a lot more than what it does now, but somewhere it needs to do that math operation. By passing an IMathOperation you can tell the DoMath Class that it should perform an Add or Substraction. If you would have used the statis Class this would not have been possible. Your DoMathOperation would have looked something like:
```public double DoMathOperation()
{
}
```
Now all this Class will ever do is Add. If the behaviour needs to change you need to break open and change your Class, which is not what you want.
Another gain you will get from the Strategy Pattern is that when, should the need arise, you can easily create new MathOperations. You now have Add, Subtract, Divide and Multiply, but maybe you'll get a NthRoot operation one day. You could easily create a new Class, Implement IMathOperation and write code for A^B (or however that looks in code...). If you now pass your new Class to the DoMathOperation Function you have changed the behaviour of the DoMath Class without having to change it.

The idea that a Class is closed for modification (you'll never have to change anything in the DoMath Class), but open for extension (you can write new IMathOperations and pass it as parameter to DoMathOperation) is called the Open-Closed Principle or OCP, one of the SOLID principles of Object Oriented Programming. More on SOLID principles here[^] and more on OCP in specific here[^].

Of course you do not need the Strategy Pattern if adding, subtracting, multiplying or dividing is the only thing that makes sense in a certain scenario. If, for example, you need to calculate the total sum of money on an order with 5 items which each cost \$10 you would always use 5 * 10 or:
`double total = SimpleMathOperation.Multiply(orderedAmount, price);`

I hope that was a bit understandable :)
EgyptianRobot 7-Oct-11 15:32pm

Naerling 7-Oct-11 18:53pm

No problem. I started out the same way as you. Someone pointed me to those SOLID articles and they've helped me a lot. Every programmer should know them. I believe knowing SOLID is the only way to understanding design patterns such as the strategy pattern :)
EgyptianRobot 8-Oct-11 4:42am

I'm looking for best free tool to create my project data layers.

Naerling 8-Oct-11 5:13am

Thanks for your confidence in me. However, I am not the best person to answer that.
As far as ORM's go I only know Microsofts own Entity Framework. http://msdn.microsoft.com/en-us/library/bb399572.aspx
The pros to EF are that it is a part of the .NET Framework (needs no additional downloads and installs) and has an easy learning curve in comparison to some other ORM's out there. I have used it for a few months now. But I cannot say if it is better than, for example, nHibernate. You best check out some discussions on the net using Google. If those don't answer your question ask a new, well formulated question here on the forum.
Good luck!
EgyptianRobot 8-Oct-11 4:44am

please note that I use Sql database in my work not oracle

Top Experts
Last 24hrsThis month
 Dave Kreskowiak 50 Suvendu Shekhar Giri 30 Nathan Minier 30 Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ) 28 Afzaal Ahmad Zeeshan 25
 OriginalGriff 3,901 Suvendu Shekhar Giri 1,743 John Simmons / outlaw programmer 1,687 ppolymorphe 1,571 Karthik Bangalore 1,210