标签:
状态模式:允许一个对象在其内部状态改变时改变它的行为。对象看起来似乎修改了它的类。

<?php
class State{
function WriteProgram(){
}
}
class Work{
public $hour,$current;
function __construct(){
$this -> hour = 9;
$this -> current = new ForenoonState();
}
function SetState($temp){
$this -> current = $temp;
}
function WriteProgram(){
$this -> current -> WriteProgram($this);
}
}
class NoonState extends State{
function WriteProgram($w){
print "noon working\n";
if($w -> hour < 13){
print "fun.\n";
}else{
print "need to rest\n";
}
}
}
class ForenoonState extends State{
function WriteProgram($w){
if($w -> hour < 12){
print "morning working\n";
}else{
$w -> SetState(new NoonState());
$w -> WriteProgram();
}
}
}
$mywork = new Work();
$mywork -> hour = 9;
$mywork -> WriteProgram();
$mywork -> hour = 14;
$mywork -> WriteProgram();
?>
标签:
原文地址:http://www.cnblogs.com/zhutianpeng/p/4232102.html