<?php
/**
* Created by PhpStorm.
* User: Jerry
* Date: 2019/10/26
* Time: 16:23
*/
class apple{
public static function getFruit()
{
echo '吃大苹果';
}
}
class pear{
public static function getFruit()
{
echo '吃大梨子';
}
}
class FruitFactory{
static function makeDrink($fruit){
if ($fruit == 'apple'){
return new apple();
}elseif ($fruit == 'pear'){
return new pear();
}
}
}
$factory = new FruitFactory();
$factory::makeDrink('apple');
//工厂 对扩展开放
interface fruit{
public function getFruit();
}
class apple implements fruit {
public static function getFruit()
{
echo '吃大苹果';
}
}
class pear implements fruit {
public static function getFruit()
{
echo '吃大梨子';
}
}
interface factory{
function makeDrinK();
}
class appleFactory implements factory {
function makeDrink(){
return new apple();
}
}
//增加对应工厂
//抽象工厂
//抽象产品
interface apple {
function taste();
}
interface pear {
function taste();
}
//具体产品
class goodApple implements apple {
function taste(){
echo '好苹果好吃……';
}
}
class badApple implements apple {
function taste(){
echo '坏苹果不好吃……';
}
}
class goodPear implements pear {
function taste(){
echo '好梨好吃……';
}
}
class badPear implements pear {
function taste(){
echo '坏梨不好吃……';
}
}
interface FruitFactory {
public function apple();
public function pear();
}
//具体工厂
class GoodFruitFactory implements FruitFactory {
function apple(){
return new goodApple();
}
function pear(){
return new goodPear();
}
}
class BadFruitFactory implements FruitFactory {
function apple(){
return new badFruit();
}
function pear(){
return new badFruit();
}
}
//买家
class Man {
public static function main() {
self::buy(new GoodFruitFactory());
self::buy(new BadFruitFactory());
}
public static function buy(FruitFactory $FruitFactory){
$apple = $FruitFactory->apple();
$apple->taste();
$pear = $FruitFactory->pear();
$pear->taste();
}
}
Man::main();