Design Patterns: Factory mode (factory)

Factory model factory

The factory pattern summarized in that the process of creating the object extraction (abstract) it is formed specifically for the class object to create uniform;
according to create different requirements needed to create an object;

C ++ implementation

#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 implementation

#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);
}
Published 134 original articles · won praise 20 · views 60000 +

Guess you like

Origin blog.csdn.net/u011583798/article/details/82700456