> You said the first bit is a hack. Well I don't see that you
> used any blunt instruments in getting access to protected properties.
> Just a little cheating.

It is a hack because it assumes that a base class can be safely cast into a
descendent.  This is OK due to the nature of Delphi's code generation, but
code like this will not work on say the .NET framework.

>   B/ you failed to mention that in this case you would then also have to
> create adaptors for     every other case available which wasn't a
> control.

In my post, there was...

  TColourableGraphicsObject = class (TInterfacedObject, IColourable)
  ... similar to TColourableControlAdapter

>   C/ when creating the adaptors in the first place to put in the list,
> you would then also have to have a big if statement testing which class
> you are creating an adaptor for.

No you don't - because you know the type of the objects at the time when you
add the objects to the list, you simply choose the appropriate
implementation, i.e., instead of writing

var
  tl: Tlist;
  MyFont: TFont;
  MyForm: TForm;

begin
  tl.Add (MyFont);
  tl.Add (MyForm);

you write:

var
  list: IInterfaceList;
  MyFont: TFont;
  MyForm: TForm;

begin
  list.Add (TColourableFontAdapter.Create (MyFont));
  list.Add (TColourableControlAdapter.Create (MyForm));

I don't see the need of if's, etc.

The very idea of using adapters is to avoid the use If's to test the class
type.  The adapters abstract out the different interfaces for the color
property into one common interface.

> The question was a simple one. Given an object of unknown class, how do
> you change the color property?

I think you also need to read the original post *CAREFULLY*.  I belief the
question was:

> Polymorphic assignment to Color - How? (sometime it's a Property,
sometimes not)

> I'm building a list of objects that can be coloured and want to be able to
get/set their colour.
>
> The object properties are all going to be stored in a Tlist so the types
will probably need to be stored along with them. I'm happy to store the
Object type (eg Tform ...) along with the reference to the object but I
can't see a general way to do this and get/set the color.

As far as I can see all the different proposed solutions satisfy the
problem:  "I can't see a general way to do this and get/set the color.".  I
don't think any one of them is right / wrong, good / bad.  They are
different approaches to solving one problem and have different pros and
cons.  The interface / adapter solution abstracts out the color property
into a common interface (polymorphic behaviour), is extensible but at the
expense of more code. The typinfo method uses RTTI and less code but assumes
that all object has a published property named "Color", is slower and is not
extensible.

Hopefully, Giovanni will have plenty to think about and choose the solution
appropriate to his problem.

This kinda reminds me of a German joke, "Our solution is your problem" - a
broken English version of "We have the solution to your problems".

Dennis.

----- Original Message -----
From: "Kyley Harris" <[EMAIL PROTECTED]>
To: "Multiple recipients of list delphi" <[EMAIL PROTECTED]>
Sent: Thursday, October 24, 2002 10:36 AM
Subject: RE: [DUG]: Polymorphic assignment to Color - How? (sometime it's a
Property, sometimes not)


> Now that I've woken up (yawn)
>
> I have to reply to two posts in one.
>
> 1/ Neven. Your solution is a good one. I do agree. I disagree that it
> was a good answer only :) based on the question asked. From the samples
> given etc of source, I would think that your solution did not solve the
> question appropriately based on the coding style and requirements of the
> query.
>
> 2/ Dennis. You said the first bit is a hack. Well I don't see that you
> used any blunt instruments in getting access to protected properties.
> Just a little cheating. The control adaptor is also a good solution to a
> problem. But in this case it also is not a good answer because:
>   A/ who said anything about it coming from Tcontrol? Only that it would
> have a color property.
>   B/ you failed to mention that in this case you would then also have to
> create adaptors for     every other case available which wasn't a
> control.
>   C/ when creating the adaptors in the first place to put in the list,
> you would then also have to have a big if statement testing which class
> you are creating an adaptor for. (no better than the original if
> statement), or you would need a dymamic generation of classes against
> class adaptors, Which would need to be implemented somewhere.
>
> The question was a simple one. Given an object of unknown class, how do
> you change the color property?
>
> While interfaces are cool, and adapters are neat. None of them actually
> answered the question within the standard potential of a delphi object.
>
> Hopefully they give everyone who didn't know of these options more
> insight. As I say, cats can be skinned nine times, then you get another
> cat :)
>
> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:owner-delphi@;delphi.org.nz] On
> Behalf Of Neven MacEwan
> Sent: Thursday, 24 October 2002 8:14 a.m.
> To: Multiple recipients of list delphi
> Subject: Re: [DUG]: Polymorphic assignment to Color - How? (sometime
> it's a Property, sometimes not)
>
>
> Kyley
>
> I did put '' around correct for a reason :-)
>
> Neven
>
>
> --------------------------------------------------------------------------
-
>     New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
>                   Website: http://www.delphi.org.nz
> To UnSub, send email to: [EMAIL PROTECTED]
> with body of "unsubscribe delphi"
> Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/
>
---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED] 
with body of "unsubscribe delphi"
Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/

Reply via email to