PHP设计模式(创建型)-php教程

资源魔 24 0
媒介

跟着编程名目经历的添加,从效劳于营业逻辑到针对名目的全局设计。意识到设计模式正在开发进程中 \的首要性,遵照 S.O.L.I.D 五年夜基准准则。它拓展了我的视野,让代码愈加灵敏,看起来愈加富裕美感.\美是构建万物的哲学思维.

咱们学习的设计模式分为三类:创立者模式、构造型模式、行为型模式;创立型模式与工具的创立无关;构造型模式解决类或工具的组合;而行为型模式是对类或工具怎么交互以及怎么调配职责进行形容;

内容:本文引见的是 PHP 设计模式的创立型一篇。包罗:单例模式(Singleton), 多例模式(Multiton), 工场办法模式(Factory Method), 形象工场模式(Abstract Factory), 简略工场模式(Simple Factory), 原型模式(Prototype), 工具池模式(Pool), 建造者模式(Builder)

保举:《PHP教程》

(一)单例模式(Singleton)

● 界说

保障一个类只有一个实例,而且提供一个拜访它的全局拜访点。零碎内存中该类只存正在一个工具,节流了零碎资本,关于一些需求频仍创立销毁的工具,应用单例模式能够进步零碎功能。

● 代码示例

class Singleton
{
    /**
    * @var Singleton
    */
    private static $instance;
    /**
    * 没有容许从内部挪用以避免创立多个实例
    * 要应用单例,必需经过 Singleton::getInstance() 办法猎取实例
    */
    private function __construct()
    {
    }
    /**
    * 经过懒加载取得实例(正在第一次应用的时分创立)
    */
    public static function getInstance(): Singleton
    {
        if (null === static::$instance) {
            static::$instance = new static();
        }
        return static::$instance;
    }
    /**
    * 避免实例被克隆(这会创立实例的正本)
    */
    private function __clone()
    {
    }
    /**
    * 避免反序列化(这将创立它的正本)
    */
    private function __wakeup()
    {
    }
}

(二)多例模式(Multiton)

● 界说

正在多例模式中,多例类能够有多个实例,并且多例类必需本人创立、治理本人的实例,并向外界提供本人的实例。1. 经过实例容器保留容器。2. 行使公有结构阻止内部结构。3. 提供getInstantce()办法猎取实例.

● 代码示例 两个工具经过一个类进行屡次实例化

abstract class Multiton { 
    private static $instances = array(); 
    public static function getInstance() { 
        $key = get_called_class() . serialize(func_get_args());
        if (!isset(self::$instances[$key])) { 
            $rc = new ReflectionClass(get_called_class());
            self::$instances[$key] = $rc->newInstanceArgs(func_get_args());
        }
        return self::$instances[$key]; 
    }
    /**
     * 该公有工具阻止实例被克隆
     */
    private function __clone()
    {
    }
    /**
     * 该公有办法阻止实例被序列化
     */
    private function __wakeup()
    {
    }
} 
class Hello extends Multiton { 
    public function __construct($string = 'World') { 
        echo "Hello $string\n"; 
    } 
} 
class GoodBye extends Multiton { 
    public function __construct($string = 'my', $string2 = 'darling') { 
        echo "Goodbye $string $string2\n"; 
    }
}
$a = Hello::getInstance('World'); 
$b = Hello::getInstance('bob'); 
// $a !== $b 
$c = Hello::getInstance('World'); 
// $a === $c 
$d = GoodBye::getInstance(); 
$e = GoodBye::getInstance();
// $d === $e 
$f = GoodBye::getInstance('your'); 
// $d !== $f

(三)工场办法模式(Factory Method)

● 界说

将类的实例化(详细产物的创立)提早到工场类的子类(详细工场)中实现,即由子类来决议应该实例化(创立)哪个类

● 代码示例 : 小成有一间塑料加工场(仅消费 A 类产物);跟着客户需要的变动,客户需求消费 B 类产物。扭转原有塑料加工场的设置装备摆设以及变动十分艰难,假定下一次客户需求再发作变动,再次扭转将增年夜十分年夜的老本;小成决议购置塑料分厂 B 来消费 B 类产物。

abstract class Product{
    public abstract function Show();
}
//详细产物A类
class  ProductA extends  Product{
    public function Show() {
        echo "消费出了产物A";
    }
}
//详细产物B类
class  ProductB extends  Product{
    public function Show() {
        echo "消费出了产物B";
    }
}
abstract class Factory{
    public abstract function Manufacture();
}
//工场A类 - 消费A类产物
class  FactoryA extends Factory{
    public function Manufacture() {
        return new ProductA();
    }
}
//工场B类 - 消费B类产物
class  FactoryB extends Factory{
    public function Manufacture() {
        return new ProductB();
    }
}

(四)形象工场模式(Abstract Factory)

● 界说

