设计模式之:工厂模式(factory)

工厂模式factory

工厂模式其主旨在于将对象的创建过程抽取(抽象)出来,形成专门用于统一创建对象的类;
根据不同的创建要求,创建所需要的对象;

C++实现

#include<iostream>
using namespace std;

#define TANK 1
#define HOWITZER 2

class Weapon
{
public:
    Weapon() {}
    virtual ~Weapon() {}
    virtual void test() = 0;
};

class Tank : public Weapon
{
public:     
    Tank(std::string& name, int id):
        name_(name), id_(id){}
    ~Tank() {}

    void test() override {
        std::cout << "Tank: name[" << name_<< "], id[" << id_<<"], test ok!" << std::endl;
    }

private:
    std::string name_;
    int id_;
};

class Howitzer : public Weapon
{
public:     
    Howitzer(std::string& name, int id):
        name_(name), id_(id){}
    ~Howitzer() {}

    void test() override {
        std::cout << "Howitzer: name[" << name_<< "], id[" << id_<<"], test ok!" << std::endl;
    }

private:
    std::string name_;
    int id_;
};

class Factory
{
public:
    Factory(){}
    virtual ~Factory(){}
    virtual Weapon* produce(int type) = 0;
};

class WeaponFactory : public Factory
{
public:
    WeaponFactory(std::string& name): name_(name){
        std::cout << "WeaponFactory: " << name_ << std::endl;
    }
    ~WeaponFactory(){}
    Weapon* produce(int type) override {
        Weapon* temp = NULL;
        std::string name;
        switch(type) {
            case TANK: 
                name = "<M1A2 Abrams Main Battle Tank>";
                temp = new Tank(name, 1);
                break;
            case HOWITZER: 
                name = "<M777 Howitzer>";
                temp = new Howitzer(name, 2);
                break;
        }
        return temp;
    }
private:
    std::string name_;
};


int main()
{
    std::string name("7981Factory");
    Factory *factory = new WeaponFactory(name); 

    Weapon* weapon = factory->produce(TANK);
    weapon->test();
    delete weapon;
    weapon = NULL;

    weapon = factory->produce(HOWITZER);
    weapon->test();

    delete weapon;
    weapon = NULL;

    return 0;
}

C实现

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define TANK 1
#define HOWITZER 2

typedef struct _Tank
{
    char name_[64];
    int id_;
    void (*test)(struct _Tank*);
}Tank;

typedef struct _Howitzer
{
    char name_[64];
    int id_;
    void (*test)(struct _Howitzer*);
}Howitzer;

typedef struct _Factory
{
    void* (*produce)(int type);
}Factory;

Factory* CreateWeaponFactory()
{
    Factory* pf = malloc(sizeof(Factory));
    return pf;
}

void TankTest(Tank* pt)
{
    printf("Tank: name[%s], id[%d] test ok!\n", pt->name_, pt->id_);
}

void HowitzerTest(Howitzer* pt)
{
    printf("Howitzer: name[%s], id[%d] test ok!\n", pt->name_, pt->id_);
}

void* ProduceTank()
{
    Tank* tank = malloc(sizeof(Tank));
    memset(tank, sizeof(Tank), 0);
    strncpy(tank->name_, "<M1A2 Abrams Main Battle Tank>", 64);
    tank->id_ = 1;
    tank->test = TankTest;

    return tank;
}

void* ProduceHowitzer()
{
    Howitzer* how = malloc(sizeof(Howitzer));
    memset(how, sizeof(Howitzer), 0);
    strncpy(how->name_, "<M777 Howitzer>", 64);
    how->id_ = 2;
    how->test = HowitzerTest;

    return how;
}

void* produce(int type)
{
    switch(type) {
        case TANK:
            return ProduceTank();
        case HOWITZER:
            return ProduceHowitzer();
        default:
            return NULL;
    }
}

int main()
{
    Factory *pf = CreateWeaponFactory();
    pf->produce = produce;

    Tank* m1a2 = pf->produce(TANK);
    m1a2->test(m1a2);

    Howitzer* m777 = pf->produce(HOWITZER);
    m777->test(m777);
}
发布了134 篇原创文章 · 获赞 20 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/u011583798/article/details/82700456