zoukankan      html  css  js  c++  java
  • 设计模式---工厂模式Factory(创建型)

    1. 概述

      在编程中,经常需要new一些对象,但是在一些情况下, new操作直接生成对象会带来一些问题。举例来说,许多类型对象的创建需要一系列的步骤,在这种情况下,新对象的建立就是一个“过程”,不仅是一个操作。所以,如何能轻松方便地构造对象实例,而不关心构造对象实例的细节和复杂过程呢?

    2. 应用场景

      简化创建对象实例的操作,屏蔽创建对象的具体过程。

    3. 示例

    (1)简单工厂模式

      建立一个工厂(一个函数或一个类方法)来制造新的对象。

     1 <?php  
     2 /** 
     3  * 车子系列 
     4  * 
     5  */  
     6 abstract Class BWM{  
     7     function __construct($pa) {  
     8   
     9     }  
    10 }  
    11 Class BWM320 extends BWM{  
    12     function __construct($pa) {  
    13   
    14     }  
    15 }  
    16 Class BMW523 extends BWM{  
    17    function __construc($pb){  
    18   
    19    }  
    20 }  
    21 /** 
    22  *  
    23  * 工厂创建车 
    24  */  
    25 class Factory {  
    26   
    27   
    28     static function  createBMW($type){  
    29         switch ($type) {  
    30           case 320:  
    31              return new BWM320();  
    32           case 523:  
    33              return new BMW523();  
    34         //....  
    35    }  
    36 }  
    37 /** 
    38  *  
    39  * 客户通过工厂获取车 
    40  */  
    41 class Customer {  
    42     private $BMW;  
    43     function getBMW($type){  
    44         $this¬-> BMW =  Factory::createBMW($type);  
    45     }  
    46 }  

    (2)工厂方法模式

     1 <?php  
     2 /** 
     3  * 车子系列 
     4  * 
     5  */  
     6 abstract Class BWM{  
     7     function __construct($pa) {  
     8   
     9     }  
    10 }  
    11 Class BWM320 extends BWM{  
    12     function __construct($pa) {  
    13   
    14     }  
    15 }  
    16 Class BMW523 extends BWM{  
    17     function __construc($pb){  
    18   
    19     }  
    20 }  
    21 /** 
    22  * 创建工厂的接口 
    23  * 
    24  */  
    25 interface FactoryBMW {   
    26        function createBMW();   
    27 }   
    28   
    29   
    30 /** 
    31  *  
    32  * 创建BWM320车 
    33  */  
    34 class FactoryBWM320 implements FactoryBMW {  
    35    function  createBMW($type){  
    36       return new BWM320();  
    37    }  
    38   
    39 }  
    40   
    41   
    42 /** 
    43  *  
    44  * 创建BWM523车 
    45  */  
    46 class FactoryBWM523 implements FactoryBMW {  
    47    function  createBMW($type){  
    48       return new BMW523();  
    49    }  
    50 }  
    51 /** 
    52  *  
    53  * 客户得到车 
    54  */  
    55 class Customer {  
    56    private $BMW;  
    57    function  getBMW($type){  
    58       switch ($type) {  
    59         case 320:  
    60            $BWM320 = new FactoryBWM320();  
    61            return $BWM320->createBMW();  
    62         case 523:  
    63            $BWM523 = new FactoryBWM523();  
    64            return $BWM320->createBMW();  
    65             //....  
    66       }  
    67   
    68   }  
    69 }  
    70 class Customer {  
    71      private $BMW;  
    72      function  getBMW($type){  
    73          $class = new ReflectionClass('FactoryBWM' .$type );//建立 'FactoryBWM'这个类的反射类    
    74           $instance  = $class->newInstanceArgs();//相当于实例化'FactoryBWM' .$type类    
    75           return $instance->createBMW();  
    76         //或者直接   
    77          /** 
    78          * $instance = new 'FactoryBWM' .$type(); 
    79          * return $instance->createBMW(); 
    80          */  
    81     }  
    82 }  

    (3)抽象工厂模式

     1 <?php  
     2 /** 
     3  * 车子系列以及型号 
     4  * 
     5  */  
     6 abstract class  BWM{  
     7 }  
     8   
     9 class BWM523 extends  BWM {  
    10 }  
    11 class BWM320 extends  BWM {  
    12   
    13   
    14 }  
    15 /** 
    16  * 空调 
    17  * 
    18  */  
    19 abstract class aircondition{  
    20 }  
    21 class airconditionBWM320  extends aircondition {  
    22   
    23 }  
    24 class airconditionBWM52 extends aircondition {  
    25   
    26 }  
    27 /** 
    28  * 创建工厂的接口 
    29  * 
    30  */  
    31 interface FactoryBMW {   
    32      function createBMW();   
    33      function createAirC();   
    34 }   
    35   
    36   
    37 /** 
    38  *  
    39  * 创建BWM320车 
    40  */  
    41 class FactoryBWM320 implements FactoryBMW {  
    42     function  createBMW(){  
    43         return new BWM320();  
    44     }  
    45     function  createAirC(){ //空调  
    46         return new airconditionBWM320();  
    47     }  
    48 }  
    49   
    50   
    51 /** 
    52  *  
    53  * 创建BWM523车 
    54  */  
    55 class FactoryBWM523 implements FactoryBMW {  
    56     function  createBMW(){  
    57         return new BWM523();  
    58     }  
    59     function  createAirC(){  
    60         return new airconditionBWM523();  
    61     }  
    62 }  
    63 /** 
    64  *  
    65  * 客户得到车 
    66  */  
    67 class Customer {  
    68    private $BMW;  
    69    private $airC;  
    70    function  getBMW($type){  
    71        $class = new ReflectionClass('FactoryBWM' .$type );//建立 Person这个类的反射类    
    72         $instance  = $class->newInstanceArgs();//相当于实例化Person 类    
    73         $this->BMW =  $instance->createBMW();  
    74        $this->airC =  $instance->createAirC();  
    75    }  
    76 }  
  • 相关阅读:
    @RequestParam注解使用:Name for argument type [java.lang.String] not available, and parameter name information not found in class file either.
    cglib动态代理导致注解丢失问题及如何修改注解允许被继承
    springboot Autowired BeanNotOfRequiredTypeException
    git根据用户过滤提交记录
    不同包下,相同数据结构的两个类进行转换
    How to use Jackson to deserialise an array of objects
    jooq实践
    java如何寻找main函数对应的类
    Python--matplotlib
    Python 和 Scikit-Learn
  • 原文地址:https://www.cnblogs.com/sydeveloper/p/3777911.html
Copyright © 2011-2022 走看看