正在没有指定详细类的状况下创立一系列相干或依赖工具。 通常创立的类都完成相反的接口。 形象工场的客户其实不关怀这些工具是若何创立的,它只是晓得它们是若何一同运转的。

● 代码示例 : 有两个工场,A 工场担任运输,B 工场消费数码产物.

interface Product
{
    public function calculatePrice(): int;
}
class ShippableProduct implements Product
{
    /**
     * @var float
     */
    private $productPrice;
    /**
     * @var float
     */
    private $shippingCosts;
    public function __construct(int $productPrice, int $shippingCosts)
    {
        $this->productPrice = $productPrice;
        $this->shippingCosts = $shippingCosts;
    }
    public function calculatePrice(): int
    {
        return $this->productPrice + $this->shippingCosts;
    }
}
class DigitalProduct implements Product
{
    /**
     * @var int
     */
    private $price;
    public function __construct(int $price)
    {
        $this->price = $price;
    }
    public function calculatePrice(): int
    {
        return $this->price;
    }
}
class ProductFactory
{
    const SHIPPING_COSTS = 50;
    public function createShippableProduct(int $price): Product
    {
        return new ShippableProduct($price, self::SHIPPING_COSTS);
    }
    public function createDigitalProduct(int $price): Product
    {
        return new DigitalProduct($price);
    }
}

(五)简略工场模式(Simple Factory)

● 界说

简略工场模式是一个精简版的工场模式。工场脚色-详细产物-形象产物

● 代码示例 :

一个农场,要向市场发卖生果。农场里有三种生果 苹果、葡萄,咱们想象:一、生果有多种属性,每一个属性都有没有同,然而,他们有独特之处 | 成长、莳植、收货、吃。未来有可能会添加新的生果、咱们需求界说一个接口来标准他们必需完成的办法.

interface fruit{
    /**
     * 成长
     */
    public function grow();
    /**
     * 莳植
     */
    public function plant();
    /**
     * 播种
     */
    public function harvest();
    /**
     * 吃
     */
    public function eat();
}
class apple implements fruit{
    //苹果树有春秋
    private $treeAge;
    //苹果有颜色
    private $color;
    public function grow(){
        echo "grape grow";
    }
    public function plant(){
        echo "grape plant";
    }
    public function harvest(){
        echo "grape harvest";
    }
    public function eat(){
        echo "grape eat";
    }
    //取苹果树的春秋
    public function getTreeAge(){
        return $this->treeAge;
    }
    //设置苹果树的春秋
    public function setTreeAge($age){
        $this->treeAge = $age;
        return true;
    }
}
class grape implements fruit{
    //葡萄能否有籽
    private $seedLess;
    public function grow(){
        echo "apple grow";
    }
    public function plant(){
        echo "apple plant";
    }
    public function harvest(){
        echo "apple harvest";
    }
    public function eat(){
        echo "apple eat";
    }
    //有没有籽取值
    public function getSeedLess(){
        return $this->seedLess;
    }
    //设置有籽无籽
    public function setSeedLess($seed){
        $this->seedLess = $seed;
        return true;
    }
}
class farmer
{
    //界说个动态工场办法
    public static function factory($fruitName){
        switch ($fruitName) {
            case 'apple':
                return new apple();
                break;
            case 'grape':
                return new grape();
                break;
            default:
                throw new badFruitException("Error no the fruit", 1);
                break;
        }
    }
}
class badFruitException extends Exception
{
    public $msg;
    public $errType;
    public function __construct($msg = '' , $errType = 1){
        $this->msg = $msg;
        $this->errType = $errType;
    }  
}
/**
 * 猎取生果实例化的办法
 */
try{
    $appleInstance = farmer::factory('apple');
    var_dump($appleInstance);
}catch(badFruitException $err){
    echo $err->msg . "_______" . $err->errType;
}

(六)原型模式(Prototype)

● 界说

相比失常创立一个工具 (new Foo () ),起首创立一个原型,而后克隆它会更节流开支。

● 代码示例 : 为每一一本书设置题目

abstract class BookPrototype
{
    /**
    * @var string
    */
    protected $title = 0;
    /**
    * @var string
    */
    protected $category;
    abstract public function __clone();
    public function getTitle(): string
    {
        return $this->title;
    }
    public function setTitle($title)
    {
       $this->title = $title;
    }
}
class BarBookPrototype extends BookPrototype
{
    /**
    * @var string
    */
    protected $category = 'Bar';
    public function __clone()
    {
    }
}
class FooBookPrototype extends BookPrototype
{
    /**
    * @var string
    */
    protected $category = 'Foo';
    public function __clone()
    {
    }
}
$fooPrototype = new FooBookPrototype();
$barPrototype = new BarBookPrototype();
for ($i = 5; $i < 10; $i++) {
    $book = clone $fooPrototype;
    $book->setTitle('Foo Book No ' . $i);
    var_dump(new FooBookPrototype == $book);
}
for ($i = 0; $i < 5; $i++) {
    $book = clone $barPrototype;
    $book->setTitle('Bar Book No ' . $i);
    var_dump(new BarBookPrototype == $book);
}

(七)工具池模式(Pool)

● 界说

工具池能够用于结构而且寄存一系列的工具并正在需求时猎取挪用。正在初始化实例老本高,实例化率高,可用实例有余的状况下,工具池能够极年夜地晋升功能。正在创立工具(尤为是经过网络)工夫花消没有确定的状况下,经过工具池正在短时间工夫内就能够取得所需的工具。

● 代码示例

class Factory {
    protected static $products = array();
    public static function pushProduct(Product $product) {
        self::$products[$product->getId()] = $product;
    }
    public static function getProduct($id) {
        return isset(self::$products[$id]) ? self::$products[$id] : null;
    }
    public static function removeProduct($id) {
        if (array_key_exists($id, self::$products)) {
            unset(self::$products[$id]);
        }
    }
}
Factory::pushProduct(new Product('first'));
Factory::pushProduct(new Product('second'));
print_r(Factory::getProduct('first')->getId());
// first
print_r(Factory::getProduct('second')->getId());
// second

(八)建造者模式(Builder)

● 界说

将一个复杂工具的构建与它的示意别离,使患上一样的构建进程能够创立没有同的示意

● 2)代码示例 建造相反规范的卡车以及汽车。相似于变形金刚,相反的整机进行没有同的组合.

● 分为 Director 导演者,担任构建、BuilderInterface 构建接口,标准建造规范、TruckBuilder 构建卡车类 CarBuilder 构建汽车类

Vehicle 零部件公共类、Truck Car Engine Wheel Door 零部件类、DirectorTest 测试类

class Director
{
    public function build(BuilderInterface $builder): Vehicle
    {
        $builder->createVehicle();
        $builder->addDoors();
        $builder->addEngine();
        $builder->addWheel();
        return $builder->getVehicle();
    }
}
interface BuilderInterface
{
    public function createVehicle();
    public function addWheel();
    public function addEngine();
    public function addDoors();
    public function getVehicle(): Vehicle;
}
class TruckBuilder implements BuilderInterface
{
    /**
    * @var Truck
    */
    private $truck;
    public function addDoors()
    {
        $this->truck->setPart('rightDoor', new Door());
        $this->truck->setPart('leftDoor', new Door());
    }
    public function addEngine()
    {
        $this->truck->setPart('truckEngine', new Engine());
    }
    public function addWheel()
    {
        $this->truck->setPart('wheel1', new Wheel());
        $this->truck->setPart('wheel2', new Wheel());
        $this->truck->setPart('wheel3', new Wheel());
        $this->truck->setPart('wheel4', new Wheel());
        $this->truck->setPart('wheel5', new Wheel());
        $this->truck->setPart('wheel6', new Wheel());
    }
    public function createVehicle()
    {
        $this->truck = new Truck();
    }
    public function getVehicle(): Vehicle
    {
        return $this->truck;
    }
}
class CarBuilder implements BuilderInterface
{
    /**
    * @var Car
    */
    private $car;
    public function addDoors()
    {
        $this->car->setPart('rightDoor', new Door());
        $this->car->setPart('leftDoor', new Door());
        $this->car->setPart('trunkLid', new Door());
    }
    public function addEngine()
    {
        $this->car->setPart('engine', new Engine());
    }
    public function addWheel()
    {
        $this->car->setPart('wheelLF', new Wheel());
        $this->car->setPart('wheelRF', new Wheel());
        $this->car->setPart('wheelLR', new Wheel());
        $this->car->setPart('wheelRR', new Wheel());
    }
    public function createVehicle()
    {
        $this->car = new Car();
    }
    public function getVehicle(): Vehicle
    {
        return $this->car;
    }
}
abstract class Vehicle
{
    /**
    * @var object[]
    */
    private $data = [];
    /**
    * @param string $key
    * @param object $value
    */
    public function setPart($key, $value)
    {
        $this->data[$key] = $value;
    }
}
class Truck extends Vehicle
{
}
class Car extends Vehicle
{
}
class Engine extends Vehicle
{
}
class Wheel extends Vehicle
{
}
class Door extends Vehicle
{
}
class DirectorTest
{
    public function testCanBuildTruck()
    {
        $truckBuilder = new TruckBuilder();
        return (new Director())->build($truckBuilder);
    }
    public function testCanBuildCar()
    {
        $carBuilder = new CarBuilder();
        return (new Director())->build($carBuilder);
    }
}
$directorTest = new DirectorTest();
var_dump($directorTest->testCanBuildTruck());
var_dump($directorTest->testCanBuildCar());

以上就是PHP设计模式(创立型)的具体内容,更多请存眷资源魔其它相干文章!

标签: php开发教程 php开发资料 php开发自学 PHP设计模式

抱歉,评论功能暂时关闭!