• 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
  • 相关阅读:
    如何使用Java计算货币/钱~(How to calculate monetary values in Java)
    BigDecimal类
    状态码定义
    常见服务器返回状态码(Status Codes)
    2020-3-26学习地图
    ReentrantLock类
    HashSet类
    Vector类
    课程总结
    第十四周课程总结&实验报告
  • 原文地址:https://www.cnblogs.com/renleimlj/p/5426090.html
Copyright © 2020-2023  润新知