• Builder(构造者)


    Builder(构造者)

    <?php
     
    class Product {
     
        private $name;
     
        public function setName($name) {
            $this->name = $name;
        }
     
        public function getName() {
            return $this->name;
        }
    }
     
    abstract class Builder {
     
        protected $product;
     
        final public function getProduct() {
            return $this->product;
        }
     
        public function buildProduct() {
            $this->product = new Product();
        }
    }
     
    class FirstBuilder extends Builder {
     
        public function buildProduct() {
            parent::buildProduct();
            $this->product->setName('The product of the first builder');
        }
    }
     
    class SecondBuilder extends Builder {
     
        public function buildProduct() {
            parent::buildProduct();
            $this->product->setName('The product of second builder');
        }
    }
     
    class Factory {
     
        private $builder;
     
        public function __construct(Builder $builder) {
            $this->builder = $builder;
            $this->builder->buildProduct();
        }
     
        public function getProduct() {
            return $this->builder->getProduct();
        }
    }
     
    $firstDirector = new Factory(new FirstBuilder());
    $secondDirector = new Factory(new SecondBuilder());
     
    print_r($firstDirector->getProduct()->getName());
    // The product of the first builder
    print_r($secondDirector->getProduct()->getName());
    // The product of second builder
    

      

  • 相关阅读:
    Unique path
    *Jump Game
    Valid Palindrome
    *Reverse Words in a String
    Min Stack
    [?]*Simplify Path
    *Valid Parentheses
    *Sqrt(x)
    String to Integer (atoi)
    Add Digits
  • 原文地址:https://www.cnblogs.com/Czc963239044/p/7116030.html
Copyright © 2020-2023  润新知