home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue49 / Factory / FMain.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-06-16  |  6.9 KB  |  236 lines

  1. {* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2.   TechInsite Pty. Ltd.
  3.   PO Box 429, Abbotsford, Melbourne. 3067 Australia
  4.   Phone: +61 3 9419 6456
  5.   Fax:   +61 3 9419 1682
  6.   Web:   www.techinsite.com.au
  7.   EMail: peter_hinrichsen@techinsite.com.au
  8.  
  9.   Created: 01/06/1999
  10.  
  11.   Notes: Factory pattern demo main form
  12.          Allow the user choose an object to create.
  13.          Options include:
  14.          1) Reports using an if-then-else statement
  15.          2) Reports using a simple factory
  16.          3) Reports using an abstract / concrete factory
  17.          4) TObject descendants using an abstract / concrete factory. These
  18.             objects are cached so they are only created once.
  19.  
  20. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
  21.  
  22. unit FMain;
  23.  
  24. interface
  25.  
  26. uses
  27.   Windows, Messages, SysUtils, Classes, Graphics, Controls,
  28.   Forms, Dialogs, StdCtrls, ComCtrls;
  29.  
  30. type
  31.   TFormReportSelection = class(TForm)
  32.     pcFactoryDemo: TPageControl;
  33.     tsComponentFactory: TTabSheet;
  34.     tsObjectFactory: TTabSheet;
  35.     GroupBox1: TGroupBox;
  36.     lbReports: TListBox;
  37.     GroupBox2: TGroupBox;
  38.     btnIfThenElse: TButton;
  39.     btnSimpleFactory: TButton;
  40.     GroupBox4: TGroupBox;
  41.     lbAnimals: TListBox;
  42.     btnCreateAnimal: TButton;
  43.     btnAbsConcreteFactory: TButton;
  44.     procedure FormCreate(Sender: TObject);
  45.     procedure btnIfThenElseClick(Sender: TObject);
  46.     procedure btnSimpleFactoryClick(Sender: TObject);
  47.     procedure btnAbsConcreteFactoryClick(Sender: TObject);
  48.     procedure btnCreateAnimalClick(Sender: TObject);
  49.   private
  50.     { Private declarations }
  51.   public
  52.     { Public declarations }
  53.   end;
  54.  
  55. var
  56.   FormReportSelection: TFormReportSelection;
  57.  
  58. implementation
  59. uses
  60.   // This unit contains the declarations for
  61.   // cgStrReportListing, cgStrReportNameMailingLabels,
  62.   // and so on. All constants are declared in a seperate
  63.   // unit in preparation for porting the application to
  64.   // multi-tier. This is supprisingly important and will be
  65.   // discussed later.
  66.   Constants
  67.  
  68.   ,FactoryReport         // The simple factory
  69.   ,FactoryConcreteReport // The report concrete factory
  70.   
  71.   ,FReportAbstract       // The parent of all reports
  72.   ,FReportAddressLabel   // The address label report
  73.   ,FReportListing        // The listing report
  74.   ,FReportDataExport     // The data export report
  75.  
  76.   ,FactoryConcreteAnimal // The animal concrete factory
  77.   ,Animals               // The family of animal classes
  78.  
  79.   ;
  80.  
  81. {$R *.DFM}
  82.  
  83. // In the form's FormCreate event, we fill the listBox with
  84. // possible report names
  85. //----------------------------------------------------------
  86. procedure TFormReportSelection.FormCreate(Sender: TObject);
  87. begin
  88.  
  89.   // Just in case I changed the active page at design time.
  90.   pcFactoryDemo.ActivePage := tsComponentFactory ;
  91.  
  92.   // Add reports (TComponents) to the list
  93.   with lbReports.Items do begin
  94.     Clear;
  95.     Add( cgStrReportNameListing );
  96.     Add( cgStrReportNameMailingLabels );
  97.     Add( cgStrReportNameDataExport );
  98.   end;
  99.  
  100.   // Add a animals (TObjects) to the list
  101.   with lbAnimals.Items do begin
  102.     Clear ;
  103.     Add( cgStrAnimalDog  ) ;
  104.     Add( cgStrAnimalCat  ) ;
  105.     Add( cgStrAnimalBird ) ;
  106.   end ;
  107.  
  108. end ;
  109.  
  110. // Run the selected report, using the if-then-else
  111. //programming style
  112. //----------------------------------------------------------
  113. procedure TFormReportSelection.btnIfThenElseClick(
  114.                                            Sender: TObject);
  115. var lStrReportName : string ;
  116.     lReport        : TFormReportAbstract ;
  117. begin
  118.  
  119.   // If no report is selected, then show an error dialog
  120.   if lbReports.ItemIndex = -1 then
  121.     Raise Exception.Create( 'Please select a report' ) ;
  122.  
  123.   // Read the selected report name into a string variable
  124.   lStrReportName := lbReports.Items[ lbReports.ItemIndex ] ;
  125.  
  126.   // The if-then-else statement
  127.   if lStrReportName = cgStrReportNameListing then
  128.   begin
  129.     lReport := TFormReportListing.Create( nil ) ;
  130.  
  131.   end
  132.   else if lStrReportName = cgStrReportNameMailingLabels then
  133.   begin
  134.     lReport := TFormReportMailingLabels.Create( nil )  ;
  135.  
  136.   end
  137.   else if lStrReportName = cgStrReportNameDataExport then
  138.   begin
  139.     lReport := TFormReportDataExport.Create( nil ) ;
  140.  
  141.   end
  142.   // More else if lStrReportName = ??? then for each report.
  143.  
  144.   else begin
  145.     raise Exception.Create( 'Can not run report. Report ' +
  146.                             'name <' + lStrReportName +
  147.                             '> not known. Called in ' +
  148.                             'TFormReportSelection.' +
  149.                             'btnIfThenElseClick.' ) ;
  150.   end ;
  151.  
  152.   try
  153.     lReport.Execute ;
  154.   finally
  155.     lReport.Free ;
  156.   end ;
  157.  
  158. end ;
  159.  
  160.  
  161. // Run the report using a simple factory
  162. //----------------------------------------------------------
  163. procedure TFormReportSelection.btnSimpleFactoryClick(
  164.                                            Sender: TObject);
  165. var lStrReportName : string ;
  166.     lReport : TFormReportAbstract ;
  167. begin
  168.  
  169.   // If no report is selected, then show an error dialog
  170.   if lbReports.ItemIndex = -1 then
  171.     Raise Exception.Create( 'Please select a report' ) ;
  172.  
  173.   // Read the selected report name into a string variable
  174.   lStrReportName := lbReports.Items[ lbReports.ItemIndex ] ;
  175.  
  176.   // Call the simple report factory to create an instance
  177.   // of TFormReportAbstract
  178.   lReport := gFactoryReport.GetReport( lStrReportName ) ;
  179.  
  180.   try
  181.     lReport.Execute ;
  182.   finally
  183.     lReport.Free ;
  184.   end ;
  185.  
  186. end;
  187.  
  188. // Run the report using an abstract / concrete factory
  189. //----------------------------------------------------------
  190. procedure TFormReportSelection.btnAbsConcreteFactoryClick(
  191.                                            Sender: TObject);
  192. var lStrReportName : string ;
  193.     lReport : TFormReportAbstract ;
  194. begin
  195.  
  196.   // If no report is selected, then show an error dialog
  197.   if lbReports.ItemIndex = -1 then
  198.     Raise Exception.Create( 'Please select a report' ) ;
  199.  
  200.   // Read the selected report name into a string variable
  201.   lStrReportName := lbReports.Items[ lbReports.ItemIndex ] ;
  202.  
  203.   // Call the simple report factory to create an instance
  204.   // of TFormReportAbstract
  205.   lReport := gFactoryConcreteReport.CreateInstanceExt( lStrReportName ) ;
  206.  
  207.   try
  208.     lReport.Execute ;
  209.   finally
  210.     lReport.Free ;
  211.   end ;
  212. end;
  213.  
  214. // Create an animal and make it 'talk' using an abstract / concrete factory
  215. // This factory is caching the animals as they are created
  216. procedure TFormReportSelection.btnCreateAnimalClick(Sender: TObject);
  217. var lStrClassID : string ;
  218.     lAnimal : TAnimal ;
  219. begin
  220.  
  221.   if lbAnimals.ItemIndex = -1 then
  222.     Raise Exception.Create( 'Please select an animal' ) ;
  223.  
  224.   lStrClassID := lbAnimals.Items[ lbAnimals.ItemIndex ] ;
  225.  
  226.   lAnimal := gFactoryConcreteAnimal.CreateInstanceExt( lStrClassID ) ;
  227.  
  228.   // These are all created as singletons, so the factory will
  229.   // take care of freeing them up.
  230.   lAnimal.Talk ;
  231.  
  232. end;
  233.  
  234. end.
  235.  
  236.