<?php
abstract class Creator{
/*
startFactory 返回一个具体的产品
factoryMethod 返回对象
*/
protected abstract function factoryMethod(product $product);
public function startFactory($product)
{
return $this->factoryMethod($product);
}
}
//所用的产品统一调用
class countryFactory extends Creator{
private $product;
protected function factoryMethod(product $product)
{
$this->product = $product;
return $this->product->getProperties();
}
} class mysqlFactory extends Creator{
private $product;
protected function factoryMethod(product $product)
{
$this->product = $product;
return $this->product->getProperties();
}
} class sqliteFactory extends Creator{
private $product;
protected function factoryMethod(product $product)
{
$this->product = $product;
return $this->product->getProperties();
}
}
//共同访问接口
interface product
{
public function getProperties();
}
//具体的操作
class mysqlProduct implements product{
private $conn;
public function getProperties()
{
return $this->conn="mysql连接成功".PHP_EOL;;
}
} class sqliteProduct implements product{
private $conn;
public function getProperties()
{
return $this->conn="sqlite连接成功".PHP_EOL;
}
} /**
*
*/
class client
{
public $conn;
public function __construct()
{
$this->conn = new countryFactory();
echo $this->conn->startFactory(new mysqlProduct());
echo $this->conn->startFactory(new sqliteProduct());
}
} $worker = new client();