[fpc-devel] Parameters must match exactly?

Graeme Geldenhuys graemeg.lists at gmail.com
Tue May 18 10:40:12 CEST 2010


On 18 May 2010 10:12, Florian Klaempfl wrote:
>
> - it is one of the basic programming principles: if a result is returned
> by a subroutine, use a function

The function might return a boolean (success/fail), or an error code
etc.. The function result doesn't need to return the new instance
created by the factory method.


Anyway, I knocked up a very basic Factory Method example, and I fear
(it pains me dearly <wink>) to say that you are correct in this
example. :) Normal usage of a Factory Method will pass in a base
class, which will actually match the signature of the Factory Method.

But getting back to may original issue about
TfpgApplication.CreateForm(), this works very different to a Factor
Method and the usage is not deemed "unsafe".

  fpgApplication.CreateForm(TMyForm, MyForm);

But I guess the only solution (forced upon me now) is to use some
magic code like LCL does. An untyped variable (YUCK!!! They are as bad
as Variants!) and then type cast it inside the implementation of
CreateForm and use the 'is' operator to find out if the instance
really is a TfpgWindowBase descendant. A whole much of crap code that
wasn't needed with FPC 2.4.1. Oh well. :-/


Anyway, to show that I can admit I was wrong.... Here is an example to
show that Factory Method can be implemented without problems using
latest FPC 2.5.1
----------------------------
type
   TBaseReport = class(TObject)
   public
     procedure Run; virtual; abstract;
   end;

   TReportA = class(TBaseReport)
   public
     procedure Run; override;
   end;

   TReportB = class(TBaseReport)
   public
     procedure Run; override;
   end;

   TReportClass = class of TBaseReport;

   TFactory = class(TObject)
   public
     procedure RegisterReport(const AName: string; AClass: TReportClass);
     function  CreateReport(const AName: string; out ARpt:
TBaseReport): Boolean;
   end;

  // singleton of TFactory
 function gFactory: TFactory;

... and then when used...

var
  rpt: TBaseReport;
  ok: Boolean;
begin
  ok := gFactory.CreateReport('a', rpt);
  if ok then
  begin
    rpt.Run;
    rpt.Free;
  end;
end;




-- 
Regards,
  - Graeme -


_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/



More information about the fpc-devel mailing list