Thursday, 18 June 2015

Abstract factories in C++ with example

Abstract factories in Cpp with example

The Abstract Factory pattern looks like the factory objects we have seen previously, with not one but several factory methods.
Each of the factory methods creates a different kind of objects.

The idea is that at the point of creation of the factory object, you decide how all the objects created by that factory will be used.

Here is an example suppose you are creating a general-purpose gaming environment and you want to be able to support different types of games. Here is how it might look using an abstract factory:

#include "stdafx.h"

#include <iostream>

#include <string>

#include <exception>

#include <vector>

#include <map>

using namespace std;

class MyObstruction

{

public:

virtual void action () = 0;

};

class MyClass

{

public:

virtual void interactWith (MyObstruction*) = 0;

};

class Test1: public MyClass

{

virtual void interactWith (MyObstruction* ob)

{

cout << "Test1 has encountered a ";

ob->action();

}

};

class Test2: public MyClass

{

virtual void interactWith(MyObstruction* ob) {

cout << "Test2 now battles against a ";

ob->action();

}

};

class Test3: public MyObstruction {

public:

void action() { cout << "Test3\n"; }

};

class OcObstruction: public MyObstruction {

public:

void action() { cout << "OcObstruction\n"; }

};

// The abstract factory:

class FactoryClass {

public:

virtual MyClass* makePlayer() = 0;

virtual MyObstruction* makeObstacle() = 0;

};

// Concrete factories:

class Test5 : public FactoryClass

{

public:

virtual MyClass* makePlayer ()

{

return new Test1;

}

virtual MyObstruction* makeObstacle() {

return new Test3;

}

};

class Test4 : public FactoryClass {

public:

virtual MyClass* makePlayer() {

return new Test2;

}

virtual MyObstruction* makeObstacle ()

{

return new OcObstruction;

}

};



class EnvClass

{

FactoryClass* gef;

MyClass* p;

MyObstruction* ob;

public:

EnvClass (FactoryClass* factory) : gef (factory), p(factory->makePlayer()), ob (factory->makeObstacle())

{}

void play()

{

p->interactWith(ob);

}

~EnvClass ()

{

  delete p;

  delete ob;

  delete gef;

}

};

int _tmain (int argc, _TCHAR* argv[])

{

  EnvClass g1 (new Test5),

  g2 (new Test4);

  g1.play ();

  g2.play ();

  getchar ();

 return 0;

}


In this environment, MyClass objects interact with MyObstruction objects, but there are different types of players and obstacles depending on what kind of game you’re playing. You determine the kind of game by choosing a particular FactoryClass, and then the EnvClass controls the setup and play of the game. In this example, the setup and play is very simple, but those activities can determine much of the game’s outcome. Here, EnvClass is not designed to be inherited, although it could very possibly make sense to do that. This also contains examples of Double Dispatching and the Factory Method, both of which will be explained later. The output of  the above class is
Test1 has encountered a Test3

Test2 now battles against a OcObstruction



No comments: