2

I have these C++ classes defined as follows:

class A
{
public:
    B *createB();
};

class B 
{
public:
    virtual void fun() = 0;
};
class B1 : B {/* ... */};
class B2 : B {/* ... */};

So basically B is an abstract class, B1 and B2 are concrete implementations of B, and A creates an instance of type B somewhere in its code. It's important to note that A is not a factory, A::createB is just an example.

I'd like to be able to pass a subclass of B during initialization of A so it is possible to create instances of the former by the latter as necessary during runtime. Example:

A *a1 = /* sorcery here */;
A *a2 = /* another magic code */;

a1->createB(); // getting B1
a2->createB(); // getting B2

What is the best way to achieve it? Is it possible without using templates?


Basing on responses I ended up with this. Thanks!

class B
{
public:
    virtual void fun() = 0;
    virtual B *clone() = 0;
};

class B1 : public B
{
public:
    virtual void fun()
    {
        std::cout << "B1" << std::endl;
    }
    virtual B *clone()
    {
        return new B1();
    }
};

class B2 : public B {/* analogous to B1 */};

class A
{
public:
    A(B *b) : b(b) {};

    B *createB()
    {
        return b->clone();
    }
private:
    B *b;
};

A(new B1()).createB()->fun(); // prints "B1"
A(new B2()).createB()->fun(); // prints "B2"
mariusz
  • 645
  • 6
  • 10

3 Answers3

4

Implement a clone() method in B.

Pass a B* to A when creating it. A will call B's clone() with that B* as a parameter.

For further information about cloning see questions Which situation will use clone in C++ and how to use it?, What's the best signature for clone() in C++? and How to write a clone method easily?, among others.

Community
  • 1
  • 1
Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
2

You can use the Prototype design pattern to achieve this. Pass A an instance of B1 or B2, and add a clone() member function to B, like this:

class B 
{
public:
    virtual void fun() = 0;
    virtual B* clone() = 0; // B1::clone returns new B1; B2::clone returns new B2
};

A stores the prototype instance of B passed in during initialization for later use. When it needs to create a new B later on, it calls clone() on the prototype to get an instance of the right class.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

Sounds like you want abstract factory.

You can see example here :

#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;
}
BЈовић
  • 62,405
  • 41
  • 173
  • 273