码迷,mamicode.com
首页 > Web开发 > 详细

php-抽象工厂

时间:2019-11-30 21:05:39      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:cli   UNC   interface   actor   code   click   依赖   php   xxx   

目标:创建有依赖关系的实例;(套餐)

<?php
//抽象类 食物
interface IAllayFood {
    function Allay();

}
interface IDrinkFood {
    function Drink();
}

 

<?php
//抽象类 获取食物 --》客户
interface Ifactorys {
    function getAllayFood();
    function getDrinckFood();
}

 

<?php 
include "D:\\xxxx\cx_gc.php";
class Afactory implements Ifactorys
{
    function getAllayFood()
    {
        return new hanbao();
    }

    function getDrinckFood()
    {
        return new coco();
    }
}


class Bfactory implements Ifactorys
{
    function getAllayFood()
    {
        return new chicken();
    }

    function getDrinckFood()
    {
        return new baishi();
    }
}

 

<?php
include "D:\\xxxx\cp_cx.php";
class chicken implements IAllayFood 
{
    function Allay() {
        echo ‘鸡肉给小妹妹解饿‘."<br/>";
    }
}

class hanbao implements IAllayFood 
{
    function Allay() {
    
        echo ‘汉堡给小妹妹解饿‘."<br/>";
    }
}


class coco implements IDrinkFood 
{
    function Drink() {
        echo ‘可口可乐给小妹妹解饿‘."<br/>";
    }
}

class baishi implements IDrinkFood 
{
    function Drink() {
        echo ‘百世可乐给小妹妹解饿‘."<br/>";
    }
}

 

<?php
require "./jt_sl.php";
require "./jt_gc.php";

$a = new Afactory();
$b = new Bfactory();

$achi = $a->getAllayFood()->Allay();
$ahe = $a->getDrinckFood()->Drink();
//print_r($ahe);die;


$achi = $b->getAllayFood()->Allay();
$ahe = $b->getDrinckFood()->Drink();

 

php-抽象工厂

标签:cli   UNC   interface   actor   code   click   依赖   php   xxx   

原文地址:https://www.cnblogs.com/songyanan/p/11963688.html

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