zoukankan      html  css  js  c++  java
  • 空对象模式

     1 <?php
     2 
     3 abstract class Person
     4 {
     5     protected $_name;
     6 
     7     abstract public function getName();
     8 }
     9 
    10 class RealPerson extends Person
    11 {
    12     function __construct($name)
    13     {
    14         $this->_name = $name;
    15     }
    16 
    17     public function getName()
    18     {
    19         echo "我的名字是:{$this->_name}";
    20     }
    21 }
    22 
    23 class NullPerson extends Person
    24 {
    25     public function getName()
    26     {
    27         echo "<br/>我不是人类哦";
    28     }
    29 }
    30 
    31 
    32 
    33 
    34 class Factory
    35 {
    36     private $_names = ['A', 'B', 'C'];
    37 
    38     public function produce($personName)
    39     {
    40         if (array_search($personName, $this->_names) !== false) {
    41             return new RealPerson($personName);
    42         }
    43 
    44         return new NullPerson();
    45     }
    46 }
    47 
    48 
    49 
    50 $f = new Factory();
    51 $p1 = $f->produce('A');
    52 $p1->getName();
    53 
    54 $p2 = $f->produce('G');
    55 $p2->getName();
    View Code
  • 相关阅读:
    表单数据源控制器笔记
    方法汇总1
    Persistence.beans
    数据库外键理解
    SQL语句缺少
    树型结构需要绑定的字段
    模型绑定替换
    乱码
    表单的验证:客户端验证和服务器端验证
    表单
  • 原文地址:https://www.cnblogs.com/hangtt/p/6258870.html
Copyright © 2011-2022 走看看