码迷,mamicode.com
首页 > 其他好文 > 详细

工厂模式

时间:2017-07-04 12:13:18      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:需要   second   dfa   class   cto   actor   imp   blog   interface   

工厂模式是另一种非常常用的模式,正如其名字所示:确实是对象实例的生产工厂。某些意义上,工厂模式提供了通用的方法有助于我们去获取对象,而不需要关心其具体的内在的实现

<?php
 
interface Factory {
    public function getProduct();
}
 
interface Product {
    public function getName();
}
 
class FirstFactory implements Factory {
 
    public function getProduct() {
        return new FirstProduct();
    }
}
 
class SecondFactory implements Factory {
 
    public function getProduct() {
        return new SecondProduct();
    }
}
 
class FirstProduct implements Product {
 
    public function getName() {
        return ‘The first product‘;
    }
}
 
class SecondProduct implements Product {
 
    public function getName() {
        return ‘Second product‘;
    }
}
 
$factory = new FirstFactory();
$firstProduct = $factory->getProduct();
$factory = new SecondFactory();
$secondProduct = $factory->getProduct();
 
print_r($firstProduct->getName());
// The first product
print_r($secondProduct->getName());
// Second product

  

工厂模式

标签:需要   second   dfa   class   cto   actor   imp   blog   interface   

原文地址:http://www.cnblogs.com/Czc963239044/p/7115748.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!