1. 程式人生 > >設計模式之:工廠模式(factory)

設計模式之:工廠模式(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(); 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);
}