Monday, May 5, 2014

Memento Pattern

/*
        Without violating encapsulation, capture and externalize an object's internal state
        so that the object can be restored to this state later.
*/

  1 #include<iostream>
  2
  3 using namespace std;
  4
  5 class EmployeeDataMemento{
  6         public:
  7                 int id;
  8                 string name;
  9                 EmployeeDataMemento(){}
 10                 EmployeeDataMemento(int number, string employee_name)
 11                 {
 12                         id = number;
 13                         name = employee_name;
 14                 }
 15 };
 16
 17 class EmployeeData{
 18         int id;
 19         string name;
 20         EmployeeDataMemento *ed;
 21         public:
 22                 EmployeeData(int number, string emp_name)
 23                 {
 24                         id = number;
 25                         name  = emp_name;
 26                         ed = new EmployeeDataMemento(id,name);
 27                 }
 28                 void displayEmployeeData()
 29                 {
 30                         cout<<"Emp Id: "<<id<<endl;
 31                         cout<<"employee Name: "<<name<<endl;
 32                 }
 33                 void update(int number, string emp_name)
 34                 {
 35                         id = number;
 36                         name = emp_name;
 37                 }
 38                 void undo(char ans)
 39                 {
 40                         if(ans == 'Y' || ans == 'y')
 41                         {
 42                                 id = ed->id;
 43                                 name = ed->name;
 44                         }
 45                         else
 46                                 ed = new EmployeeDataMemento(id,name);
 47                 }
 48 };
 49
 50
 51 int main()
 52 {
 53         EmployeeData ed(100, "Dhananjay Kumar");
 54         ed.displayEmployeeData();
 55         int id;
 56         string name;
 57         cout<<"Enter New Id = ";
 58         cin>>id;
 59         cout<<"Enter New Name = ";
 60         cin>>name;
 61         cout<<"Update the data.."<<endl;
 62         ed.update(id, name);
 63         ed.displayEmployeeData();
 64         cout<<"Wants to Undo updates..? Y/N: ";
 65         char ans;
 66         cin>>ans;
 67         ed.undo(ans);
 68         ed.displayEmployeeData();
 69 }

Observer Pattern

  1 #include<iostream>
  2 #include<vector>
  3 using namespace std;
  4
  5 class Notification{
  6         public:
  7                 virtual void Notify()=0;
  8 };
  9
 10 class EmailNotification: public Notification{
 11         public:
 12                 void Notify(){cout<<"Email Notification sent to.....@yahoo.com"<<endl;}
 13 };
 14
 15 class SMSNotification: public Notification{
 16         public:
 17                 void Notify(){cout<<"SMS Notification sent to.... 999999990 "<<endl;}
 18
 19 };
 20
 21 class Notifier{
 22         vector<Notification *> notifications;
 23         public:
 24         void addNotifications(Notification *notification)
 25         {
 26                 notifications.push_back(notification);
 27         }
 28         void NotifyAll()
 29         {
 30                 for(int i = 0; i < notifications.size(); i++)
 31                         notifications[i]->Notify();
 32         }
 33 };
 34
 35 int main()
 36 {
 37         cout<<"Do you want to send notifications....? Y/N:  ";
 38         Notifier *notify = new Notifier();
 39         char response;
 40         cin>> response;
 41
 42         if(response == 'Y' || response == 'y')
 43         {
 44                 EmailNotification *emailnotification = new EmailNotification();
 45                 SMSNotification *smsnotification = new SMSNotification();
 46                 notify->addNotifications(smsnotification);
 47                 notify->addNotifications(emailnotification);
 48                 notify->NotifyAll();
 49         }
 50         else
 51         {
 52                 cout<<"No Notification Sent. Bye!!"<<endl;
 53         }
 54 }

Sunday, May 4, 2014

Builder Design Pattern

/* 
Builder pattern helps us to separate the construction of a complex object from its representation so that     the same construction process can create different representations. 
*/

1 #include <iostream>
  2 using namespace std;
  3
  4 class Report{
  5         char *type;
  6         char *header;
  7         char *footer;
  8         public:
  9
 10         void set_type(char *report_type)
 11         {
 12                 type = report_type;
 13         }
 14         void set_header(char *header_text)
 15         {
 16                 header = header_text;
 17         }
 18         void set_footer(char *footer_text)
 19         {
 20                 footer = footer_text;
 21         }
 22         void display_report()
 23         {
 24                 cout<<"========================================="<<endl;
 25                 cout<<"Type = "<<type<<endl;
 26                 cout<<"Header = "<<header<<endl;
 27                 cout<<"Footer = "<<footer<<endl;
 28         }
 29 };
 30
 31 class ReportBuilder{
 32         protected:
 33                 Report *report;
 34         public:
 35                 virtual void set_type() = 0;
 36                 virtual void set_header() = 0;
 37                 virtual void set_footer() = 0;
 38                 virtual void create_report() { report = new Report(); }
 39                 virtual Report* get_report(){return report;}
 40                 virtual ~ReportBuilder() {delete report;}
 41 };
 42
 43 class PDFReportBuilder: public ReportBuilder{
 44         public:
 45                 virtual void set_type() { report->set_type("PDF"); }
 46                 virtual void set_header(){report->set_header("Dhananjay PDF Ltd.");}
 47                 virtual void set_footer(){report->set_footer("Thanks for this PDF file.");}
 48
 49 };
 50
 51 class XLSReportBuilder: public ReportBuilder{
 52         public:
 53                 virtual void set_type() { report->set_type("Excel"); }
 54                 virtual void set_header(){report->set_header("Dhananjay Excel Ltd.");}
 55                 virtual void set_footer(){report->set_footer("Thanks for this XLS file.");}
 56 };
 57
 58 class ReportDirector{
 59         public:
 60                 Report* make_report(ReportBuilder *rb)
 61                 {
 62                         rb->create_report();
 63                         rb->set_type();
 64                         rb->set_header();
 65                         rb->set_footer();
 66                         return rb->get_report();
 67                 }
 68 };
 69
 70
 71 int main()
 72 {
 73         Report *report;
 74         ReportDirector *rd = new ReportDirector();
 75         PDFReportBuilder *prb = new PDFReportBuilder();
 76         report = rd->make_report(prb);
 77         report->display_report();
 78
 79         XLSReportBuilder *xrb = new XLSReportBuilder();
 80         report = rd->make_report(xrb);
 81         report->display_report();
 82
 83 }

