Skip to content

Instantly share code, notes, and snippets.

@pazdera
Created July 24, 2011 11:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save pazdera/1102546 to your computer and use it in GitHub Desktop.
Save pazdera/1102546 to your computer and use it in GitHub Desktop.
Example of `abstract factory' design pattern in C++
/*
* Example of `abstract factory' design pattern.
* Copyright (C) 2011 Radek Pazdera
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <string>
class Window
{
protected:
int width;
int height;
std::string toolkit;
std::string type;
Window(std::string usedToolkit, std::string windowType)
: toolkit(usedToolkit), type(windowType)
{}
public:
std::string getToolkit()
{
return toolkit;
}
std::string getType()
{
return type;
}
};
class GtkToolboxWindow : public Window
{
public:
GtkToolboxWindow()
: Window("Gtk", "ToolboxWindow")
{}
};
class GtkLayersWindow : public Window
{
public:
GtkLayersWindow()
: Window("Gtk", "LayersWindow")
{}
};
class GtkMainWindow : public Window
{
public:
GtkMainWindow()
: Window("Gtk", "MainWindow")
{}
};
class QtToolboxWindow : public Window
{
public:
QtToolboxWindow()
: Window("Qt", "ToolboxWindow")
{}
};
class QtLayersWindow : public Window
{
public:
QtLayersWindow()
: Window("Qt", "LayersWindow")
{}
};
class QtMainWindow : public Window
{
public:
QtMainWindow()
: Window("Qt", "MainWindow")
{}
};
/* This is the abstract factory. */
class UIFactory
{
public:
virtual Window* getToolboxWindow() = 0;
virtual Window* getLayersWindow() = 0;
virtual Window* getMainWindow() = 0;
};
/* Factory for Gtk toolkit */
class GtkUIFactory : public UIFactory
{
public:
Window* getToolboxWindow()
{
return new GtkToolboxWindow();
}
Window* getLayersWindow()
{
return new GtkLayersWindow();
}
Window* getMainWindow()
{
return new GtkMainWindow();
}
};
/* Factory for Qt toolkit */
class QtUIFactory : public UIFactory
{
public:
Window* getToolboxWindow()
{
return new QtToolboxWindow();
}
Window* getLayersWindow()
{
return new QtLayersWindow();
}
Window* getMainWindow()
{
return new QtMainWindow();
}
};
int main()
{
UIFactory* ui = 0;
/* Check what environment is running
and create appropriate factory. */
if (/* Gtk == */ true)
{
ui = new GtkUIFactory();
}
else
{
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;
}
@rajendrauppal
Copy link

Nice example!

@erfanhossainshoaib
Copy link

After Window* toolbox = ui->getToolboxWindow(); line
It calls return new QtToolboxWindow(); line.
It allocate memory and return which store in toolbox pointer in main function.
How can I delete this and who is responsible for deallocate the memory.
what is the delete procedure.
Is it create the memory leakage issue???

@MartyLake
Copy link

@erfanhossainshoaib : It looks like leaks to me. One way to solve this is to store these pointers in RAII auto pointers like this : std::unique_ptr<Window> toolbox(ui->getToolboxWindow(););

@Daparrag
Copy link

Daparrag commented Feb 4, 2018

Very interesting example tanks you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment