Monday, April 1, 2013

Composite Pattern

  • Composite pattern is useful when you want to treat a group of objects in the same manner as a single instance of the object. The objects grouped, themselves could be in composite manner in a tree fashion.

  • The primitive and compound objects are treated uniformly.

  • A component interface is defined and both the leaf as well as composite objects implement it in uniform fashion.

  • Below example shows an implementation with a phone diagnostics composite pattern. A composite object is formed by combining all the phone related diagnostics objects in a tree manner. Then a diagnostic run on the composite object, which in turn reciprocates the request to all the diagnostics objects in a tree manner.

Sample implementation of composite pattern in C++


#include <iostream>
#include <string>
#include <list>

using namespace std;

class DiagnosticInterface {
public:
    virtual void runDiagnostics() = 0;
    virtual string getDianosticsResult() = 0;
};

class DiagnosticCompositeInterface : public DiagnosticInterface  {
public:
    void runDiagnostics() {
        for (list<DiagnosticInterface*>::iterator it = mCompositeObjs.begin(); it != mCompositeObjs.end(); ++it )
            (*it)->runDiagnostics();
    }
    string getDianosticsResult() {
        string ret = "";
        for (list<DiagnosticInterface*>::iterator it = mCompositeObjs.begin(); it != mCompositeObjs.end(); ++it )
            ret += (*it)->getDianosticsResult() + "\n";
        return ret;
    }
    void addChild(DiagnosticInterface *child) {
        mCompositeObjs.push_back(child);
    }
    void removeChild(DiagnosticInterface *child) {
        // find and remove the child. not implemented yet.
    }
    DiagnosticInterface* getChild(int i) {
        // get the child at i. Not implemented yet.
    }
protected:
    list<DiagnosticInterface*> mCompositeObjs;
};

class PhoneDiagnostics : public DiagnosticCompositeInterface {
public:
    void runDiagnostics() {
        cout << "running PhoneDiagnostics..." << endl;
        DiagnosticCompositeInterface::runDiagnostics();
    }
    string getDianosticsResult() {
        string ret = "PhoneDiagnostics all tested Okay!\n";
        ret += DiagnosticCompositeInterface::getDianosticsResult();
        return ret;
    }
};

class ScreenSaverDiagnostics : public DiagnosticInterface {
public:
    void runDiagnostics() {
        cout << "running ScreenSaverDiagnostics..." << endl;
    }
    string getDianosticsResult() {
        return "ScreenSaverDiagnostics all tested Okay!";
    }
};

class HardwareDiagnostics : public DiagnosticCompositeInterface {
public:
    void runDiagnostics() {
        cout << "running HardwareDiagnostics..." << endl;
        DiagnosticCompositeInterface::runDiagnostics();
    }
    string getDianosticsResult() {
        string ret = "HardwareDiagnostics all tested Okay!\n";
        ret += DiagnosticCompositeInterface::getDianosticsResult();
        return ret;
    }
};

class NetworkDiagnostics : public DiagnosticCompositeInterface {
public:
    void runDiagnostics() {
        cout << "running NetworkDiagnostics..." << endl;
        DiagnosticCompositeInterface::runDiagnostics();
    }

    string getDianosticsResult() {
        string ret = "NetworkDiagnostics all tested Okay!\n";
        ret += DiagnosticCompositeInterface::getDianosticsResult();
        return ret;
    }
};

class TouchScreenDiagnostics : public DiagnosticInterface {
public:
    void runDiagnostics() {
        cout << "running TouchScreenDiagnostics..." << endl;
    }
    string getDianosticsResult() {
        return "TouchScreenDiagnostics all tested Okay!";
    }
};

class OnOffKeyDiagnostics : public DiagnosticInterface {
public:
    void runDiagnostics() {
        cout << "running OnOffKeyDiagnostics..." << endl;
    }
    string getDianosticsResult() {
        return "OnOffKeyDiagnostics all tested Okay!";
    }
};

class WifiDiagnostics : public DiagnosticInterface {
public:
    void runDiagnostics() {
        cout << "running WifiDiagnostics..." << endl;
    }
    string getDianosticsResult() {
        return "WifiDiagnostics all tested Okay!";
    }
};

