DesignPattern_Behavioral_State

void Main()
{
    Subject subject = new Subject{ Level=10 };
    State sa = new StateA();
    State sb = new StateB();
    State sc = new StateC();
    sa.Set(sb);
    sb.Set(sc);
    sa.Show(subject);
}
class Subject{
    public int Level { get; set; }
}
abstract class State{
    protected State superior;
    public void Set(State s){ superior = s;}
    public abstract void Show(Subject subject);
}
class StateA:State{
    public override void Show(Subject subject){
        if (subject.Level<=10){}
        else if(superior!=null){
            superior.Show(subject);
        }
    }
}
class StateB:State{
    public override void Show(Subject subject){
        if (subject.Level<=100){}
        else if(superior!=null){
            superior.Show(subject);
        }
    }
}
class StateC:State{
    public override void Show(Subject subject){
        if (subject.Level<=1000){}
        else if(superior!=null){
            superior.Show(subject);
        }
    }
}
时间: 2024-11-10 15:42:15

DesignPattern_Behavioral_State的相关文章