zoukankan      html  css  js  c++  java
  • 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
    

      

  • 相关阅读:
    HDU 2757 Ocean Currents
    HDU 2704 Bulletin Board
    HDU 2234 无题I
    HDU 3638 Go , SuSu
    HDU 1199 Color the Ball
    HDU 1430 魔板
    PL/SQL例外的介绍
    表分区介绍
    移动表空间数据文件
    long\lob\bfile类型介绍
  • 原文地址:https://www.cnblogs.com/Czc963239044/p/7116030.html
Copyright © 2011-2022 走看看