关于状态模式
自己编了个例子,但感觉似是而非,不是那么自然,有哪里不对或者可以优化的地方?请模式达人给看下。
public interface State{
void wear();
State changState();
}
public class SpringState : State {
public void wear() {
Console.WriteLine("Everyone wear outercoat.");
}
public State changState() {
return new SummerState();
}
}
public class SummerState : State {
public void wear() {
Console.WriteLine("Everyone wear shirts");
}
public State changState() {
return new AutumnState();
}
}
public class AutumnState : State {
public void wear() {
Console.WriteLine("Everyone wear sweater.");
}
public State changState() {
return new WinterState();
}
}
public class WinterState : State {
public void wear() {
Console.WriteLine("Everyone wear jacket.");
}
public State changState() {
return new SpringState();
}
}
public class Person {
private State current;
public Person() {
current = new SpringState();
}
public void wear() {
current.wear();
}
public void senseSeason() {
current =current.changState();
}
}
class client {
static void Main(string[] args) {
Person p = new Person();
p.wear();
p.senseSeason();
p.wear();
p.senseSeason();
p.wear();
p.senseSeason();
p.wear();
}
}
------解决方案--------------------