php-抽象工厂
Posted 白云千载空悠悠
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php-抽象工厂相关的知识,希望对你有一定的参考价值。
目标:创建有依赖关系的实例;(套餐)
<?php //抽象类 食物 interface IAllayFood { function Allay(); } interface IDrinkFood { function Drink(); }
<?php //抽象类 获取食物 --》客户 interface Ifactorys { function getAllayFood(); function getDrinckFood(); }
<?php include "D:\xxxxcx_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:\xxxxcp_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-抽象工厂的主要内容,如果未能解决你的问题,请参考以下文章