Board index » delphi » Best (better) solution

Best (better) solution

Hi,

I have a conceptual dilemma regarding one of my custom classes. The base
class (TDatabase) has a couple of methods, one of which is used to insert
data into a database. I would like to be able to have different parameters
for this method, so one suggestion was to use overloading. My questions:

1) Should I overload this method in the base class and write all the
necessary implementations (as in Example 1)?
2) Or should I define this method as virtual and abstract, and then derive
from this class and implement the method as needed (as in Example 2)--but
then I will only be able to write once its implementation--won't I?
3) Define the method in the base but not write any implementation, and let
the derived class overload the method as needed (as in Example 3)?
4) Or is there a better solution?

Thanks.
Michael

*********************
EXAMPLE 1
*********************
type
  TDatabase = class
  private
    m_Connection: TADOConnection;
    m_Command: TADOCommand;
    procedure SetCommand(const Value: TADOCommand);
    procedure SetConnection(const Value: TADOConnection);
  public
    procedure closeConnection;
    function insertData(const Value: String; const ProcName: String):
Boolean; overload;    -- the implementation would contain what is needed.
    function insertData(const Value: String): Boolean;
          -- the implementation would contain what is needed.
    function openConnection: Boolean;
    property Connection: TADOConnection
      read m_Connection write SetConnection;
    property Command: TADOCommand
      read m_Command write SetCommand;
  end;

*********************
EXAMPLE 2
*********************
type
  TDatabase = class
  private
    m_Connection: TADOConnection;
    m_Command: TADOCommand;
    procedure SetCommand(const Value: TADOCommand);
    procedure SetConnection(const Value: TADOConnection);
  public
    procedure closeConnection;
    function insertData:Boolean; virtual; abstract;        -- inheritance
here with method implementation written in the descendant class.
    function openConnection: Boolean;
    property Connection: TADOConnection
      read m_Connection write SetConnection;
    property Command: TADOCommand
      read m_Command write SetCommand;
  end;

*********************
EXAMPLE 3
*********************
type
  TDatabase = class
  private
    m_Connection: TADOConnection;
    m_Command: TADOCommand;
    procedure SetCommand(const Value: TADOCommand);
    procedure SetConnection(const Value: TADOConnection);
  public
    procedure closeConnection;
    function insertData:Boolean;              -- descendant class will
overload this method.
    function openConnection: Boolean;
    property Connection: TADOConnection
      read m_Connection write SetConnection;
    property Command: TADOCommand
      read m_Command write SetCommand;
  end;

//Descendant class
TMyDatabase = class(TDatabase)
  public
    function insertData: Boolean;
          -- overloaded from base class.
    function insertData(const Value: String): Boolean; overload;        --
overloaded from base class.
  end;

 

Re:Best (better) solution


Quote
> 1) Should I overload this method in the base class and write all the
> necessary implementations (as in Example 1)?

It depends. Generally speaking, you should push common functionality up
the inheritance hierarchy (ie have it as close to the root class as
possible). So if it makes sense to have an implementation of a method in
your base class, then put it there.

Quote
> 2) Or should I define this method as virtual and abstract, and then derive
> from this class and implement the method as needed (as in Example 2)

This option should be used if you want to force descendant classes to
implement the method. If the method is not implemented, then you will
not be able to make instances of the class. Usually, you would only do
this if the method was essential to every descendant class, and the
implementation would be different in every descendant class.

If there is a common implementation that will work most of the time, and
you only need some descendant classes to change it, then implement the
method in your base class and mark it as virtual but not abstract.

You can introduce overloaded methods whenever you like in descendant
classes. However, if you find 2 descendant classes at the same level of
inheritance (eg they both inherit from TDatabase) and you have
introduced the same method signature in both, then push the definition
up to the ancestor class.

Quote
> then I will only be able to write once its implementation--won't I?

No. If a method is virtual you can override it as often as you like. If
it is abstract you **must** implement it in classes you want to
instantiate.

Also, I know this is just an example, but I would be careful with your
naming conventions. TDatabase already exists as a class name in Delphi.
You would be better off adding some sort of prefix of your own
after the "T" in class names (eg TMIkeDatabase) to reduce name
collisions. You can deal with the collisions other ways, but they are
best avoided, IMO.

Cheers,
Jim Cooper

____________________________________________

Jim Cooper      jcoo...@tabdee.ltd.uk
Tabdee Ltd      http://www.tabdee.ltd.uk

TurboSync - Connecting Delphi with your Palm
____________________________________________

Re:Best (better) solution


Thanks for your reply Jim.

Quote
>> then I will only be able to write once its implementation--won't I?
> No. If a method is virtual you can override it as often as you like. If
> it is abstract you **must** implement it in classes you want to
> instantiate.

What I meant is that I cannot (or at least I could not) overload an
overriden method. Therefore, if I have a subclass, in that subclass I will
be able to implement the method only once, thus no method overloading. Is
that correct?

Michael

Quote
"Jim Cooper" <jcoo...@tabdee.ltd.uk> wrote in message

news:3E783C1E.98E21BA6@tabdee.ltd.uk...
Quote

> > 1) Should I overload this method in the base class and write all the
> > necessary implementations (as in Example 1)?

> It depends. Generally speaking, you should push common functionality up
> the inheritance hierarchy (ie have it as close to the root class as
> possible). So if it makes sense to have an implementation of a method in
> your base class, then put it there.

> > 2) Or should I define this method as virtual and abstract, and then
derive
> > from this class and implement the method as needed (as in Example 2)

> This option should be used if you want to force descendant classes to
> implement the method. If the method is not implemented, then you will
> not be able to make instances of the class. Usually, you would only do
> this if the method was essential to every descendant class, and the
> implementation would be different in every descendant class.

> If there is a common implementation that will work most of the time, and
> you only need some descendant classes to change it, then implement the
> method in your base class and mark it as virtual but not abstract.

> You can introduce overloaded methods whenever you like in descendant
> classes. However, if you find 2 descendant classes at the same level of
> inheritance (eg they both inherit from TDatabase) and you have
> introduced the same method signature in both, then push the definition
> up to the ancestor class.

> > then I will only be able to write once its implementation--won't I?

> No. If a method is virtual you can override it as often as you like. If
> it is abstract you **must** implement it in classes you want to
> instantiate.

> Also, I know this is just an example, but I would be careful with your
> naming conventions. TDatabase already exists as a class name in Delphi.
> You would be better off adding some sort of prefix of your own
> after the "T" in class names (eg TMIkeDatabase) to reduce name
> collisions. You can deal with the collisions other ways, but they are
> best avoided, IMO.

> Cheers,
> Jim Cooper

> ____________________________________________

> Jim Cooper      jcoo...@tabdee.ltd.uk
> Tabdee Ltd      http://www.tabdee.ltd.uk

> TurboSync - Connecting Delphi with your Palm
> ____________________________________________

Re:Best (better) solution


Example 3 will not work, because TDatabase.insertData is not virtual, it can
not be overridden in TMyDatabase.
Example 2: you cant add other parameters in an overriden class, it will only
have insertData.

hth

Bas

Re:Best (better) solution


i think you can overload an overriden method.

i think that this does not work:
method;overload;override;
but this does work:
method;override;overload;
(but i'm not sure)

hth

Bas

Quote
"Mike" <n...@none.com> wrote in message

news:3e7841ec$1@newsgroups.borland.com...
|
| Thanks for your reply Jim.
|
| >> then I will only be able to write once its implementation--won't I?
| > No. If a method is virtual you can override it as often as you like. If
| > it is abstract you **must** implement it in classes you want to
| > instantiate.
|
| What I meant is that I cannot (or at least I could not) overload an
| overriden method. Therefore, if I have a subclass, in that subclass I will
| be able to implement the method only once, thus no method overloading. Is
| that correct?
|
| Michael
|
|
|
|
| "Jim Cooper" <jcoo...@tabdee.ltd.uk> wrote in message
| news:3E783C1E.98E21BA6@tabdee.ltd.uk...
| >
| > > 1) Should I overload this method in the base class and write all the
| > > necessary implementations (as in Example 1)?
| >
| > It depends. Generally speaking, you should push common functionality up
| > the inheritance hierarchy (ie have it as close to the root class as
| > possible). So if it makes sense to have an implementation of a method in
| > your base class, then put it there.
| >
| > > 2) Or should I define this method as virtual and abstract, and then
| derive
| > > from this class and implement the method as needed (as in Example 2)
| >
| > This option should be used if you want to force descendant classes to
| > implement the method. If the method is not implemented, then you will
| > not be able to make instances of the class. Usually, you would only do
| > this if the method was essential to every descendant class, and the
| > implementation would be different in every descendant class.
| >
| > If there is a common implementation that will work most of the time, and
| > you only need some descendant classes to change it, then implement the
| > method in your base class and mark it as virtual but not abstract.
| >
| > You can introduce overloaded methods whenever you like in descendant
| > classes. However, if you find 2 descendant classes at the same level of
| > inheritance (eg they both inherit from TDatabase) and you have
| > introduced the same method signature in both, then push the definition
| > up to the ancestor class.
| >
| > > then I will only be able to write once its implementation--won't I?
| >
| > No. If a method is virtual you can override it as often as you like. If
| > it is abstract you **must** implement it in classes you want to
| > instantiate.
| >
| > Also, I know this is just an example, but I would be careful with your
| > naming conventions. TDatabase already exists as a class name in Delphi.
| > You would be better off adding some sort of prefix of your own
| > after the "T" in class names (eg TMIkeDatabase) to reduce name
| > collisions. You can deal with the collisions other ways, but they are
| > best avoided, IMO.
| >
| > Cheers,
| > Jim Cooper
| >
| > ____________________________________________
| >
| > Jim Cooper      jcoo...@tabdee.ltd.uk
| > Tabdee Ltd      http://www.tabdee.ltd.uk
| >
| > TurboSync - Connecting Delphi with your Palm
| > ____________________________________________
|
|

Re:Best (better) solution


Quote
> Is that correct?

No, you can override overloaded methods.

Cheers,
Jim Cooper

____________________________________________

Jim Cooper      jcoo...@tabdee.ltd.uk
Tabdee Ltd      http://www.tabdee.ltd.uk

TurboSync - Connecting Delphi with your Palm
____________________________________________

Re:Best (better) solution


Quote
On Wed, 19 Mar 2003 11:09:46 +0100, "Mike" <n...@none.com> wrote:

>Thanks for your reply Jim.

>>> then I will only be able to write once its implementation--won't I?
>> No. If a method is virtual you can override it as often as you like. If
>> it is abstract you **must** implement it in classes you want to
>> instantiate.

>What I meant is that I cannot (or at least I could not) overload an
>overriden method. Therefore, if I have a subclass, in that subclass I will
>be able to implement the method only once, thus no method overloading. Is
>that correct?

you can override an overloaded method but not overload an overridden
one   Thus you have to include at least abstract versions in the base
class..

Regards from Germany

Franz-Leo

Re:Best (better) solution


Quote
> you can override an overloaded method but not overload an overridden
> one  

Sure you can :

  TBaseClass = class(TObject)
  public
    procedure MyMethod; overload; virtual;
  end;

  TDescendant1 = class(TBaseClass)
  public
    procedure MyMethod; override;
    procedure MyMethod(AStr : string); overload; virtual;
  end;

Cheers,
Jim Cooper

____________________________________________

Jim Cooper      jcoo...@tabdee.ltd.uk
Tabdee Ltd      http://www.tabdee.ltd.uk

TurboSync - Connecting Delphi with your Palm
____________________________________________

Re:Best (better) solution


Thanks Jim. I was not trying with the proper directives, that is why I could
not make it work :-)

Quote
"Jim Cooper" <jcoo...@tabdee.ltd.uk> wrote in message

news:3E7A5375.A85F418B@tabdee.ltd.uk...
Quote

> > you can override an overloaded method but not overload an overridden
> > one

> Sure you can :

>   TBaseClass = class(TObject)
>   public
>     procedure MyMethod; overload; virtual;
>   end;

>   TDescendant1 = class(TBaseClass)
>   public
>     procedure MyMethod; override;
>     procedure MyMethod(AStr : string); overload; virtual;
>   end;

> Cheers,
> Jim Cooper

> ____________________________________________

> Jim Cooper      jcoo...@tabdee.ltd.uk
> Tabdee Ltd      http://www.tabdee.ltd.uk

> TurboSync - Connecting Delphi with your Palm
> ____________________________________________

Other Threads