1

我想实现一个命令模式。我有以下内容:

public class State
{
    public int Number { get; set; }

    public void Execute(IAction action)
    {
        if (action.IsValid(this))
            action.Apply(this);
    }            
}

public interface IAction
{
    bool IsValid(State state);
    void Apply(State state);
}       

public class ActionSet5IfZero : IAction
{

    public bool IsValid(State state)
    {
        if (state.Number == 0)
            return true;
        else
            return false;
    }

    public void Apply(State state)
    {
        state.Number = 5;
    }
}

和程序:

static void Main(string[] args)
{
    State s = new State();
    s.Execute(new ActionSet5IfZero());
}

这按预期工作。当我想扩展 State 类时,我的问题就开始了:

public class ExtendedState : State
{
    public int Number2 { get; set; }
}

现在该操作必须将更改应用于ExtendedState. 所以我想我会创建一个扩展动作,它有两个额外的函数ExtendedState作为参数:

public class ExtendedActionSet5IfZero : IAction
{

    public bool IsValid(State state)
    {
        throw new NotImplementedException();
    }

    public void Apply(State state)
    {
        throw new NotImplementedException();
    }

    public bool IsValid(ExtendedState state)
    {
        if (state.Number == 0 && state.Number2 == 0)
            return true;
        else
            return false;
    }

    public void Apply(ExtendedState state)
    {
        state.Number = 5;
        state.Number2 = 5;
    }
}

这是我已经不喜欢的东西,因为实现接口的函数变得多余。此外,我需要Execute在我的中创建一个ExtendedState使用新类型而不是 IAction 的新函数(否则将调用未实现的函数)。

我相信它可以以一种很好的 OO 方式完成。你能帮我吗?目的是创建一个可扩展的 State 类和 IAction 接口(甚至可能是通用的,我不知道),所以我可以扩展State但保持通用功能而无需额外编码。

4

3 回答 3

1

您可以添加一个虚拟SetNumber方法来声明

public class State 
{ 
    public int Number { get; set; } 

    public virtual void SetNumber(int n)
    { 
        Number = n;
    }

    public void Execute(IAction action) 
    { 
        if (action.IsValid(this)) 
            action.Apply(this); 
    }             
} 

在扩展状态下,您可以覆盖它

public class ExtendedState : State  {
    public int Number2 { get; set; }

    public orverride void SetNumber(int n)
    { 
        base.SetNumber(n);
        Number2 = n;
    }
}  

然后将像这样执行该操作

public void Apply(State state)        
{
    state.SetNumber(5);        
}    

编辑

将 Number 声明为数组怎么样?

public class State  
{
    public int[] Numbers { get; private set; }

    public State()
    {
        Numbers = new int[1];
    }

   ...
}

然后该操作执行此操作

public void Apply(State state)         
{
    for (int i = 0; i < state.Numbers.Length; i++) {
        state.Numbers[i] = 5;
    }
}   

的构造函数ExtendedState将初始化Numbers

Numbers = new int[2];

此外,您可以拥有单个数字的属性

public int Number { 
    get { return Numbers[0]; }
    set { Numbers[0] = value; }
}

public int Number2 { 
    get { return Numbers[1]; }
    set { Numbers[1] = value; }
}
于 2012-02-09T16:19:24.077 回答
0

您可以使用泛型:

interface IAction<TState> where TState: State
{
    bool IsValid(TState state);
    void Apply(TState state);
}
于 2012-02-09T16:09:50.127 回答
0

将 StateContainer 添加到 State 和 Action 怎么样:

public interface IStateContainer<TState, TAction> where TState : IState where TAction : IAction<TState> { 
    public TState State;
    public void Execute(TAction action);
}

public interface IState { }

public interface IAction<TState> where TState : IState {
    bool IsValid(TState state);
    void Apply(TState state);
}

然后您的原始类可以替换为:

public class ValidatingStateContainer<TState, TAction> : IStateContainer<TState, TAction> {

    public ValidatingStateContainer(TState state) {
        State = state;
    }

    public TState State { get; private set; }

    public void Execute(TAction action)
    {
        if (action.IsValid(this))
            action.Apply(State);
    }
}

public class ActionSet5IfZero : IAction<NumberState>
{
    public boolean IsValid(NumberState state)
    {
        if (state.Number == 0)
            return true;
        else
            return false;
    }

    public void Apply(NumberState state)
    {
        state.Number = 5;
    }
}

public class ExtendedActionSet5IfZero : ActionSet5IfZero, IAction<TwoNumberState>
{   
    public boolean IsValid(TwoNumberState state)
    {
        if (base.IsValid(state) && state.Number2 == 0)
            return true;
        else
            return false;
    }

    public void Apply(TwoNumberState state)
    {
        base.Apply(state);
        state.Number2 = 5;
    }
}

public class NumberState : IState {
    public int Number { get; set; }
}

public class TwoNumberState : NumberState {
    public int Number2 { get; set; }
}
于 2012-02-09T16:16:07.317 回答