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[^]
Loading
VulpesPosted Oct 8, 2011, 6:48 AM
Mohammed MostafaPosted Oct 8, 2011, 4:02 AM
VulpesPosted Oct 7, 2011, 11:30 AM
To call one of these you simply do:
double a = 3.5;
double b = 2.5;
double c = SimpleMathOperations.add(a, b);
Console.WriteLine(c); // 6
In the second case you have an interface which has a single method, PerformOperation, which performs a binary operation on doubles.
To cater for all 4 basic operations, you need to define four classes which implement this interface in the appropriate ways.
For example:
AddOperation addop = new AddOperation();
double a = 3.5;
double b = 2.5;
double c = addop.PerformOperation(a, b);
Console.WriteLine(c); // 6