Tony Sinclair wrote ...
> Hi all,
>
> Is it possible to have a constructor for the parent class that receives a
> parameter which determines the type of child that is instantiated in it's
> place?

You could try creating a 'factory' class whose sole
responsibility is to manufacture descendants of another
class (my favourite use/misuse of a design pattern.  :) )

For example, say you had the following hierarchy, and
any coloured widget might be required at runtime:

TColouredWidget = class( TObject );
TBlueWidget     = class( TColouredWidget );
TRedWidget      = class( TColouredWidget );
TGreenWidget    = class( TColouredWidget );

The factory class could be
something like:

type
  TWidgetColour = (wcBlue, wcRed, wcGreen);

  TWidgetFactory = class(TObject)
  public
    class function CreateColouredWidget( WidgetColour: TWidgetColour ):
TColouredWidget;
  end;

implementation

class function TWidgetFactory.CreateColouredWidget( WidgetColour:
TWidgetColour ): TColouredWidget;
begin
  case WidgetColour of
    wcBlue:  Result := TBlueWidget.Create;
    wcRed:       Result := TRedWidget.Create;
    wcGreen: Result := TGreenWidget.Create;
  end;
end;


... and could be called thus:

Widget := TWidgetFactory.CreateColouredWidget( WidgetColour );

Is this similar to what you're after?

Cheers,

Cory.



---------------------------------------------------------------------------
    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"


---------------------------------------------------------------------------
    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"

Reply via email to