工厂模式-设计模式coding

简单工厂模式

  • 最简单,高内聚性不好
  • 不属于23种设计模式中的设计模式
#include <iostream>
using namespace std;

class Fruit
{
public:
    virtual void getFruit() = 0;
};
class Banana: public Fruit
{
public:
    virtual void getFruit()
    {
        cout << "banana" << endl;
    }
};
class Apple: public Fruit
{
public:
    virtual void getFruit()
    {
        cout << "apple" << endl;
    };
};

class Factory
{
public:
    Fruit* Create(const char* p)
    {
        if (strcmp(p, "banana") == 0)
        {
            return new Banana;
        }
        else if (strcmp(p, "apple") == 0)
        {
            return new Apple;
        }
        else
        {
            cout << "unsupport" << endl;
            return nullptr;
        }
    }
};
int main()
{
    Factory* f = new Factory;
    Fruit* fruit = nullptr;

    fruit = f-> Create("banana");
    fruit -> getFruit();
    delete fruit;

    fruit = f->Create("apple");
    fruit->getFruit();
    delete fruit;

    cout << "Hello World!\n";
    return 0;
}

工厂模式

  • 增加抽象工厂类,作为所有工厂类的基类
  • 工厂要生产新类的对象时,只需继承自抽象工厂类,而不必修改原有代码
  • 面向抽象类编程,实现了客户端、工厂、产品的解耦合
#include <iostream>
using namespace std;

class Fruit
{
public:
    virtual void getFruit() = 0;
};

class Banana: public Fruit
{
public:
    virtual void getFruit()
    {
        cout << "banana" << endl;
    }
};

class Apple: public Fruit
{
public:
    virtual void getFruit()
    {
        cout << "apple" << endl;
    };
};
class AbFactory
{
public:
    virtual Fruit *Create() = 0;
};

class BananaFactory: public AbFactory
{
public:
    Fruit* Create()
    {
        return new Banana;
    }
};

class AppleFactory : public AbFactory
{
public:
    Fruit* Create()
    {
        return new Apple;
    }
};

int main()
{
    AbFactory   * f = nullptr;
    Fruit       * fruit = nullptr;

    f = new BananaFactory;
    fruit = f->Create();
    fruit->getFruit();

    delete f;
    delete fruit;

    cout << "Hello World!\n";
    return 0;
}

抽象工厂

  • 相比工厂只能生产一个产品,抽象共产用于生产一个产品族
#include <iostream>
using namespace std;

class Fruit
{
public:
    virtual void getFruit() = 0;
};

class SouthBanana: public Fruit
{
public:
    virtual void getFruit()
    {
        cout << "south banana" << endl;
    }
};

class NorthBanana : public Fruit
{
public:
    virtual void getFruit()
    {
        cout << "north banana" << endl;
    }
};

class SouthApple : public Fruit
{
public:
    virtual void getFruit()
    {
        cout << "south apple" << endl;
    }
};

class NorthApple : public Fruit
{
public:
    virtual void getFruit()
    {
        cout << "north apple" << endl;
    }
};

class AbFactory
{
public:
    virtual Fruit* GetApple() = 0;
    virtual Fruit* GetBanana() = 0;
};

class SouthFactory: public AbFactory
{
public:
    Fruit* GetApple()
    {
        return new SouthApple;
    }
    Fruit* GetBanana()
    {
        return new SouthBanana;
    }
};

class NorthFactory : public AbFactory
{
public:
    Fruit* GetApple()
    {
        return new NorthApple;
    }
    Fruit* GetBanana()
    {
        return new NorthBanana;
    }
};

int main()
{
    AbFactory   * f = nullptr;
    Fruit       * fruit = nullptr;

    f = new SouthFactory;
    fruit = f->GetApple();
    fruit->getFruit();

    delete f;
    delete fruit;

    cout << "Hello World!\n";
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值