策略模式VS静态类 [英] Strategy Pattern VS Static Class

查看:97
本文介绍了策略模式VS静态类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要专业人士帮助我获得与众不同的成就

首先:

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
} 



我引用了该网站的策略模式
http://www.c-sharpcorner.com/UploadFile/rmcochran/strategyPattern08072006095804AM/strategyPattern.aspx [ ^ ]



I quoted strategy pattern from this website
http://www.c-sharpcorner.com/UploadFile/rmcochran/strategyPattern08072006095804AM/strategyPattern.aspx[^]

推荐答案

使用该策略模式,可以更改或扩展类的行为.
这听起来有点抽象,但让我们看一个例子.

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);
    }
}


DoMath类很简单,它利用了IMathOperation.现在,仅假设DoMath的功能比现在多得多,但是需要在某个地方执行该数学运算.通过传递IMathOperation,您可以告诉DoMath类应该执行加法或减法.如果您将使用Statis Class,则将不可能.您的DoMathOperation看起来应该像这样:


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);
}


现在,该类将要做的就是添加.如果需要更改行为,则需要打开并更改班级,这不是您想要的.
您将从策略模式中获得的另一个好处是,在需要时可以轻松创建新的MathOperations.现在,您可以进行加,减,除和乘运算,但是也许您将有一天获得NthRoot操作.您可以轻松地创建一个新的类,实现IMathOperation并为A ^ B编写代码(或者在代码中查找...).如果现在将新类传递给DoMathOperation函数,则无需更改DoMath类的行为即可.

关闭类以进行修改(您不必在DoMath类中进行任何更改),但可以扩展以进行扩展(您可以编写新的IMathOperations并将其作为参数传递给DoMathOperation)的想法称为开放式封闭原理或OCP,即面向对象编程的SOLID原则之一.有关SOLID原则的更多信息,请此处 [ ^ ]以及有关OCP的更多信息,具体在此处 [ ^ ].

当然,如果在某些情况下唯一可行的方法是加,减,乘或除,那么您就不需要策略模式.例如,如果您需要计算一个包含5个项目的订单的总金额,每个项目的成本为


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,则您将始终使用5 * 10或:
10 you would always use 5 * 10 or:
double total = SimpleMathOperation.Multiply(orderedAmount, price);



我希望这是可以理解的:)



I hope that was a bit understandable :)


这篇关于策略模式VS静态类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