class ThreeGDiagnostics : public DiagnosticInterface {
public:
    void runDiagnostics() {
        cout << "running ThreeGDiagnostics..." << endl;
    }
    string getDianosticsResult() {
        return "ThreeGDiagnostics all tested Okay!";
    }
};

int main()
{
    DiagnosticCompositeInterface *phoneDiagnostics = new PhoneDiagnostics();

    DiagnosticInterface *screenSaverDiagnostics = new ScreenSaverDiagnostics();
    DiagnosticCompositeInterface *hardwareDiagnostics = new HardwareDiagnostics();
    DiagnosticCompositeInterface *networkDiagnostics = new NetworkDiagnostics();

    DiagnosticInterface *touchScreenDiagnostics = new TouchScreenDiagnostics();
    DiagnosticInterface *onOffKeyDiagnostics = new OnOffKeyDiagnostics();

    DiagnosticInterface *wifiDiagnostics = new WifiDiagnostics();
    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;
}

Output:-
Run diagnostics on the phone! 
running PhoneDiagnostics... 
running ScreenSaverDiagnostics... 
running HardwareDiagnostics... 
running TouchScreenDiagnostics... 
running OnOffKeyDiagnostics... 
running NetworkDiagnostics... 
running WifiDiagnostics... 
running ThreeGDiagnostics... 
**** 
Get the diagnostics result 
PhoneDiagnostics all tested Okay! 
ScreenSaverDiagnostics all tested Okay! 
HardwareDiagnostics all tested Okay! 
TouchScreenDiagnostics all tested Okay! 
OnOffKeyDiagnostics all tested Okay! 
 
NetworkDiagnostics all tested Okay! 
WifiDiagnostics all tested Okay! 
ThreeGDiagnostics all tested Okay!

1 comment :

  1. If you are actually rich, it is going to take each luck in addition to proficiency to get down it gambling den popular. In order the home to get your individual casino when you previously had such as, receving your own collection happens to be a good option. This particular dazzling sit back and watch will be the most suitable topic involving connection in any sport, featuring red and ebony style, decorations emerge black colored coating which often embeds couple of card account models, 360 french fries during 5 shades plus your five dices, virtually all together with the glamorous feel of timepieces replica cartier watches uk. As i was while using the rather sufferer store assistant relating to basic steps or so minutes, investigating every fine detail. Just what had As i acquire? Making the price, not a single thing truly i think. In order to better the particular keep an eye on had been for you to exchange out there typically the quartz circulation that have an automatic just one, ; however , may well in no way be considered a affordable follow. The case, good layout, not to mention performance merit an automatic exercise. When i discovered though, which can be from the works out! Rubber is really a superior materials solution for your tie since the device helps you have a pleasant, protected match which will avoids the actual follow via getting around an issue provided your sizing and even significant material option replica breitling uk. However, enjoy lovers figure this out odd simple fact as the principle will probably be difficult to understand that will all others wax replica watches. However, will not be at this time there area for that correct model to help you work with crews of people which include learners on the the school or simply some people who are executing things like this particular which includes a rational association returning to true horology? All the watch sector can be each benefited through not to mention scheduled backside by just its concentrate on mechanical-only tools along with tiny deal with nearly anything digital camera. Certainly no strong idea practice must notice that this view is really a testament in the strength in inertia. What is maybe the a large number of enjoyable element belonging to the watch is normally her capacity to balance once more when dealing with force. The machine is related to the gyroscope in this this enjoy uses her heart in the form of central balance issue. As soon as reasonable on a point of the dice, that video tutorial displays ways to make an effort to affect the item out from destination that is why definitely will refrain from the actual trigger along with go back to her good spot. Beyond just the result rims and also engines, a fabulous difficult formula that operations advice instantly is normally why is typically the model possible. Definitely, we have a giant and even encourage area for your old classic component pieces, except for each and every brand name could follow the same course when many others. Watch firms searching to focus on advanced layout plus the success about modern-day thinkers might possibly enjoy assisting ventures want some of those.

    ReplyDelete

Contact Form

Name

Email *

Message *

Back to Top