I posted a question a few days ago, and the answers told me to create my own classes.
I'm an old-school programmer from the pre-OOP days my programming is well structured, efficient and organized, but lacks in any custom OOPing other than using Delphi and 3rd party objects.
I had looked at how Delphi's object oriented classes worked back when I started using Delphi 2, but they seemed foreign to my programming background. I understand how they were and are excellent for developers designing components and for visual controls on the user interface. But I never found the need to use them in the coding of my program itself.
So now I look again, 15 years later, at Delphi's classes and OOPing. If I take, for example, a structure that I have such as:
type
TPeopleIncluded = record
IndiPtr: pointer;
Relationship: string;
end;
var
PeopleIncluded: TList<TPeopleIncluded>;
Then an OOP advocator will probably tell me to make this a class. Logically, I would think this would be a class inherited from the generic TList. I would guess this would be done like this:
TPeopleIncluded<T: class> = class(TList<T>)
But that's where I get stuck, and don't have good instructions on how ot do the rest.
When I look at some class that Delphi has as an example in the Generics.Collections unit, I see:
TObjectList<T: class> = class(TList<T>)
private
FOwnsObjects: Boolean;
protected
procedure Notify(const Value: T; Action: TCollectionNotification); override;
public
constructor Create(AOwnsObjects: Boolean = True); overload;
constructor Create(const AComparer: IComparer<T>; AOwnsObjects: Boolean = True); overload;
constructor Create(Collection: TEnumerable<T>; AOwnsObjects: Boolean = True); overload;
property OwnsObjects: Boolean read FOwnsObjects write FOwnsObjects;
end;
and then their definitions of the constructors and procedures are:
{ TObjectList<T> }
constructor TObjectList<T>.Create(AOwnsObjects: Boolean);
begin
inherited;
FOwnsObjects := AOwnsObjects;
end;
constructor TObjectList<T>.Create(const AComparer: IComparer<T>; AOwnsObjects: Boolean);
begin
inherited Create(AComparer);
FOwnsObjects := AOwnsObjects;
end;
constructor TObjectList<T>.Create(Collection: TEnumerable<T>; AOwnsObjects: Boolean);
begin
inherited Create(Collection);
FOwnsObjects := AOwnsObjects;
end;
procedure TObjectList<T>.Notify(const Value: T; Action: TCollectionNotification);
begin
inherited;
if OwnsObjects and (Action = cnRemoved) then
Value.Free;
end;
Let me tell you that this "simple" class definition may be obvious to those of you who have used OOP in Delphi for years, but to me it only provides me with hundreds of unanswered questions on what do I use and how do I use it.
To me, this does not appear to be a science. It appears to be an art of how to best structure your information into objects.
So this question, and I hope it doesn't get closed because I really need help with this, is where or how do I get the best instruction on using Delphi to create classes - and how to do it the proper Delphi way.
Well... Yeah. There really aren't a lot of formal requirements. It's really just a set of tools to help you organize your ideas, and eliminate a lot of duplication along the way.
Actually, the whole point of generic containers is that you don't have to make a new container class for each type of object. Instead, you'd make a new content class and then create a
TList<TWhatever>
.Think of a class instance as a pointers to a record.
Now: why use a class when you could use a pointer to a record? A couple reasons:
private
keyword so that other developers (including your future self) know not to depend on implementation details that may change or that just aren't important to understanding the concept.case
statement where you do different things for each type of object, you loop through your list and send each object the same message, then it follows the function pointer to decide what to do.So in your other post, you indicated that your overall program looks like this:
It's not clear to me what
Indi
orJumpID
mean, so I'm going to pretend that your company does skydiving weddings, and thatIndi
means "individual" andJumpID
is a primary key in a database, indicating a flight where all those individuals are in the wedding party and scheduled to jump out of the same plane... And it's vitally important to know theirRelationship
to the happy couple so that you can give them the right color parachute.Obviously, that isn't going to match your domain exactly, but since you're asking a general question here, the details don't really matter.
What the people in the other post were trying to tell you (my guess anyway) wasn't to replace your list with a class, but to replace the JumpID with one.
In other words, rather than passing
JumpID
to a procedure and using that to fetch the list of people from a database, you create aJump
class.And if your JumpID actually indicates a jump as in
goto
, then you'd probably actually a bunch of classes that all subclass the same thing, and override the same method in different ways.In fact, let's assume that you do some parties that aren't weddings, and in that case, you don't need the Relationships, but only a simple list of people:
So now
PrintManifest()
does the job of yourPrintIndyEntry()
, but instead of calculating the list inline, it callsSelf.GetManifest()
.Now maybe your database doesn't change much, and your
TJump
instance is always short lived, so you decide to just populateSelf.manifest
in the constructor. In that case,GetManifest()
just returns that list.Or maybe your database changes frequently, or the
TJump
sticks around long enough that the database may change underneath it. In that case,GetManifest()
rebuilds the list each time it's called... Or perhaps you add anotherprivate
value indicating the last time you queried, and only update after the information expires.The point is that
PrintManifest
doesn't have to care howGetManifest
works, because you've hidden that information away.Of course, in Delphi, you could have done the same thing with a
unit
, hiding a list of cached passenger lists in yourimplementation
section.But clasess bring a little more to the table, when it comes time to implement the wedding-party-specific features:
So here, the
TWeddingJump
inherits theInit
andGetManifest
from theTJump
, but it also adds aGetWeddingManifest( );
, and it's going to override the behavior ofPrintManifest()
with some custom implementation. (You know it's doing this because of theoverride
marker here, which corresponds to thevirtual
marker inTJump
.But now, suppose that
PrintManifest
is actually a rather complicated procedure, and you don't want to duplicate all that code when all you want to do is add one column in the header, and another column in the body listing the relationship field. You can do that like so:Now, you want to do this:
But you can't, yet, because
GetManifest()
returnsTList< TPassenger >;
and forTWeddingJump
, you need it to returnTList< TWeddingGuest >
.Well, how can you handle that?
In your original code, you have this:
Pointer to what? My guess is that, just like this example, you have different types of individual, and you need them to do different things, so you just use a generic pointer, and let it point to different kinds of records, and hope you cast it to the right thing later. But classes give you several better ways to solve this problem:
TPassenger
a class and add aGetRelationship()
method. This would eliminate the need forTWeddingGuest
, but it means thatGetRelationship
method is always around, even when you're not talking about weddings.GetRelationship(guest:TPassenger)
in theTWeddingGuest
class, and just call that insideTWeddingGuest.PrintManifestRow()
.But suppose you have to query a database to populate that information. With the two methods above, you're issuing a new query for each passenger, and that might bog down your database. You really want to fetch everything in one pass, in
GetManifest()
.So, instead, you apply inheritance again:
Because
GetManifest()
returns a list of passengers, and all wedding guests are passengers, you can now do this:And now, you fill in the details for
TWeddingJump.PrintManifestRow
, and the same version ofPrintManifest
works for bothTJump
andTWeddingJump
.There's still one problem: we declared
PrintManifestRow(passenger:TPassenger)
but we're actually passing in aTWeddingGuest
. This is legal, becauseTWeddingGuest
is a subclass ofTPassenger
... But we need to get at the.relationship
field, andTPassenger
doesn't have that field.How can the compiler trust that inside a
TWeddingJump
, you're always going to pass in aTWeddingGuest
rather than just an ordinaryTPassenger
? You have to assure it that therelationship
field is actually there.You can't just declare it as
TWeddingJupmp.(passenger:TWeddingGuest)
because by subclassing, you're basically promising to do all the things the parent class can do, and the parent class can handle anyTPassenger
.So you could go back to checking the type by hand and casting it, just like an untyped pointer, but again, there are better ways to handle this:
PrintManifestRow()
method to theTPassenger
class (removing thepassenger:TPassenger
parameter, as this is now the implicit parameterSelf
), override that method inTWeddingGuest
, and then just haveTJump.PrintManifest
callpassenger.PrintManifestRow()
.TJump
itself a generic class (typeTJump<T:TPassenger> = class
), and instead of havingGetManifest()
return aTList<TPassenger>
, you have it returnTList<T>
. Likewise,PrintManifestRow(passenger:TPassenger)
becomesPrintManifestRow(passenger:T)
;. Now you can say:TWeddingJump = class(TJump<TWeddingGuest>)
and now you're free to declare the overridden version asPrintManifestRow(passenger:TWeddingGuest)
.Anyway, that's way more than I expected to write about all this. I hope it helped. :)