Skip to content

Instantly share code, notes, and snippets.

@FONQRI
Created January 2, 2018 10:13
Show Gist options
  • Save FONQRI/355f424827f9d63ef53bec988b151608 to your computer and use it in GitHub Desktop.
Save FONQRI/355f424827f9d63ef53bec988b151608 to your computer and use it in GitHub Desktop.
Composite Design Pattern in c++
#include <algorithm>
#include <iostream>
#include <list>
#include <memory>
#include <string>
using namespace std;
class DiagnosticInterface {
public:
virtual ~DiagnosticInterface();
virtual void runDiagnostics() = 0;
virtual string getDianosticsResult() = 0;
};
DiagnosticInterface::~DiagnosticInterface() {}
class DiagnosticCompositeInterface : public DiagnosticInterface {
public:
void runDiagnostics();
string getDianosticsResult();
void addChild(shared_ptr<DiagnosticInterface> child);
void removeChild(shared_ptr<DiagnosticInterface> child);
shared_ptr<DiagnosticInterface> getChild(size_t i);
protected:
list<shared_ptr<DiagnosticInterface>> mCompositeObjs;
};
void DiagnosticCompositeInterface::runDiagnostics()
{
for (auto it : mCompositeObjs) {
it->runDiagnostics();
}
}
string DiagnosticCompositeInterface::getDianosticsResult()
{
string ret = "";
for (auto it : mCompositeObjs) {
ret += it->getDianosticsResult() + "\n";
}
return ret;
}
void DiagnosticCompositeInterface::addChild(
shared_ptr<DiagnosticInterface> child)
{
mCompositeObjs.push_back(child);
}
void DiagnosticCompositeInterface::removeChild(
shared_ptr<DiagnosticInterface> child)
{
mCompositeObjs.remove_if([&child](shared_ptr<DiagnosticInterface> it) {
/*you should check if object is same and return a bool
i did'nt because operator == did not implemented for class
DiagnosticInterface yet
eg.
if == operator was implemented i could use this code :
if (*it == *child)
return true;
return false;
*/
return false;
});
}
shared_ptr<DiagnosticInterface> DiagnosticCompositeInterface::getChild(size_t i)
{
if (i > mCompositeObjs.size()) {
return nullptr;
}
size_t index = 0;
for (auto it : mCompositeObjs) {
index++;
if (index == i)
return it;
}
return nullptr;
}
class PhoneDiagnostics : public DiagnosticCompositeInterface {
public:
void runDiagnostics();
string getDianosticsResult();
};
void PhoneDiagnostics::runDiagnostics()
{
cout << "running PhoneDiagnostics..." << endl;
DiagnosticCompositeInterface::runDiagnostics();
}
string PhoneDiagnostics::getDianosticsResult()
{
string ret = "PhoneDiagnostics all tested Okay!\n";
ret += DiagnosticCompositeInterface::getDianosticsResult();
return ret;
}
class ScreenSaverDiagnostics : public DiagnosticInterface {
public:
void runDiagnostics();
string getDianosticsResult();
};
void ScreenSaverDiagnostics::runDiagnostics()
{
cout << "running ScreenSaverDiagnostics..." << endl;
}
string ScreenSaverDiagnostics::getDianosticsResult()
{
return "ScreenSaverDiagnostics all tested Okay!";
}
class HardwareDiagnostics : public DiagnosticCompositeInterface {
public:
void runDiagnostics();
string getDianosticsResult();
};
void HardwareDiagnostics::runDiagnostics()
{
cout << "running HardwareDiagnostics..." << endl;
DiagnosticCompositeInterface::runDiagnostics();
}
string HardwareDiagnostics::getDianosticsResult()
{
string ret = "HardwareDiagnostics all tested Okay!\n";
ret += DiagnosticCompositeInterface::getDianosticsResult();
return ret;
}
class NetworkDiagnostics : public DiagnosticCompositeInterface {
public:
void runDiagnostics();
string getDianosticsResult();
};
void NetworkDiagnostics::runDiagnostics()
{
cout << "running NetworkDiagnostics..." << endl;
DiagnosticCompositeInterface::runDiagnostics();
}
string NetworkDiagnostics::getDianosticsResult()
{
string ret = "NetworkDiagnostics all tested Okay!\n";
ret += DiagnosticCompositeInterface::getDianosticsResult();
return ret;
}
class TouchScreenDiagnostics : public DiagnosticInterface {
public:
void runDiagnostics();
string getDianosticsResult();
};
void TouchScreenDiagnostics::runDiagnostics()
{
cout << "running TouchScreenDiagnostics..." << endl;
}
string TouchScreenDiagnostics::getDianosticsResult()
{
return "TouchScreenDiagnostics all tested Okay!";
}
class OnOffKeyDiagnostics : public DiagnosticInterface {
public:
void runDiagnostics();
string getDianosticsResult();
};
void OnOffKeyDiagnostics::runDiagnostics()
{
cout << "running OnOffKeyDiagnostics..." << endl;
}
string OnOffKeyDiagnostics::getDianosticsResult()
{
return "OnOffKeyDiagnostics all tested Okay!";
}
class WifiDiagnostics : public DiagnosticInterface {
public:
void runDiagnostics();
string getDianosticsResult();
};
void WifiDiagnostics::runDiagnostics()
{
cout << "running WifiDiagnostics..." << endl;
}
string WifiDiagnostics::getDianosticsResult()
{
return "WifiDiagnostics all tested Okay!";
}
class ThreeGDiagnostics : public DiagnosticInterface {
public:
void runDiagnostics();
string getDianosticsResult();
};
void ThreeGDiagnostics::runDiagnostics()
{
cout << "running ThreeGDiagnostics..." << endl;
}
string ThreeGDiagnostics::getDianosticsResult()
{
return "ThreeGDiagnostics all tested Okay!";
}
int main()
{
unique_ptr<DiagnosticCompositeInterface> phoneDiagnostics(
new PhoneDiagnostics());
shared_ptr<DiagnosticInterface> screenSaverDiagnostics(
new ScreenSaverDiagnostics());
shared_ptr<DiagnosticCompositeInterface> hardwareDiagnostics(
new HardwareDiagnostics());
shared_ptr<DiagnosticCompositeInterface> networkDiagnostics(
new NetworkDiagnostics());
shared_ptr<DiagnosticInterface> touchScreenDiagnostics(
new TouchScreenDiagnostics());
shared_ptr<DiagnosticInterface> onOffKeyDiagnostics(
new OnOffKeyDiagnostics());
shared_ptr<DiagnosticInterface> wifiDiagnostics(new WifiDiagnostics());
shared_ptr<DiagnosticInterface> threeGDiagnostics(new ThreeGDiagnostics());
hardwareDiagnostics->addChild(touchScreenDiagnostics);
hardwareDiagnostics->addChild(onOffKeyDiagnostics);
networkDiagnostics->addChild(wifiDiagnostics);
networkDiagnostics->addChild(threeGDiagnostics);
phoneDiagnostics->addChild(screenSaverDiagnostics);
phoneDiagnostics->addChild(hardwareDiagnostics);
phoneDiagnostics->addChild(networkDiagnostics);
cout << "Run diagnostics on the phone!" << endl;
phoneDiagnostics->runDiagnostics();
cout << "****\nGet the diagnostics result\n"
<< phoneDiagnostics->getDianosticsResult() << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment