标签:3.1 方法 ext idt rcu break public tor ati
<?php
/**
*工厂模式就是一种类,具有为您创建对象的某些方法,这样就可以使用工厂类创建对象,而不直接使用new。这样如果想更改创建的对象类型,只需更改工厂即可
*/
class Factory
{
public static function create($type)
{
switch ($type) {
case ‘rect‘:
return new Rect();
break;
case ‘triangle‘:
return new Triangle();
break;
case ‘circular‘:
return new Circular();
break;
defalut:
return null;
break;
}
}
}
abstract class Shape
{
abstract public function Area();
}
class Rect extends Shape
{
private $width;
private $height;
public function Area(){
}
public function __toString(){
return "矩形";
}
}
class Triangle extends Shape
{
private $side1;
private $side2;
private $side3;
public function Area(){
}
public function __toString(){
return "三角形";
}
}
class Circular extends Shape
{
private $radius;
const PAI = 3.14;
public function Area(){
}
public function __toString(){
return "圆形";
}
}
$obj = Factory::create(‘rect‘);
if($obj instanceof Shape){
echo $obj;
}else{
exit("没有这个形状");
}
标签:3.1 方法 ext idt rcu break public tor ati
原文地址:http://www.cnblogs.com/kuku2/p/7650090.html