Abstract Method w/o Factory
From: Benjamin M. Stocks (stocksb_at_ieee.org)
Date: 10/15/03
- Next message: Kevin Cline: "Re: Question about removing duplication: inheritance vs static methods"
- Previous message: Phlip: "Re: Question about removing duplication: inheritance vs static methods"
- Next in thread: H. S. Lahman: "Re: Abstract Method w/o Factory"
- Reply: H. S. Lahman: "Re: Abstract Method w/o Factory"
- Reply: Uncle Bob (Robert C. Martin): "Re: Abstract Method w/o Factory"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 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?
Thanks in advance,
Ben
- Next message: Kevin Cline: "Re: Question about removing duplication: inheritance vs static methods"
- Previous message: Phlip: "Re: Question about removing duplication: inheritance vs static methods"
- Next in thread: H. S. Lahman: "Re: Abstract Method w/o Factory"
- Reply: H. S. Lahman: "Re: Abstract Method w/o Factory"
- Reply: Uncle Bob (Robert C. Martin): "Re: Abstract Method w/o Factory"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|