抽象工厂模式实例与讲解

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_25551295/article/details/49850167

<?php
/**
 * Created by PhpStorm.
 * User: captain
 * Date: 15/11/15
 * Time: 下午4:11
 */

abstract class ApptEncoder{
    abstract function encode();
}

class BloggsApptEncoder extends ApptEncoder{
    function encode(){
        return 'this is BloggsApptEncoder object';
    }
}

class MegaApptEncoder extends ApptEncoder{
    function encode(){
        return 'this is MegaApptEncoder object';
    }
}

abstract class ContactEncoder{
    abstract function encode();
}

class BloggsContactEncoder extends ContactEncoder{
    function encode(){
        return 'this is BloggsContactEncoder object';
    }
}

class MegaContactEncoder extends ContactEncoder{
    function encode(){
        return 'this is MegaContactEncoder object';
    }
}

abstract class ContachEncoder{
    abstract function encode();
}

class BloggsContachEncoder extends ContachEncoder{
    function encode(){
        return 'this is BloggsContachEncoder object';
    }
}

class MegaContachEncoder extends ContachEncoder{
    function encode(){
        return 'this is MegaContachEncoder object';
    }
}

abstract class TtdEncoder{
    abstract function encode();
}

class BloggsTtdEncoder extends TtdEncoder{
    function encode(){
        return 'this is BloggsContactEncoder object';
    }
}

class MegaTtdEncoder extends TtdEncoder{
    function encode(){
        return 'this is MegaApptContactEncoder object';
    }
}

abstract class CommsManager{
    const APPT = 1;
    const TTD = 2;
    const CONTACT = 3;
    abstract function getHeaderText();

    abstract function make($flat_int);

    abstract function getFooterText();
}

class BloggsCommsManager extends CommsManager{
    function getHeaderText(){
        return 'this is BloggsCommsManager header';
    }


    function make($flag_int){
        switch ($flag_int){
            case self::APPT:
                return new BloggsApptEncoder();
            case self::CONTACT:
                return new BloggsContachEncoder();
            case self::TTD:
                return new BloggsTtdEncoder();
        }
    }


    function getFooterText(){
        return 'this is BloggsCommsManager footer';
    }
}


class MegaCommsManager extends CommsManager{
    function getHeaderText(){
        return 'this is MegaCommsManager header';
    }


    function make($flag_int){
        switch ($flag_int){
            case self::APPT:
                return new MegaApptEncoder();
            case self::CONTACT:
                return new MegaContachEncoder();
            case self::TTD:
                return new MegaTtdEncoder();
        }
    }


    function getFooterText(){
        return 'this is MegaCommsManager footer';
    }
}

从上面的例子我们可以看出,我们将工厂(也就是创建者)抽象化了,也就是我们的抽象工厂模式,我们将工厂抽象出来,让子工厂去继承抽象的父工厂,然后在子工厂生产具体的产品(也就是调用不同的产品方法)。这样,我们的工厂就更加灵活。例如,我们此时想声场以一种新的产品(也就是创建一种新的工厂,我们只需要创建新的子工厂,让它继承抽象的父工厂,然后生产对应的产品即可)。例如,我们此时,新建了新的工厂Test1CommsManager:

class Test1CommsManager extends CommsManager{
    function getHeaderText(){
        return 'this is Test1CommsManager header';
    }


    function make($flag_int){
        switch ($flag_int){
            case self::APPT:
                return new MegaApptEncoder();
            case self::CONTACT:
                return new MegaContachEncoder();
            case self::TTD:
                return new MegaTtdEncoder();
        }
    }


    function getFooterText(){
        return 'this is Test1CommsManager footer';
    }
}




展开阅读全文

没有更多推荐了,返回首页