C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Seeking design pattern..

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Søren Johansen
Guest





PostPosted: Fri Sep 12, 2003 9:02 pm    Post subject: Seeking design pattern.. Reply with quote



Hi,

I am writing a small framework for doc/view applications. For this I have a
document base class, CBaseDocument, that implements a number of load/save
functionalities and some other stuff.
As it is now, the constructor makes a semi-zombie object that has to be
either Load'ed or New'ed (New being a method of this class) before it can be
relied upon.

class CBaseDocument
{
public:
CBaseDocument();
virtual void Load(std::string const &filename);
virtual void New(std::string const &name);

...
};

I would like "raii-enable" the class instead so this zombie state could be
avoided. This would require a Load-constructor and a New-constructor, but
there are two problems with this. One, as you can see, Load and New are
virtual (well actually, this is simplified. Load is not virtual as the
example states but it calls virtual methods), and two, Load and New take
identical sets of parameters so I would have to use named constructors or
some other trick but this makes it difficult to inherit from the class.

Any suggestions to an approach would be greatly appreciated.

Søren




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ivan Vecerina
Guest





PostPosted: Mon Sep 15, 2003 12:37 am    Post subject: Re: Seeking design pattern.. Reply with quote



"Søren Johansen" <soerenjohansen171 (AT) hotmail (DOT) com> wrote

Quote:
I am writing a small framework for doc/view applications. For this I have
a
document base class, CBaseDocument, that implements a number of load/save
functionalities and some other stuff.
As it is now, the constructor makes a semi-zombie object that has to be
either Load'ed or New'ed (New being a method of this class) before it can
be
relied upon.

class CBaseDocument
{
public:
CBaseDocument();
virtual void Load(std::string const &filename);
virtual void New(std::string const &name);

...
};

I would like "raii-enable" the class instead so this zombie state could be
avoided. This would require a Load-constructor and a New-constructor, but
there are two problems with this. One, as you can see, Load and New are
virtual (well actually, this is simplified. Load is not virtual as the
example states but it calls virtual methods), and two, Load and New take
identical sets of parameters so I would have to use named constructors or
some other trick but this makes it difficult to inherit from the class.

Any suggestions to an approach would be greatly appreciated.

What you are looking for is the "Factory" design pattern
(I'll let you google for it or check your favorite book).
Basic idea for a possible implementation approach:
class CDocumentSubclass {
// these functions create and initialize a new subclass instance
static CBaseDocument* loadDoc(std::string const &filename);
static CBaseDocument* newDoc (std::string const &filename);
};

Pointers to the loadDoc() & newDoc() factory functions can be
passed around when needed -- as a substitute to polymorphism
when no object instance exist yet.


hth
--
http://ivan.vecerina.com



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

Back to top
Ben Hutchings
Guest





PostPosted: Mon Sep 15, 2003 9:59 pm    Post subject: Re: Seeking design pattern.. Reply with quote



[I tried to mail this, but your address seems to be invalid.]

In article <3f6181d3$0$216$edfadb0f (AT) dread11 (DOT) news.tele.dk>,
S÷ren Johansen wrote:
Quote:
Hi,

I am writing a small framework for doc/view applications.
snip


If you haven't done so already, I suggest you consider the
model/view/controller pattern instead of doc/view. It tends
to result in a cleaner separation of responsibilities.

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

Back to top
Mirek Fidler
Guest





PostPosted: Wed Sep 17, 2003 9:34 am    Post subject: Re: Seeking design pattern.. Reply with quote

Quote:
As it is now, the constructor makes a semi-zombie object that has to
be
either Load'ed or New'ed (New being a method of this class) before it
can be
relied upon.

class CBaseDocument
{
public:
CBaseDocument();
virtual void Load(std::string const &filename);
virtual void New(std::string const &name);

...
};

I would like "raii-enable" the class instead so this zombie state
could be
avoided. This would require a Load-constructor and a New-constructor,
but


Hm, maybe my answer is not what you would expect, but I think you
should stay with "zombie" state. This does not prevent you from cleaning
resources in destructor. I am using this approach quite often (though I
do not call it "zombie" but "empty" Smile - often it is the best one.

Mirek



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

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.