assessmentee.com

Home | Start test | Contact
 

Activerecord | Adapter | Decorator | DependencyInjectionContainer | Facade | Factory | Hydration | Inheritance | Iterator | Mapper | MVC | Observer | Prototype | Proxy | Registry | ServiceLocator | Singleton | Specification | Strategy | TableGateway | ZF1_TableGateway | ZF2_TableGateway |




    <?php
/*
 * Factory design pattern
 * Instantiate class based on input
 */


class CanFactory {

     public static function can($product){
         $class = 'can'.ucfirst($product);
         if(class_exists($class)){
             return new $class();
         }
     }
}

interface Can {
    public function getWeight();
}

class CanSalmon implements Can {
    public function getWeight(){
        return 500;
    }
}

class CanTuna implements Can {
     public function getWeight(){
        return 200;
    }
}

echo CanFactory::can('salmon')->getWeight();
?>


    

Outputs:


500



 

© 2018 assessmentee.com