zoukankan      html  css  js  c++  java
  • php原型模式的研究

     1 <?php
     2 
     3 class Sea{}
     4 class EarthSea extends Sea{}
     5 class MarsSea extends Sea{}
     6 
     7 class Plains{}
     8 class EarthPlains{}
     9 class MarsPlains{}
    10 
    11 class Forest{}
    12 class EarthForest{}
    13 class MarsForest{}
    14 
    15 
    16 
    17 class TerrainFactory{
    18     private $sea;
    19     private $forest;
    20     private $plains;
    21     
    22     function __construct(Sea $sea,Plains $plains,Forest $forest){
    23         $this->sea = $sea;
    24         $this->plains = $plains;
    25         $this->forest = $forest;
    26     }
    27     
    28     function getSea(){
    29         return clone $this->sea;
    30     }
    31     function getPlains(){
    32         return clone $this->plains;
    33     }
    34     function getForest(){
    35         return clone $this->forest;
    36     }
    37 }
    38 
    39 
    40 $factory = new TerrainFactory(new EarthSea,new EarthPlains, new EarthForest);
    41 print_r($factory->getSea());
    42 print_r($factory->getPlains());
    43 print_r($factory->getForest());
    44 
    45 $mars_factory = new TerrainFactory(new MarsSea,new MarsPlains, new MarsForest);
    46 print_r($mars_factory->getSea());
    47 print_r($mars_factory->getPlains());
    48 print_r($mars_factory->getForest());
  • 相关阅读:
    Lombok介绍、使用方法和总结
    Vargant centOS7安装
    Nginx
    Docker
    GOPATH
    Golang http
    /^正则表达式$/
    go: missing Git command. See https://golang.org/s/gogetcmd
    Golang 反射
    Golang 常量
  • 原文地址:https://www.cnblogs.com/jami918/p/3614366.html
Copyright © 2011-2022 走看看