Click here to Skip to main content
Licence CPOL
First Posted 4 Sep 2006
Views 18,533
Bookmarked 12 times

How to Code like Professionals? Part 2: Abstract Factory

By Vahid Kazemi | 4 Sep 2006
Lessons on Design Patterns
5 votes, 35.7%
1
5 votes, 35.7%
2

3
1 vote, 7.1%
4
3 votes, 21.4%
5
1.65/5 - 14 votes
3 removed
μ 2.09, σa 2.81 [?]

Introduction

In the first article of this series, we talked about one of the basic Design Patterns called “Singleton” which can be used to solve a lot of problems effectively. Here we are going to talk about another popular Design Pattern called “Abstract Factory”.

Abstract Factory

Consider you want to build a program with different themes (for example XP and Vista theme). Each theme consists of some different widgets like window, button, checkbox, etc. When you select a theme, you want every widget that you create to follow that theme. A quick solution is to declare a global variable indicating the current theme and using switch/case everywhere you create a widget:

#define XP_THEME 1
#define VISTA_THEME 2

extern int g_SelectedTheme;

switch(g_SelectedTheme)
{
case XP_THEME:
    window = new XPWindow();
    break;

case VISTA_THEME:
    window = new VistaWindow();
    break;
}

When your code goes larger and you want to build multiple widgets in different parts of the code, creating new widgets turns to a nightmare.

This is where the story of the Abstract Factory begins. To solve the problem using this method, we need to create four types of classes:

  • Abstract Factory (ITheme)
  • Actual Factory (VistaTheme, XPTheme)
  • Abstract Product (IWindow, IButton)
  • Actual Product (VistaWindow, XPWindow, VistaButton, XPButton)

Here is how it looks like:

/*
 * Abstract Theme
 */

class IWindow
{
    //...
};

class IButton
{
    //...
};

class ITheme
{
public:
    virtual IWindow *CreateWindow() = 0;
    virtual IButton *CreateButton() = 0;
};

/*
 * XP Theme 
 */

class XPWindow : public IWindow
{
    //...
};

class XPButton : public IButton
{
    //...
};


class XPTheme : public ITheme
{
    IWindow *CreateWindow()
    {
        return new XPWindow();
    }

    IButton *CreateButton()
    {
        return new XPButton();
    }
};

/*
 * VistaTheme
 */

class VistaWindow : public IWindow
{
    //...
};

class VistaButton : public IButton
{
    //...
};

class VistaTheme : public ITheme
{
    IWindow *CreateWindow()
    {
        return new VistaWindow();
    }

    IButton *CreateButton()
    {
        return new VistaButton();
    }
};

And here is how you can use it:

ITheme *theme = NULL;

theme = new VistaTheme();

theme->CreateWindow();
theme->CreateButton();

Well, it looks much better. A good idea is to define Theme classes as Singleton because you don’t need to instantiate more than one theme at a time. OK, there are a lot of other implementations of Abstract Factory, just choose one that fits your needs. If you have comments or questions, let me know by writing to vkazemi {at} gmail.com and make sure to check my web site for updates: http://www.gameprogrammer.org.

Good luck!

History

  • 4th September, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Vahid Kazemi

Software Developer
OculusAI
Sweden Sweden

Member
I have got my BSc in Computer Science from SBU, and I am currently a master student of Systems, Controls and Robotics at KTH, University of Technology. I am currently doing research and development in Computer Vision area.
 
My homepage at KTH. I am currently looking for a PhD position.
 
GameProgrammer.org, My website containing tutorials about Direct3D and OpenGL and more.
 
"Quest of Persia", An adventure game which I've worked on as programmer.
 
contact: vkazemi [at] gmail.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionDid you choose the right sample? Pinmemberemilio_grv22:33 4 Sep '06  
AnswerRe: Did you choose the right sample? PinmemberVahid Kazemi13:15 5 Sep '06  
Questionmissing something? Pinmemberjrziviani7:31 4 Sep '06  
AnswerRe: missing something? PinmemberVahid Kazemi13:10 5 Sep '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120210.1 | Last Updated 4 Sep 2006
Article Copyright 2006 by Vahid Kazemi
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid