简单工厂,工厂方法和抽象工厂模式

简单工厂:在开闭原则下不支持添加新产品。
工厂方法:支持任意添加新产品。
抽象工厂:不支持添加新产品,支持增加产品族。

个人理解:工厂方法可以看做是抽象工厂的一个特例。

简单工厂代码:

/**
 * @author  nhpeng,nhpeng1104@gmail.com
 * @date    2015/08/17 15:29:22 CST
 * @brief   简单工厂模式
 *
 * 
 */
#include <stdio.h>

class Battle
{
    public:
        Battle( ){};
        virtual ~Battle( ){};

        virtual void Whoiam( ){ printf( "Battle\n" ); }
};

class CommonBattle : public Battle
{
    public:
        CommonBattle( ){};
        virtual ~CommonBattle( ){};

        virtual void Whoiam( ){ printf( "CommonBattle\n" ); }

class SpecialBattle : public Battle
{
    public:
        SpecialBattle( ){};
        virtual ~SpecialBattle( ){};

        virtual void Whoiam( ){ printf( "SpecialBattle\n" ); }
};

enum BattleType
{
    BattleType_Common, 
    BattleType_Special, 
};

class BattleFactory
{
    public:
    BattleFactory( ){};
    ~BattleFactory( ){};

    Battle *CreateBattle( enum BattleType battle_type );
};

Battle *BattleFactory::CreateBattle( enum BattleType battle_type )
{
    switch( battle_type )
    {
        case BattleType_Common:
            return new CommonBattle( );
            break;
        case BattleType_Special:
            return new SpecialBattle( );
            break;
            default:
            break;
    }
    return NULL;
}

int main( )
{
    BattleFactory factory;
    Battle *common_battle = factory.CreateBattle( BattleType_Common );
    Battle *special_battle = factory.CreateBattle( BattleType_Special );
    if( !common_battle ) return -1;
    if( !special_battle ) return -2;
    common_battle->Whoiam( );
    special_battle->Whoiam( );
    delete common_battle;
    delete special_battle;
    return 0;
}

简单工厂如果要增加产品,就必须在BattleFactory中添加代码,所以我们说在开闭原则下,简单工厂是不支持增加产品的。
而工厂方法是每种产品设计一个工厂类,如果要添加产品是不会违反开闭原则的。
工厂方法代码:

/**
 * @author  nhpeng,nhpeng1104@gmail.com
 * @date    2015/08/17 15:29:22 CST
 * @brief   工厂方法模式
 *
 * 
 */
#include <stdio.h>

class Battle
{
    public:
        Battle( ){};
        virtual ~Battle( ){};

        virtual void Whoiam( ){ printf( "Battle\n" ); }
};

class CommonBattle : public Battle
{
    public:
        CommonBattle( ){};
        virtual ~CommonBattle( ){};

        virtual void Whoiam( ){ printf( "CommonBattle\n" ); }
};

class SpecialBattle : public Battle
{
    public:
        SpecialBattle( ){};
        virtual ~SpecialBattle( ){};

        virtual void Whoiam( ){ printf( "SpecialBattle\n" ); }
};

class BattleFactory
{
    public:
    BattleFactory( ){};
    ~BattleFactory( ){};

    virtual Battle *CreateBattle( );
};

class CommonBattleFactory
{
    public:
    CommonBattleFactory( ){};
    ~CommonBattleFactory( ){};

    virtual Battle *CreateBattle( );
};

Battle *CommonBattleFactory::CreateBattle( )
{
    return new CommonBattle( );
}

class SpecialCommonBattleFactory
{
    public:
    SpecialCommonBattleFactory( ){};
    ~SpecialCommonBattleFactory( ){};

    virtual Battle *CreateBattle( );
};

Battle *SpecialBattleFactory::CreateBattle( )
{
    return new SpecialBattle( );
}

int main( )
{
    BattleFactory common_factory;
    BattleFactory special_factory;
    Battle *common_battle = common_factory.CreateBattle( );
    Battle *special_battle = special_factory.CreateBattle( );
    if( !common_battle ) return -1;
    if( !special_battle ) return -2;
    common_battle->Whoiam( );
    special_battle->Whoiam( );
    delete common_battle;
    delete special_special_battle;
    return 0;
}

而抽象工厂则可以把工厂方法延伸一下,再设计一个War的产品类,和Battle同级,有子类CommonWar,和SpecialWar。

如此可以设计两个工厂:CommonFactory,SpecialFactory,每个工厂有两个方法:CreateBattle,CreateWar,这样把Battle和War的元素抽象出来设计工厂,是为抽象工厂(也可以把Battle和War抽象出来对吧),Common和Special就是产品族。Battle和War是产品。所以可以看出,抽象工厂是可以增加产品族,而不能增加产品。

抽象工厂代码:

/**
 * @author  nhpeng,nhpeng1104@gmail.com
 * @date    2015/08/17 15:29:22 CST
 * @brief   抽象工厂模式
 *
 * 
 */
#include <stdio.h>

class Battle
{
    public:
        Battle( ){};
        virtual ~Battle( ){};

        virtual void Whoiam( ){ printf( "Battle\n" ); }
};

class CommonBattle : public Battle
{
    public:
        CommonBattle( ){};
        virtual ~CommonBattle( ){};

        virtual void Whoiam( ){ printf( "CommonBattle\n" ); }
};

class SpecialBattle : public Battle
{
    public:
        SpecialBattle( ){};
        virtual ~SpecialBattle( ){};

        virtual void Whoiam( ){ printf( "SpecialBattle\n" ); }
};

class War 
{
    public:
        War( ){};
        virtual ~War( ){};

        virtual void Whoiam( ){ printf( "War\n" ); }
};

class CommonWar : public War
{
    public:
        CommonWar( ){};
        virtual ~CommonWar( ){};

        virtual void Whoiam( ){ printf( "CommonWar\n" ); }
};

class SpecialWar : public War
{
    public:
        SpecialWar( ){};
        virtual ~SpecialWar( ){};

        virtual void Whoiam( ){ printf( "SpecialWar\n" ); }
};

class Factory
{
    public:
        Factory( ){};
        virtual ~Factory( ){};

        virtual Battle *CreateBattle( ){};
        virtual War *CreateWar( ){};
};

class CommonFactory : public Factory
{
    public:
        CommonFactory( ){};
        virtual ~CommonFactory( ){};

        virtual Battle *CreateBattle( );
        virtual War *CreateWar( );
};

Battle *CommonFactory::CreateBattle( )
{
    return new CommonBattle( );
}

War *CommonFactory::CreateWar( )
{
    return new CommonWar( );
}

class SpecialFactory : public Factory
{
    public:
        SpecialFactory( ){};
        virtual ~SpecialFactory( ){};

        virtual Battle *CreateBattle( );
        virtual War *CreateWar( );
};

Battle *SpecialFactory::CreateBattle( )
{
    return new SpecialBattle( );
}

War *SpecialFactory::CreateWar( )
{
    return new SpecialWar( );
}

int main( )
{
    CommonFactory common_factory;
    SpecialFactory special_factory;
    Battle *common_battle = common_factory.CreateBattle( );
    Battle *special_battle = special_factory.CreateBattle( );
    if( !common_battle ) return -1;
    if( !special_battle ) return -2;
    common_battle->Whoiam( );
    special_battle->Whoiam( );
    War *common_war = common_factory.CreateWar( );
    War *special_war = special_factory.CreateWar( );
    if( !common_war ) return -3;
    if( !special_war ) return -4;
    common_war->Whoiam( );
    special_war->Whoiam( );
    delete common_battle;
    delete special_battle;
    delete common_war;
    delete special_war;
    return 0;
}

为什么可以把工厂方法看做是抽象工厂的一种特例呢,需要转换角度,把工厂方法的产品看成产品族。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值