[PHP]An example of product mode

2015-11-12 写技术

/* Here is an example of Product mode */

interface iProduct{

        public function realCreate();
}
class Product1{
        public function action(){
                echo " here is product 1";
        }
}
class Product2{
        public function action(){
                echo "this is product 2";
        }
}
class Product1Creator implements iProduct{
        public $id;
        public function realCreate(){
                return new Product1();
        }
}
class Product2Creator implements iProduct{
        public $id;
        public function realCreate(){
                return new Product2();
        }
}
class ProductFactory{
        public function create($what){
                $create = $what."Creator";
                return $create = new $create();
        }
}
$create = new ProductFactory();
$instance = $create->create("Product1");
$instance->id = 11;
$instance->realCreate()->action();

标签: php

发表评论:

Powered by anycle 湘ICP备15001973号-1