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
- Next message: Peter O'Connell: "Re: P-EAA, .NET + Domain Model"
- Previous message: Uncle Bob (Robert C. Martin): "Re: Exposing Business Layer Objects to Presentation Layer"
- In reply to: Benjamin M. Stocks: "Abstract Method w/o Factory"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Peter O'Connell: "Re: P-EAA, .NET + Domain Model"
- Previous message: Uncle Bob (Robert C. Martin): "Re: Exposing Business Layer Objects to Presentation Layer"
- In reply to: Benjamin M. Stocks: "Abstract Method w/o Factory"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|