Click here to Skip to main content
16,014,940 members
Home / Discussions / C#
   

C#

 
AnswerRe: Should I be using inhertance for this? Pin
PIEBALDconsult4-Dec-10 16:30
mvePIEBALDconsult4-Dec-10 16:30 
GeneralRe: Should I be using inhertance for this? [modified] Pin
Jacob D Dixon4-Dec-10 17:14
Jacob D Dixon4-Dec-10 17:14 
GeneralRe: Should I be using inhertance for this? Pin
PIEBALDconsult5-Dec-10 3:49
mvePIEBALDconsult5-Dec-10 3:49 
GeneralRe: Should I be using inhertance for this? Pin
Jacob D Dixon5-Dec-10 4:15
Jacob D Dixon5-Dec-10 4:15 
GeneralRe: Should I be using inhertance for this? Pin
PIEBALDconsult5-Dec-10 8:53
mvePIEBALDconsult5-Dec-10 8:53 
GeneralRe: Should I be using inhertance for this? Pin
Jacob D Dixon6-Dec-10 16:17
Jacob D Dixon6-Dec-10 16:17 
GeneralRe: Should I be using inhertance for this? Pin
Spectre_0016-Dec-10 2:33
Spectre_0016-Dec-10 2:33 
AnswerRe: Should I be using inhertance for this? Pin
nortee5-Dec-10 21:29
nortee5-Dec-10 21:29 
Hello Smile | :) ,

While I use a different coding platform, I've worked a bit with inheritance and client/server applications. The examples described below is pseudocode (okay, I lie, it's a hack of mostly Delphi mixed with pseudocode, so sue me :P), so you should be able to make a decision as to whether you would want to use inheritance or not.

Your particular scenario involves transporting data between a client and a server. What data is being transported (for argument's sake) isn't actually what's important, how you *interpret* the data, on either side of the protocol, is. The core functionality of your base class involves just putting the data into a stream and reading the data from a stream. Nothing else. You don't want your objects to do more than it needs to. The specialisation of each (derived) class will contain the specific enhancements that you would want from it. So for my example, you have your class defined like so:

TYourBase = class(TObject)
protected
  procedure WriteToStream(AStream : TMemoryStream); virtual;
  procedure ReadFromStream(AStream : TMemoryStream); virtual;
public
  function ToStream : TMemoryStream;
  procedure FromStream(AStream : TMemoryStream);
end;


With your implementation for this class looking something like this:

procedure TYourBase.WriteToStream(AStream : TMemoryStream); 
begin
  // do some basic writing to the stream...
end;

procedure TYourBase.ReadFromStream(AStream : TMemoryStream); 
begin
  // do some basic reading from the stream...
end;

function TYourBase.ToStream : TMemoryStream;
begin
  Result := TMemoryStream.Create;
  WriteToStream(Result);
end;

procedure TYourBase.FromStream(AStream : TMemoryStream);
begin
  // maybe do some checks here...
  ReadFromStream(AStream);
end;


Now for the inheritance part. Let's say that your derived class needs to contain a few accessors (a string, an integer and a float). You would define it something like this:

TSomeDescendant = class(TYourBase)
protected
  procedure WriteToStream(AStream : TMemoryStream); override;
  procedure ReadFromStream(AStream : TMemoryStream); override;
public
  SomeString : String;
  SomeInteger : Integer;
  SomeFloat : Real;
end;

With the implementation looking something like this:
procedure TSomeDescendant.WriteToStream(AStream : TMemoryStream); 
begin
  // You would want to call this because the ancestor will most probably 
  // have done it's own bit of writing to the stream...
  inherited WriteToStream(AStream); 
  // Now write your class specific stuff...
  AStream.WriteString(SomeString);
  AStream.WriteInteger(SomeInteger);
  AStream.WriteFloat(SomeFloat);
end;

procedure TSomeDescendant.ReadFromStream(AStream : TMemoryStream);
begin
  // You would want to call this because the ancestor will most probably 
  // have done it's own bit of reading from the stream...
  inherited ReadFromStream(AStream); 
  // Now write your class specific stuff...
  SomeString := AStream.ReadString;
  SomeInteger := AStream.ReadInteger;
  SomeFloat := AStream.ReadFloat;
end;


Now, when you want to read or write from a stream, all you will need to do is something like this:
var
  LSomeObject : TSomeDescendant;
  LStream : TMemoryStream;
begin
  // Initialise the descendant class...
  LSomeObject := TSomeDescendant.Create;
  LSomeObject.SomeString := 'test';
  LSomeObject.SomeInteger := 1;
  LSomeObject.SomeFloat := 5.5;
  // let's stream the object...
  LStream := LSomeObject.ToStream;
  // now free the object...
  FreeAndNil(LSomeObject);
  // recreate the object and load it from the stream...
  LSomeObject := TSomeDescendant.Create;
  LSomeObject.FromStream(LStream);
end;


This is just one way in which you can use inheritance to simplify your streaming of data. To put this into an even broader context. You can define virtual methods in your ancestor class(es), to do things like validate the class or anything you want. Bearing in mind your example, you could define a virtual method to persist the data. That way you can define descendants that don't necessarily persist your data to SQL server, it could persist to anything you like: XML, Excel, [insert your favourite persistance flavour here].

I hope this helps you understand a bit more about how inheritance can help you build your class(es) to do as little as it possibly needs to do, while still allowing it to function correctly. By defining virtual and abstract functions and methods, you allow your derived classes to do what it needs to do, specific to itself. Where you define your printer class, you can specifically stream your data for that class. If you define several other classes, you can define specific reading and writing to the stream. This is how you provide polymorphic behaviour to your objects.

My example of writing and reading from a memory stream may not pertain to your example specifically, so I'll leave you with this final story: One of the more classic 'real life' examples of polymorphic behaviour is the question "What makes a saw a saw?". Programmaticaly, If you define a class called TSaw with a (virtual) method called Cut. The TSaw object itself does not know what to do when it needs to Cut. But you can define TRipSaw and THackSaw descendants which overrides the Cut method. They are still both TSaw descendants (and for all intents and purposes, they are still both saws), but they Cut differently. You wouldn't use a THackSaw to Cut wood, nor would you use a TRipSaw to Cut metal, but they both Cut and they are both saws... Smile | :) ...

Okay, I'd better stop this monologue and let you decide from there Smile | :) ...
Cheers,
Glen Vlotman

AnswerRe: Should I be using inhertance for this? Pin
Steve Naidamast6-Dec-10 3:04
professionalSteve Naidamast6-Dec-10 3:04 
AnswerRe: Should I be using inhertance for this? Pin
mbb016-Dec-10 22:05
mbb016-Dec-10 22:05 
GeneralRe: Should I be using inhertance for this? Pin
nortee6-Dec-10 23:21
nortee6-Dec-10 23:21 
GeneralRe: Should I be using inhertance for this? Pin
Jacob D Dixon8-Dec-10 3:46
Jacob D Dixon8-Dec-10 3:46 
AnswerRe: Should I be using inhertance for this? Pin
James Lonero8-Dec-10 8:41
James Lonero8-Dec-10 8:41 
QuestionMessage Removed Pin
4-Dec-10 13:27
SRJ924-Dec-10 13:27 
AnswerRe: Game Creation Pin
Luc Pattyn4-Dec-10 13:56
sitebuilderLuc Pattyn4-Dec-10 13:56 
GeneralRe: Game Creation Pin
SRJ924-Dec-10 14:09
SRJ924-Dec-10 14:09 
GeneralRe: Game Creation Pin
Luc Pattyn4-Dec-10 14:16
sitebuilderLuc Pattyn4-Dec-10 14:16 
GeneralRe: Game Creation Pin
SRJ924-Dec-10 14:21
SRJ924-Dec-10 14:21 
GeneralRe: Game Creation Pin
Pete O'Hanlon5-Dec-10 9:33
mvePete O'Hanlon5-Dec-10 9:33 
GeneralRe: Game Creation Pin
Luc Pattyn5-Dec-10 13:56
sitebuilderLuc Pattyn5-Dec-10 13:56 
QuestionwindowsForms Pin
nasis14-Dec-10 2:16
nasis14-Dec-10 2:16 
AnswerRe: windowsForms Pin
Eddy Vluggen4-Dec-10 2:37
professionalEddy Vluggen4-Dec-10 2:37 
AnswerRe: windowsForms Pin
Keith Barrow4-Dec-10 5:16
professionalKeith Barrow4-Dec-10 5:16 
GeneralRe: windowsForms Pin
Not Active4-Dec-10 5:50
mentorNot Active4-Dec-10 5:50 
GeneralRe: windowsForms Pin
fjdiewornncalwe4-Dec-10 7:53
professionalfjdiewornncalwe4-Dec-10 7:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.