Welcome to the { mindfrost82.com } forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

Go Back   { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > C++

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-06-2008, 03:24 PM
Pallav singh
 
Posts: n/a
Decorator design pattern ( C++ )

#include <iostream>

using namespace std;

/* Component (interface) */
class Widget {

public:
virtual void draw() = 0;
virtual ~Widget() {}

};

/* ConcreteComponent */
class TextField : public Widget {

private:
int width, height;

public:
TextField( int w, int h ){
width = w;
height = h;
}
void draw() {
cout << "TextField: " << width << ", " << height << '\n';
}

};

/* Decorator (interface) */
class Decorator : public Widget {

private:
Widget* wid; // reference to Widget
public:
Decorator( Widget* w ) {
wid = w;
}
void draw() {
wid->draw();
}

~Decorator() {
delete wid;
}

};

/* ConcreteDecoratorA */
class BorderDecorator : public Decorator {

public:
BorderDecorator( Widget* w ) : Decorator( w ) { }
void draw() {
Decorator::draw();
cout << " BorderDecorator" << '\n';
}

};

/* ConcreteDecoratorB */
class ScrollDecorator : public Decorator {
public:
ScrollDecorator( Widget* w ) : Decorator( w ) { }
void draw() {
Decorator::draw();
cout << " ScrollDecorator" << '\n';
}

};

int main( void ) {

Widget* aWidget = new BorderDecorator( new ScrollDecorator( new
TextField( 80, 24 )));

aWidget->draw();

delete aWidget;

}

+++++++++++++++++++++++++++++++++++++++++++
How does interface property changes here ?

it will call draw( ) function in following order
1. TextField
2. ScrollDecorator
3. BorderDecorator

Is it Following RAII( Resource Acquisition is Order of
initialization ) principle of C++ ?

How are virtual function helping it ?

++++++++++++++++++++++++++++++++++++++++++

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
  #2 (permalink)  
Old 08-06-2008, 11:43 PM
Ulrich Eckhardt
 
Posts: n/a
Re: Decorator design pattern ( C++ )

Pallav singh wrote:
[code stripped to the minimum]
> class Widget {
> virtual void draw() = 0;
> };
> class TextField : public Widget { ... };
>
> class Decorator : public Widget {
> public:
> Decorator( Widget* w ): wid(w) {}
> void draw() {
> wid->draw();
> }
> };
>
> class BorderDecorator : public Decorator { ... };
>
> int main( void ) {
> Widget* aWidget = new BorderDecorator( new TextField( 80, 24 ));
> aWidget->draw();
> }


> How does interface property changes here ?


Huh? I'm not sure what you mean here. What is the 'interface property'? And
once you define that and 'here', it should be trivial to define whether it
changes.

> Is it Following RAII( Resource Acquisition is Order of
> initialization ) principle of C++ ?


Sorry, but you got that abbreviation wrong, there is no 'order' in RAII.
Anyway, typically RAII is used in the meaning that the lifetime of a
resource or some other operation is bound to the scope of an object, using
constructor and destructor. Which constructors and destructors of automatic
objects are called in your example?

> How are virtual function helping it ?


Just implement 'it' (whatever exactly 'it' is...) without them as an example
and you will have hands-on experience.

Note: I've been intentionally vague in some answers because your questions
smell like homework, and while I don't mind helping with homework, you
don't seem to show an effort in solving it yourself and I'm not supporting
this perceived lazy attitude.

Uli


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Reply With Quote
Reply

  { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > C++


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 05:58 PM.


Powered by vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
© 1999-2008 mindfrost82.com v11.0


Sponsors:
Cheap Loan | Loans | Advertising | Mortgages | Mortgage Loans



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114