Saturday, May 3, 2014

Abstract Factory Pattern

  1 #include <iostream>
  2
  3 using namespace std;
  4
  5 class Mobile{
  6         public:
  7                 virtual void getName() { }
  8                 virtual void getScreen() { }
  9                 virtual void getBattery() { }
 10 };
 11
 12 class NokiaMobile: public Mobile{
 13         public:
 14                 virtual void getName()
 15                 {
 16                         cout<<"This is Nokia Mobile.."<<endl;
 17                 }
 18                 virtual void getScreen()
 19                 {
 20                         cout<<"This is Nokia Mobile screen.."<<endl;
 21                 }
 22                 virtual void getBattery()
 23                 {
 24                         cout<<"This is Nokia Mobile battery.."<<endl;
 25                 }
 26 };
 27
 28
 29 class SamsungMobile: public Mobile{
 30         public:
 31                 void display()
 32                 {
 33                         cout<<"This is Samsung Mobile.."<<endl;
 34                 }
 35                 virtual void getScreen()
 36                 {
 37                         cout<<"This is Samsung Mobile screen.."<<endl;
 38                 }
 39                 virtual void getBattery()
 40                 {
 41                         cout<<"This is Samsung Mobile battery.."<<endl;
 42                 }
 43 };
 44
 45 class Charger{
 46         public:
 47                 virtual void getName(){}
 48
 49 };
 50 class NokiaCharger: public Charger{
 51         public:
 52                 virtual void getName()
 53                 {
 54                         cerr<<"This is Nokia Charger.."<<endl;
 55                 }
 56
 57 };
 58
 59 class SamsungCharger: public Charger{
 60         public:
 61                 virtual void getName()
 62                 {
 63                         cerr<<"This is Samsung Charger.."<<endl;
 64                 }
 65
 66 };
 67
 68
 69 class MobileFactory{
 70         public:
 71                 virtual Mobile* getMobile(){}
 72                 virtual Charger* getCharger(){}
 73 };
 74
 75 class NokiaMobileFactory: public MobileFactory{
 76         public:
 77                 virtual Mobile* getMobile()
 78                 {
 79                         return new NokiaMobile();
 80                 }
 81                 virtual Charger* getCharger()
 82                 {
 83                         return new NokiaCharger();
 84                 }
 85 };
 86
 87
 88 class SamsungMobileFactory : public MobileFactory{
 89         public:
 90                 virtual Mobile* getMobile()
 91                 {
 92                         return new SamsungMobile();
 93                 }
 94                 virtual Charger* getCharger()
 95                 {
 96                         return new SamsungCharger();
 97                 }
 98 };
 99
100
101 class FactoryCreator{
102         public:
103                 MobileFactory* createFactory(int order)
104                 {
105                         if(order == 1)
106                                 return new NokiaMobileFactory();
107                         else
108                                 return new SamsungMobileFactory();
109                 }
110 };
111
112
113 int main()
114 {
115         int orderMobile;
116         cout<<"1. Nokia "<<endl;
117         cout<<"2. Samsung "<<endl;
118         cin>>orderMobile;
119         FactoryCreator *fc = new FactoryCreator();
120         fc->createFactory(orderMobile)->getMobile()->getName();
121         fc->createFactory(orderMobile)->getMobile()->getScreen();
122         fc->createFactory(orderMobile)->getMobile()->getBattery();
123         fc->createFactory(orderMobile)->getCharger()->getName();
124         delete fc;
125
126 }

Command Design Pattern

// Implementing Command Design pattern. This is behavioral pattern which allows
// to encapsulate request as an object.

#include <iostream>

using namespace std;

class FileCommand{
        public:
                virtual void execute(){}
};

class FileOpen : public FileCommand{
        public:
                void execute()
                {
                        cout<<"File Opened.."<<endl;
                }
};

class FileClose : public FileCommand{
        public:
                void execute()
                {
                        cout<<"File Closed.."<<endl;
                }

};
//Invoker Class
class FileCommandInvoker{
        FileCommand* array[2];
        public:
                FileCommandInvoker()
                {
                        array[0] = new FileOpen();
                        array[1] = new FileClose();

                }
                FileCommand* getCommand(int command)
                {
                        FileCommand *fc = 0;
                        if(command == 1)
                                fc = array[0];
                        else if(command == 2)
                                fc = array[1];

                        return fc;
                }
};

//Client Code
int main()
{
        int command;
        cout<<"1. File Open  "<<endl;
        cout<<"2. File Close "<<endl;
        cout<<"Enter 1-2 : "<<endl;
        cin>>command;
        FileCommandInvoker *fci = new FileCommandInvoker();
        FileCommand *fc = fci->getCommand(command);
        fc->execute();
        return 0;
}