码迷,mamicode.com
首页 > 其他好文 > 详细

DesignPattern_Behavioral_State

时间:2014-11-17 12:02:08      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   color   sp   div   log   bs   

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

 

DesignPattern_Behavioral_State

标签:des   style   blog   io   color   sp   div   log   bs   

原文地址:http://www.cnblogs.com/rammderek/p/4103138.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!