Re: design quicky

From: TheFerryman (ferry_at_onthenet.com)
Date: 04/04/04


Date: Sun, 04 Apr 2004 22:31:01 +0100

On Sun, 04 Apr 2004 11:11:46 -0500, Robert C. Martin
<unclebob@objectmentor.com> wrote:

>On Fri, 02 Apr 2004 09:56:11 +0100, TheFerryman <ferry@onthenet.com>
>wrote:
>
>>I have a design problem that occurs fairly often. In essence it is
>>this:
>>
>>class Base
>>{
>>protected:
>>
>> stack<Base*> m_BaseStack;
>>
>> virtual void f()=0;
>>
>> virtual ~Base(){DeleteStack(m_BaseStack);}
>>};
>>
>>class DerivedType1 : public Base
>>{
>> void f()
>> {
>> if (SomeLogic())
>> {
>> m_BaseStack.push(new DerivedType2);
>> }
>> }
>>};
>>
>>
>>class DerivedType2 : public Base
>>{
>> void f()
>> {
>> if (SomeLogic())
>> {
>> m_BaseStack.push(new DerivedType999);
>> }
>> }
>>};
>>
>>
>>The code above is what I normally do but some of my peers abhor the
>>use of new in a function call. tbh, it's never felt right to me
>>either.
>
>Can you articulate why you don't want the new keyword in the function?
>I can think of a number of reasons, but I'd like to hear your reasons.

I feel as though the creation of an object should be the
responsibility of the class that owns (and deletes) the object, and
not the responsibility of the client.

Actually, writing this out has made me realize that the object that
owns the newed object *is* actually deleting it too! So this looks
okay to me now.

For the sake of furthering this discussion though, let's assume the
new keyword is used in a method call by an object who has no further
responsibility for the newly created object. What are *your* reasons
for disliking this (you said you can think of a number of them...)

>
>In any case, you could defer the new invocations into global
>functions, or you could create an abstract factory. In C++ either of
>those options will prevent your classes from #including the header
>files of the classes they are trying to create.

I think an abstract factory is what I was suggesting. I'm not sure
though, I'm inexperienced and I don't have the GOF book to hand to
verify.

>>
>>I'm considering creating a factory method in Base, that stores a map
>>of Derived types referenced into by string so I can do something like:
>>
>>class DerivedType2 : public Base
>>{
>> void f()
>> {
>> if (SomeLogic())
>> {
>> PushOnStack("DerivedType999");
>> }
>> }
>>};
>
>This is probably overkill. The base class would have to #include
>*every* derivative.

It wouldn't have to #include any derivatives. That's the partly the
point, clients can define the functionality of the map at runtime.
(all the derivatives will be #included in the object that sets up the
map)

>
>>Is this a better design or do you think it unnecessarily complex? What
>>do you think of the first design?
>
>I think the first design could be OK, depending on how many news are
>called from any given f method.



Relevant Pages

  • Last design issues to be solved.
    ... I've decided to completely revamp the design of my toy database program. ... #ifndef APPLICATION_H ... void startEng; ...
    (comp.object)
  • "Meatless MVC" seems success but new problem ahead.
    ... I've decided to completely revamp the design of my toy database program. ... #ifndef APPLICATION_H ... void startEng; ...
    (alt.comp.lang.learn.c-cpp)
  • Is this correct OO design?
    ... //Set properties of Article Object with values from Database ... IS IT BAD OO DESIGN TO CREATE AN INSTANCE OF A CLASS ... Article OriginalArticle = new Article; ... public static void GetArticleData ...
    (microsoft.public.dotnet.general)
  • Re: Is this correct OO design?
    ... This is NOT bad design. ... look at the timestamp data type in SQL Server, and Optimistic Concurrency ... > (OriginalArticle) WITHIN THE CLASS ITSELF? ... > public static void GetArticleData ...
    (microsoft.public.dotnet.general)
  • Re: Aggregation vs composition
    ... >Well, I'd say it's bad design, if one doesn't hide information when he ... >> includes responsibility for their attitude... ... >Does CarClient allocate memory for Car's Engine? ... For my part, I would say that this was composition, but I would love to ...
    (comp.object)