Click here to Skip to main content
11,433,170 members (62,443 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.5 Patterns pattern
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);
}
// ADD -----------------------------------------------
class AddOperation: IMathOperation
{
    #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
http://www.c-sharpcorner.com/UploadFile/rmcochran/strategyPattern08072006095804AM/strategyPattern.aspx[^]
Posted 7-Oct-11 5:54am
Comments
Mark Nischalke at 7-Oct-11 11:05am
   
And what questions do you have?
EgyptianRobot at 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 ?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

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()
{
    return SimpleMathOperations.Add(_A, _B);
}
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 Smile | :)
  Permalink  
Comments
EgyptianRobot at 7-Oct-11 15:32pm
   
thanks Million for your wonderful explanation and the links , it's really so helpful.
Naerling at 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 at 8-Oct-11 4:42am
   
your nice way in replying encouraged me to ask you another question and wish to get helpful reply

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

thanks million in advance
Naerling at 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 at 8-Oct-11 4:44am
   
please note that I use Sql database in my work not oracle

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.150428.2 | Last Updated 7 Oct 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100