/*
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 };
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 }
No comments:
Post a Comment