<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<?php
//设计模式
//单例模式(一个类只能做一个单体模式)
//1.将构造函数变成私有的
//2.提供一个静态方法来返回对象
//3.在方法里面加限制条件
/*class Ren{
	private function __construct(){
	}
	public static function duixiang(){
		
		return new Ren();
	}
}

$r = Ren::duixiang();
var_dump($r);*/
/*class Ren{
	public $name;
	public static $dx;
	private function __construct(){
	}
	public static function duixiang(){
		if(empty(Ren::$dx)){
			Ren::$dx = new Ren;
		}
		return Ren::$dx;
	}
}

$r = Ren::duixiang();
$r->name = "张三";
$r1 = Ren::duixiang();
var_dump($r);*/

//工厂模式
/*class YunSuan{
	public $a;
	public $b;
	public function jia(){
		return $this->a+$this->b; 
	}
	public function jian(){
		return $this->a-$this->b; 
	}
	public function cheng(){
		return $this->a*$this->b; 
	}
	public function chu(){
		return $this->a/$this->b; 
	}
}*///对类的修改是关闭的,对类的扩展是开放的
/*class YunSuan{
	public $a;
	public $b;
	public function suan(){
	}
}//作为父类
class jia extends YunSuan{
	public $a;
	public $b;
	public function suan(){
		return $this->a+$this->b; 
	}
}
class jian extends YunSuan{
	public function suan(){
		return $this->a-$this->b; 
	}
}
class Yu extends YunSuan{
	public function suan(){
		return $this->a%$this->b;
	}
}

//工厂类
class GongChang{
	public static function duixiang($ysf){
		switch($ysf){
			case "+";
				return new jia();
				break;
			case "-";
				return new jian();
				break;
			case "%";
				return new yu();
				break;
		}
	}
}
$y = GongChang::duixiang("+");
$y->a = 10;
$y->b = 5;
echo $y->suan();*/

?>
</body>
</html>

  

posted on 2017-08-27 15:48  唯懿  阅读(121)  评论(0编辑  收藏  举报