Blogger Information
Blog 32
fans 2
comment 2
visits 23310
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
【Part3】单例模式/普通和抽象工厂模式演示(0218)
暴风战斧
Original
603 people have browsed it

【抽象工厂】

<?php

namespace day18;

// 导入商城类
use chapter5\D;
use me\base\inc02\iShop;
use me\base\inc02\Jd;
use me\base\inc02\Duo;
use me\base\inc02\Tmall;

// 自动加载
require __DIR__ . '/autoload.php';

// 商城类
class Shopping2
{
    // 商城平台
    private $shop = null;

    // 由抽象工厂类完成实例化依赖对象的过程
    public function __construct(iShop $shop)
    {
        // 将依赖工厂类换成面向接口的抽象工厂模式
        $this->shop = $shop;
    }

    // 调用外部依赖对象的方法
    public function goShopping() {
        return $this->shop->shop() . ' --> 省钱更省心!';
    }
}

// 客户端调用
echo (new Shopping2(new Tmall()))->goShopping() . '<br>';
echo (new Shopping2(new Duo()))->goShopping() . '<br>';
echo (new Shopping2(new Jd()))->goShopping() . '<br>';

class Shopping2_1
{

    public function goShopping(iShop $shop) {
        return $shop->shop() . ' ==> 省钱更省心!';
    }
}

echo '<hr>';
// 客户端调用:在普通方法中调用
echo (new Shopping2_1())->goShopping(new Tmall()) . '<br>';
echo (new Shopping2_1())->goShopping(new Duo()) . '<br>';
echo (new Shopping2_1())->goShopping(new Jd()) . '<br>';

抽象工程及扩展.png

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:下次注意, 一天的作业不要分成多次提交, 不便于审核
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments