INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

OO Design: Negating methods?

OO Design: Negating methods?

(OP)
I've had the recent occasion to code something involving TFileStream, and came across a question regarding design.

What I'm doing (more or less) makes it suitable to descend a class from that. The problem is, a lot of the methods of TFileStream are either irrelevant or run completely counter to the function of the code I'm writing. I know I can override methods and get what I want done, and in fact I need to completely replace two of the methods in TFileStream in order to do what I want. The problem is the list of counter-productive methods.

So...I have two options. Option #1: List counter-productive methods in the protected section with overrides and define empty methods.

CODE

TMyClass = class(TFileStream)
...
protected
  function Seek(Offset: Longint; Origin: Word): Longint; override;

function TMyClass.Seek(Offset: Longint; Origin: Word): Longint;
begin
end; 

Or Option #2: Define an instance of TFileStream in my class, create/destroy it in my constructor/deconstructor and then use it.

CODE

TMyClass = class(TObject)
  private
    fFileStream: TFileStream;
... 

So the question: Which is the more preferred way to do it?


RE: OO Design: Negating methods?

Encapsulation (option 2) is the way to go.
The goal of a class should be clear and concise.
But encapsulation renders testability difficult, but can be solved in this case by using constructor injection (ie passing a TStream object via the constructor).

I hope this makes sense :)

/Daddy

-----------------------------------------------------
Helping people is my job...

RE: OO Design: Negating methods?

(OP)
Option #2 smelled right (as it has in most of the cases I've run into). The problem is that I'm not sure if there were any standard design sets or rules on when to do one or the other.

So when does one actually favor using inheritance as opposed to encapsulation?

RE: OO Design: Negating methods?

Inheritance is needed when you want to share the same behavior but the inherited object can change the implementation.

Think along these lines:

CODE

type 
 TAnimal = class
 public
   procedure MakeNoise; virtual; abstract;
 end; 

 TDog = class(TAnimal)
 public
  procedure MakeNoise; override;
 end;

 TCat = class(TAnimal)
 public
  procedure MakeNoise; override;
 end;

...

procedure TDog.MakeNoise;
begin
 ShowMessage('woef woef');
end;

procedure TCat.MakeNoise;
begin
 ShowMessage('miauw');
end; 

-----------------------------------------------------
Helping people is my job...

RE: OO Design: Negating methods?

(OP)
Thanks. So inheritance is functionally more for minor modifications, while encapsulation is for major ones?

RE: OO Design: Negating methods?

Ask yourself this simple question:
Is the class that you are making a TStream like class? in other words, does it behave as a Stream object?
Yes -> Inherit from the abstract class
No -> Encapsulate

-----------------------------------------------------
Helping people is my job...

RE: OO Design: Negating methods?

Agreed with all this. There are lots of examples in the Delphi source where encapsulation is done, eg. TThreadList - a thread-safe list. Even though it presents like a TList, it encapsulates and presents methods that handle the locking aspects.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close