zoukankan      html  css  js  c++  java
  • 设计模式:工厂模式

    一、工厂方法模式

      工厂方法模式。定义为:Define an interface for creating an object,but let subclasses decide which class toinstantiate.Factory Method lets a class defer instantiation to subclasses.(定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法使一个类的实例化延迟到其子类。)

    工厂模式通用类图

    <?php
       interface  zoon {
               function introduce();
       }
    
       class dog implements zoon{
               function introduce(){
                echo "我是小狗,我汪汪汪叫" . PHP_EOL;
            }
       }
    
       class pig implements zoon{
               function introduce(){
                echo "我是小猪,我哼哼哼叫" . PHP_EOL;
            }
       }
    
       class sheep implements zoon{
               function introduce(){
                echo "我是小羊,我咩咩咩叫" . PHP_EOL;
            }
       }
      
       interface createZoon{
               function create();
       }
       
       class FactoryDod implements createZoon{
           function create(){
                   return new dog();
           }
               
       }
    
       class FactoryPig implements createZoon{
                function create(){
                   return new pig();
           }
       }
    
    
       class FactorySheep implements createZoon{
                function create(){
                   return new sheep();
           }
       }
    
    
      class Factory{
              function __construct(){
                $d = new FactoryDod();
                $d->create()->introduce();
          
                $p = new FactoryPig();
                $d->create()->introduce();
          
                $s = new Factorysheep();
                $s->create()->introduce();
            }
      }
    
      $t = new Factory();

      

     二、简单工厂模式

        

    <?php
       interface  zoon {
               function introduce();
       }
    
       class dog implements zoon{
               function introduce(){
                echo "我是小狗,我汪汪汪叫" . PHP_EOL;
            }
       }
    
       class pig implements zoon{
               function introduce(){
                echo "我是小猪,我哼哼哼叫" . PHP_EOL;
            }
       }
    
       class sheep implements zoon{
               function introduce(){
                echo "我是小羊,我咩咩咩叫" . PHP_EOL;
            }
       }
      
       class  FactoryDod {
          static function create(){
                   return new dog();
           }
               
       }
    
       class  FactoryPig{
           static function  create(){
                   return new pig();
           }
       }
    
    
       class FactorySheep{
           static  function  create(){
                   return new sheep();
           }
       }
    
    
      class Factory{
              function __construct(){
                $d = FactoryDod::create();
                $d->introduce();
          
                $p = FactoryPig::create();
                $d->introduce();
          
                $s = Factorysheep::create();
                $s->introduce();
            }
      }
    
      $t = new Factory();

    三、抽象工厂模式

      抽象工厂模式(Abstract Factory Pattern)定义:Provide an interface for creating families of related or dependent objects without specifyingtheir concrete classes.(为创建一组相关或相互依赖的对象提供一个接口,而且无须指定它们的具体类。)

    <?php
        interface Human {
            function getColor();
            function talk();
            function getSex();
        }
    
        abstract class abstractWhiteHuman implements Human{
            public function getColor(){
                echo "皮肤颜色:白色" . PHP_EOL;
            }
            
            public function talk(){
                echo "语言:白人语言" . PHP_EOL;
            }
        }
    
        abstract class abstractBlackHuman implements Human{
            public function getColor(){
                echo "皮肤颜色:黑色" . PHP_EOL;
            }
            
            public function talk(){
                echo "语言:黑人语言" . PHP_EOL;
            }
        }
    
        abstract class abstractYellowHuman implements Human{
            public function getColor(){
                echo "皮肤颜色:黄色" . PHP_EOL;
            }
            
            public function talk(){
                echo "语言:黄人语言" . PHP_EOL;
            }
        }
    
        class FemaleYellowHuman extends abstractYellowHuman{
            function getSex(){
                echo "性别:女性" . PHP_EOL;
            }
        }
    
        class MaleYellowHuman extends abstractYellowHuman{
            function getSex(){
                echo "性别:男性" . PHP_EOL;
            }
        }
        
    
        class MaleBlackHuman extends abstractYellowHuman{
            public function getSex(){
                echo "性别:男性" . PHP_EOL;
            }
        }
        
        class MaleWhiteHuman extends abstractYellowHuman{
            public function getSex(){
                echo "性别:男性" . PHP_EOL;
            }
        }
    
        interface HumanFactory{
            function createYellowHuman();
            function createWhiteHuman();
            function createBlackHuman();
        }
    
        class FemaleFactory implements HumanFactory{
            function createBlackHuman(){
                return new FemaleBlackHuman();
            }
            function createWhiteHuman(){
                return new FemaleWhiteHuman();
            }
            function createYellowHuman(){
                return new FemaleYellowHuman();
            }
        }
    
        class MaleFactory implements HumanFactory{
            function createBlackHuman(){
                return new MaleBlackHuman();
            }
            function createWhiteHuman(){
                return new MaleWhiteHuman();
            }
            function createYellowHuman(){
                return new MaleYellowHuman();
            }
        }
    
        class main{
            public function create(){
                $maleHumanFactory = new MaleFactory();
                $femaleHumanFactory = new FemaleFactory();
                $maleYellowHuman = $maleHumanFactory->createYellowHuman();
                $maleYellowHuman->getColor();
                $maleYellowHuman->talk();
                $maleYellowHuman->getSex();
            }
        }
    
        $t = new main();
        $t->create();
        
  • 相关阅读:
    判断表字段是否存在default约束
    在Eclipse mars 4.5.2 中安装spring 插件 spring tool suite
    MySql (mysql-5.6.37) 在Windows的安装及使用
    在CentOS7.2中搭建Tomcat9 并启用http/2 协议
    CentOS 7.2 中 Kafka,Zookeeper的单机部署,伪分布式部署以及真正的分布式部署
    博客歇菜后的总结
    通过WiFi连接手机(device), 出去数据线的烦恼
    Android Studio 使用三星 Note4 真机调试
    在Mac team 工作的那段日子里(一)
    又到年底了,没钱回家咋办?
  • 原文地址:https://www.cnblogs.com/onlycat/p/9164529.html
Copyright © 2011-2022 走看看