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

设计模式

时间:2017-08-27 17:10:08      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:public   org   www.   限制   case   class   静态   构造   ansi   

<!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>

  

设计模式

标签:public   org   www.   限制   case   class   静态   构造   ansi   

原文地址:http://www.cnblogs.com/sglq/p/7440554.html

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