设计模式简单说应对某类问题而设计的解决方式
工厂模式:应对需求创建相应的对象
classfactory{function__construct($name){if(file_exists(‘./’.$name.’.class.php’)){
returnnew$name;
}else{
die(‘not exist’);
}
}
}
单例模式:只创建一个对象的实例,不允许再创建实例,节约资源(例如数据库的连接)
classinstance{public$val = 10;
privatestatic$instance ;
privatefunction__construct(){}
privatefunction__clone(){}
//设置为静态方法才可被类调用publicstaticfunctiongetinstance(){/*if(!isset(self::$instance)){
self::$instance = new self;
}*/if(!isset(instance::$instance)){
instance::$instance = newself;
}
return instance::$instance;
}
}
$obj_one = instance::getinstance();
$obj_one->val = 20;
//clone可以调用__clone()克隆即new出一个新的的对象//$obj_two = clone $obj_one;$obj_two = instance::getinstance();
echo$obj_two->val;
echo’
‘;
var_dump($obj_one,$obj_two);
‘).addclass(‘pre-numbering’).hide();
$(this).addclass(‘has-numbering’).parent().append($numbering);
for (i = 1; i