An abstract design pattern is a creational design pattern that which means it is solely used for creating an object as efficient as a possible way
The below C++ contains an abstract class chocolate factory through which two concrete classes are inherited such as 50\$chocolatefactory and 100\$chocolatefactory
You can switch 50\$chocolatefactory and 100\$chocolate through client.cpp
Assume the main function as a client code
Chocolate.cpp
#include <iostream>
using namespace std;
class Peanut_stuff_chocolate {
protected:
string name;
int Milk_content;
public:
Peanut_stuff_chocolate(string n, int Milk_content):name(n),Milk_content(Milk_content){};
string getName(){return name;}
int getMilk_content(){return Milk_content;}
};
class Fifty$_Peanut_stuff_chocolate : public Peanut_stuff_chocolate{
public:
Fifty$_Peanut_stuff_chocolate():Peanut_stuff_chocolate("50$_chocolate",25) {}
};
class hundred$_Peanut_stuff_chocolate : public Peanut_stuff_chocolate{
public:
hundred$_Peanut_stuff_chocolate():Peanut_stuff_chocolate("100$_chocolate",50) {}
};
class Volume {
protected:
string name;
int Weight;
public:
Volume(string n, int Weight):name(n),Weight(Weight) {}
string getName(){return name;}
int getWeight() {return Weight;}
};
class Fifty$_Volume : public Volume{
public:
Fifty$_Volume():Volume("Fifty$_Volume",225) {}
};
class hundred$_Volume : public Volume{
public:
hundred$_Volume():Volume("hundred$_Volume",500) {}
};
class Chocolate {
protected:
string name;
Peanut_stuff_chocolate *peanut_stuff_chocolate;
Volume *volume;
public:
Chocolate(string n):name(n) {}
void setPeanut_stuff_chocolate(Peanut_stuff_chocolate* p) { peanut_stuff_chocolate = p;}
void setVolume(Volume* v) { volume = v;}
void printDetails(){
cout << endl << "chocolate: " << name << endl;
cout << "Peanut_stuff_chocolate: " << peanut_stuff_chocolate->getName() << " Milk_content: " << peanut_stuff_chocolate->getMilk_content() << endl;
cout << "Volume: " << volume->getName() << " Weight: " << volume->getWeight() << endl << endl;
}
};
ChocolateFactory.cpp
#include "Chocolate.cpp"
class Chocolatefactory {
private:
Chocolate *chocolate;
protected:
virtual Peanut_stuff_chocolate * buildPeanut_stuff_chocolate() = 0;
virtual Volume * buildVolume() = 0;
// much more product's parts could come here...
public:
virtual Chocolate* buildWholeChocolate() = 0;
};
class Fifty$_Chocolatefactory : public Chocolatefactory{
Peanut_stuff_chocolate * buildPeanut_stuff_chocolate() {
return new Fifty$_Peanut_stuff_chocolate();
}
Volume * buildVolume() {
return new Fifty$_Volume();
}
Chocolate * buildWholeChocolate() {
Chocolate *chocolate = new Chocolate("50$_Chocolate");
chocolate->setPeanut_stuff_chocolate(buildPeanut_stuff_chocolate());
chocolate->setVolume(buildVolume());
return chocolate;
}
};
class hundred$_Chocolatefactory : public Chocolatefactory {
Peanut_stuff_chocolate * buildPeanut_stuff_chocolate() {
return new hundred$_Peanut_stuff_chocolate();
}
Volume * buildVolume() {
return new hundred$_Volume();
}
Chocolate * buildWholeChocolate() {
Chocolate *chocolate = new Chocolate("100$_chocolate");
chocolate->setPeanut_stuff_chocolate(buildPeanut_stuff_chocolate());
chocolate->setVolume(buildVolume());
return chocolate;
}
};
Client.cpp
#include "ChocolateFactory.cpp"
//#define FIFTY$_CHOCOLATE 1
#define HUNDRED$_CHOCOLATE 1
int main() {
#ifdef FIFTY$_CHOCOLATE
Chocolatefactory* factory = new Fifty$_Chocolatefactory;
#elif HUNDRED$_CHOCOLATE
Chocolatefactory* factory = new hundred$_Chocolatefactory;
#endif
Chocolate *chocolate = factory->buildWholeChocolate();
chocolate->printDetails();
return 0;
}
O/p with 50$ ChocolateFactory
chocolate:50$_chocolate
peanut_stuff_chocolate:50$_chocolate milk_content:25
Volume:50$ chocolate_volume weight:255
O/P with 100$ ChocolateFactory
Chocolate: 100$_chocolate
Peanut_stuff_chocolate:100$_chocolate milk_content:50
Volume:100$chocolate_volume weight:500
Leave a Reply