LN : Eden Polymorphic And OOP Design Pattern Abstract Factory

  • Appreciation to our TA, +7, who designed this task.

Client.cpp

#include <iostream>
#include <string>
#include "Factory.hpp"
#include "Product.hpp"
int main() {
    UIFactory* ui = 0;
    int choose;
    std::cin >> choose;
    switch (choose) {
        case 0:
            ui = new MFCUIFactory();
            break;
        case 1:
            ui = new GtkUIFactory();
            break;
        case 2:
        default:
            ui = new QtUIFactory();
    }
    /* Use the factory to build interface. */
    Window* toolbox = ui->getToolboxWindow();
    Window* layers = ui->getLayersWindow();
    Window* main = ui->getMainWindow();
    /* See what have we recieved. */
    std::cout << toolbox->getToolkit() << ":" << toolbox->getType() << std::endl;
    std::cout << layers->getToolkit() << ":" << layers->getType() << std::endl;
    std::cout << main->getToolkit() << ":" << main->getType() << std::endl;
    ui->deleteWindow(toolbox);
    ui->deleteWindow(layers);
    ui->deleteWindow(main);
    delete ui;
}

Product.hpp

#ifndef PRODUCT_HPP
#define PRODUCT_HPP
#include <iostream>
#include <string>
using namespace std;
class Window {
public:
    virtual string getToolkit() = 0;
    virtual string getType() = 0;
    string kit;
};
class ToolboxWindow:public Window {
public:
    ToolboxWindow(string a) {
        kit = a;
    }
    virtual string getToolkit() {
        return kit;
    }
    virtual string getType() {
        return "ToolboxWindow";
    }
};
class LayersWindow:public Window {
public:
    LayersWindow(string a) {
        kit = a;
    }
    virtual string getToolkit() {
        return kit;
    }
    virtual string getType() {
        return "LayersWindow";
    }
};
class MainWindow:public Window {
public:
    MainWindow(string a) {
        kit = a;
    }
    virtual string getToolkit() {
        return kit;
    }
    virtual string getType() {
        return "MainWindow";
    }
};
#endif

Factory.hpp

#ifndef FACTORY_HPP
#define FACTORY_HPP
#include <iostream>
#include <string>
#include "Product.hpp"
using namespace std;
class UIFactory {
public:
    virtual Window* getToolboxWindow() = 0;
    virtual Window* getLayersWindow() = 0;
    virtual Window* getMainWindow() = 0;
    virtual void deleteWindow(Window* a) = 0;
};
class MFCUIFactory:public UIFactory {
public:
    MFCUIFactory() {}
    virtual Window* getToolboxWindow() {
        return new ToolboxWindow("MFC");
    }
    virtual Window* getLayersWindow() {
        return new LayersWindow("MFC");
    }
    virtual Window* getMainWindow() {
        return new MainWindow("MFC");
    }
    virtual void deleteWindow(Window* a) {
        delete a;
    }
};
class GtkUIFactory:public UIFactory {
public :
    GtkUIFactory() {}
    virtual Window* getToolboxWindow() {
        return new ToolboxWindow("Gtk");
    }
    virtual Window* getLayersWindow() {
        return new LayersWindow("Gtk");
    }
    virtual Window* getMainWindow() {
        return new MainWindow("Gtk");
    }
    virtual void deleteWindow(Window* a) {
        delete a;
    }
};
class QtUIFactory:public UIFactory {
public:
    QtUIFactory() {}
    virtual Window* getToolboxWindow() {
        return new ToolboxWindow("Qt");
    }
    virtual Window* getLayersWindow() {
        return new LayersWindow("Qt");
    }
    virtual Window* getMainWindow() {
        return new MainWindow("Qt");
    }
    virtual void deleteWindow(Window* a) {
        delete a;
    }
};
#endif

你可能感兴趣的:(LN : Eden Polymorphic And OOP Design Pattern Abstract Factory)