php-抽象工厂

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

<?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();

 

posted @ 2019-11-30 19:40  千载白云  阅读(199)  评论(0编辑  收藏  举报