home *** CD-ROM | disk | FTP | other *** search
- {* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- TechInsite Pty. Ltd.
- PO Box 429, Abbotsford, Melbourne. 3067 Australia
- Phone: +61 3 9419 6456
- Fax: +61 3 9419 1682
- Web: www.techinsite.com.au
- EMail: peter_hinrichsen@techinsite.com.au
-
- Created: 01/06/1999
-
- Notes: Factory pattern demo main form
- Allow the user choose an object to create.
- Options include:
- 1) Reports using an if-then-else statement
- 2) Reports using a simple factory
- 3) Reports using an abstract / concrete factory
- 4) TObject descendants using an abstract / concrete factory. These
- objects are cached so they are only created once.
-
- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
-
- unit FMain;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls,
- Forms, Dialogs, StdCtrls, ComCtrls;
-
- type
- TFormReportSelection = class(TForm)
- pcFactoryDemo: TPageControl;
- tsComponentFactory: TTabSheet;
- tsObjectFactory: TTabSheet;
- GroupBox1: TGroupBox;
- lbReports: TListBox;
- GroupBox2: TGroupBox;
- btnIfThenElse: TButton;
- btnSimpleFactory: TButton;
- GroupBox4: TGroupBox;
- lbAnimals: TListBox;
- btnCreateAnimal: TButton;
- btnAbsConcreteFactory: TButton;
- procedure FormCreate(Sender: TObject);
- procedure btnIfThenElseClick(Sender: TObject);
- procedure btnSimpleFactoryClick(Sender: TObject);
- procedure btnAbsConcreteFactoryClick(Sender: TObject);
- procedure btnCreateAnimalClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
-
- var
- FormReportSelection: TFormReportSelection;
-
- implementation
- uses
- // This unit contains the declarations for
- // cgStrReportListing, cgStrReportNameMailingLabels,
- // and so on. All constants are declared in a seperate
- // unit in preparation for porting the application to
- // multi-tier. This is supprisingly important and will be
- // discussed later.
- Constants
-
- ,FactoryReport // The simple factory
- ,FactoryConcreteReport // The report concrete factory
-
- ,FReportAbstract // The parent of all reports
- ,FReportAddressLabel // The address label report
- ,FReportListing // The listing report
- ,FReportDataExport // The data export report
-
- ,FactoryConcreteAnimal // The animal concrete factory
- ,Animals // The family of animal classes
-
- ;
-
- {$R *.DFM}
-
- // In the form's FormCreate event, we fill the listBox with
- // possible report names
- //----------------------------------------------------------
- procedure TFormReportSelection.FormCreate(Sender: TObject);
- begin
-
- // Just in case I changed the active page at design time.
- pcFactoryDemo.ActivePage := tsComponentFactory ;
-
- // Add reports (TComponents) to the list
- with lbReports.Items do begin
- Clear;
- Add( cgStrReportNameListing );
- Add( cgStrReportNameMailingLabels );
- Add( cgStrReportNameDataExport );
- end;
-
- // Add a animals (TObjects) to the list
- with lbAnimals.Items do begin
- Clear ;
- Add( cgStrAnimalDog ) ;
- Add( cgStrAnimalCat ) ;
- Add( cgStrAnimalBird ) ;
- end ;
-
- end ;
-
- // Run the selected report, using the if-then-else
- //programming style
- //----------------------------------------------------------
- procedure TFormReportSelection.btnIfThenElseClick(
- Sender: TObject);
- var lStrReportName : string ;
- lReport : TFormReportAbstract ;
- begin
-
- // If no report is selected, then show an error dialog
- if lbReports.ItemIndex = -1 then
- Raise Exception.Create( 'Please select a report' ) ;
-
- // Read the selected report name into a string variable
- lStrReportName := lbReports.Items[ lbReports.ItemIndex ] ;
-
- // The if-then-else statement
- if lStrReportName = cgStrReportNameListing then
- begin
- lReport := TFormReportListing.Create( nil ) ;
-
- end
- else if lStrReportName = cgStrReportNameMailingLabels then
- begin
- lReport := TFormReportMailingLabels.Create( nil ) ;
-
- end
- else if lStrReportName = cgStrReportNameDataExport then
- begin
- lReport := TFormReportDataExport.Create( nil ) ;
-
- end
- // More else if lStrReportName = ??? then for each report.
-
- else begin
- raise Exception.Create( 'Can not run report. Report ' +
- 'name <' + lStrReportName +
- '> not known. Called in ' +
- 'TFormReportSelection.' +
- 'btnIfThenElseClick.' ) ;
- end ;
-
- try
- lReport.Execute ;
- finally
- lReport.Free ;
- end ;
-
- end ;
-
-
- // Run the report using a simple factory
- //----------------------------------------------------------
- procedure TFormReportSelection.btnSimpleFactoryClick(
- Sender: TObject);
- var lStrReportName : string ;
- lReport : TFormReportAbstract ;
- begin
-
- // If no report is selected, then show an error dialog
- if lbReports.ItemIndex = -1 then
- Raise Exception.Create( 'Please select a report' ) ;
-
- // Read the selected report name into a string variable
- lStrReportName := lbReports.Items[ lbReports.ItemIndex ] ;
-
- // Call the simple report factory to create an instance
- // of TFormReportAbstract
- lReport := gFactoryReport.GetReport( lStrReportName ) ;
-
- try
- lReport.Execute ;
- finally
- lReport.Free ;
- end ;
-
- end;
-
- // Run the report using an abstract / concrete factory
- //----------------------------------------------------------
- procedure TFormReportSelection.btnAbsConcreteFactoryClick(
- Sender: TObject);
- var lStrReportName : string ;
- lReport : TFormReportAbstract ;
- begin
-
- // If no report is selected, then show an error dialog
- if lbReports.ItemIndex = -1 then
- Raise Exception.Create( 'Please select a report' ) ;
-
- // Read the selected report name into a string variable
- lStrReportName := lbReports.Items[ lbReports.ItemIndex ] ;
-
- // Call the simple report factory to create an instance
- // of TFormReportAbstract
- lReport := gFactoryConcreteReport.CreateInstanceExt( lStrReportName ) ;
-
- try
- lReport.Execute ;
- finally
- lReport.Free ;
- end ;
- end;
-
- // Create an animal and make it 'talk' using an abstract / concrete factory
- // This factory is caching the animals as they are created
- procedure TFormReportSelection.btnCreateAnimalClick(Sender: TObject);
- var lStrClassID : string ;
- lAnimal : TAnimal ;
- begin
-
- if lbAnimals.ItemIndex = -1 then
- Raise Exception.Create( 'Please select an animal' ) ;
-
- lStrClassID := lbAnimals.Items[ lbAnimals.ItemIndex ] ;
-
- lAnimal := gFactoryConcreteAnimal.CreateInstanceExt( lStrClassID ) ;
-
- // These are all created as singletons, so the factory will
- // take care of freeing them up.
- lAnimal.Talk ;
-
- end;
-
- end.
-
-