Re: Abstract Method w/o Factory

From: Uncle Bob (Robert C. Martin) (u.n.c.l.e.b.o.b_at_objectmentor.com)
Date: 10/21/03


Date: Mon, 20 Oct 2003 23:09:59 -0500

stocksb@ieee.org (Benjamin M. Stocks) might (or might not) have
written this on (or about) 15 Oct 2003 07:36:00 -0700, :

>Hi all,
>
>I have a very simple class hierarchy, a base class and two classes
>derived from the base class. The derived classes will redefine a
>single function from the base class.
>
>My problem is in the creation of the derived classes. Needless to say
>I only want the "rest of the world" to depend on the base class, not
>the derived classes. I could use a factory to create a specific
>instance of one of the derived classes, however this is an existing
>application and adding a factory seems like a lot of overhead for what
>should be a simple task of selecting one or the other derived classes.
>
>The (C++ dependant) solution I came up with was to create a
>GetInstance method in the base class that returns a pointer to a base
>class instance and in the implementation file for the base class have
>the GetInstance method perform the same service as a factory would:
>instantiate the appropriate derived class and return it. With this the
>interface to the base class doesn't depend on the derived classes but
>the implementation of the base class depends on the derived classes.
>Make sense?
>
>I'm looking for feedback on what is wrong or right with this approach.
>I know its not an OO pure solution since it cannot be implemented in
>Java the same way and it essentially creates cyclic dependancies since
>the base class implementation depends on the derived classes. Is that
>the only drawback?

Ben,

It's a little uncommon for a base class to depend upon it's
derivatives; but it's not forbidden. It wouldn't be my first choice
solution; but I can understand why you did it.
Robert C. Martin | "Uncle Bob"
Object Mentor Inc. | unclebob @ objectmentor . com
501 N. Riverside Dr.| Tel: (800) 338-6716
Suite 206 | Fax: (847) 775-8174 | www.objectmentor.com
                    | | www.XProgramming.com
Gurnee, IL, | Training and Mentoring | www.junit.org
60031 | OO, XP, Agile, C++, Java, C# | http://fitnesse.org



Relevant Pages

  • Re: Not pure virtual functions and mixing virtual and non virtual methods
    ... Should I call base class implementation or not? ... But doing the above assumes that one answer is good for all, that all derived classes will want the base class method called in the same way. ... More generally, the way I see it, if you combine invariant implementation with variant parts, you have no elegant way to change the variant part. ... Following them will remove all inheritance relationships. ...
    (comp.object)
  • Re: Abstract Method w/o Factory
    ... Responding to Stocks... ... > single function from the base class. ... > My problem is in the creation of the derived classes. ... I could use a factory to create a specific ...
    (comp.object)
  • This one took weeks to resolve
    ... would be better designed as deriving from a base class form. ... control in the Windows generated code. ... These derived classes where the only place in a project ... Private sub Btn_ClickHandles btn.click ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How many bytes per Italian character?
    ... override the InitState() function so it needs to be pure virtual. ... derived classes call up to them anyway; ... both the base class and derived class. ...
    (microsoft.public.vc.mfc)
  • Re: Inheritance and Interfaces
    ... Talk about having a "duh" moment. ... >You only need to implement INotifyPropertyChanged in the base class, ... > Public Property NameAs String ... >The Overridable Sub OnPropertyChanged allows derived classes to raise ...
    (microsoft.public.dotnet.languages.vb)