简体   繁体   English

实现命令模式和多态

[英]Implementing command pattern and polymorphism

I want to implement a command pattern. 我想实现一个命令模式。 I have the following: 我有以下几点:

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

And the program: 和程序:

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

That works as expected. 如预期的那样。 My problem begins, when I would like to extend the State class: 当我想扩展State类时,我的问题开始了:

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

Now the action must apply changes on ExtendedState . 现在,该操作必须在ExtendedState上应用更改。 So I thought I would create extended action that has two additional functions that take ExtendedState as a parameter: 所以我想我将创建具有两个附加函数的扩展操作,这些附加函数将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;
    }
}

This is something I already do not like because the functions that implement the interface become redundant. 这是我不喜欢的,因为实现接口的功能变得多余。 Moreover I need to create a new Execute function in my ExtendedState that utilizes the new type and not IAction (otherwise not implemented functions get called). 此外,我需要在我的ExtendedState中创建一个新的Execute函数,该函数使用新类型而不使用IAction(否则将调用未实现的函数)。

I am sure it can be done in a nice OO way. 我相信可以通过一种很好的面向对象的方式来完成。 Can you help me out? 你能帮我吗? The aim is to create an extensible State class and IAction interface (maybe even generic, I do not know), so I can extend the State but remain the generic functionality without additional coding. 目的是创建一个可扩展的State类和IAction接口(我不知道甚至可以是通用的),因此我可以扩展State但保留通用的功能而无需额外的编码。

You could add a virtual SetNumber method to state 您可以添加一个虚拟的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); 
    }             
} 

In the extended state you orverride it 在扩展状态下,您将其覆盖

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

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

The action would then be implemented like this 然后将像这样执行该动作

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

EDIT : 编辑

What about declaring Number as array? 将Number声明为数组呢?

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

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

   ...
}

The action then does this 该动作然后执行此操作

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

The constructor of ExtendedState would initialize Numbers with 的构造ExtendedState将初始化Numbers

Numbers = new int[2];

In addition, you could have properties for the single numbers 此外,您可以拥有单个数字的属性

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

and

public int Number2 { 
    get { return Numbers[1]; }
    set { Numbers[1] = value; }
}

You could use generics: 您可以使用泛型:

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

How about adding StateContainer to State and Action: 如何将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);
}

Then your original classes can be replaced with: 然后,您的原始类可以替换为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM