DesignPattern SimpleFactory

xiaoxiao2021-03-06  14

Unit unit2;

Interface

uses Windows, Messages, SysUtils, Variants, Classes, Controls, Dialogs, ExtCtrls, ShellAPI, AppEvnts, Forms; type IFruitInterface = interface [ '{D4557157-5241-4116-AA1E-87F86A884EA9}'] procedure grow (); procedure harvest ( Procedure plant ();

TYPE TAPPLE = Class (Tcomponent, IFRuitInterface) Private Treeage: Integer; Procedure Harvest (); Procedure PLANT ();

TStrwaBerry = class (TComponent, IFruitInterface) private treeAge: Integer; public procedure grow (); procedure harvest (); procedure plant (); end; TGrape = class (TComponent, IFruitInterface) private treeAge: Integer; public procedure grow (); Procedure harvest (); process ();

TFRUITGARDEN = Class Public Class Function GetFruit (const strfruit: string): impRuitInterface;

Implementation // {$ r * .dfm} procedure payment.grow (); begin showMessage ('Apple Grow');

Procedure Tapple.harvest (); Begin ShowMessage ('Apple Harvest'); END;

Procedure payment (); begin showMessage ('apple plante);

Procedure tstrwaberry.grow (); begin showMessage ('strwaberry plan ";

Procedure Tstrwaberry.harvest (); Begin ShowMessage ('strwaberry plant');

Procedure tstrwaberry.plant (); begin showMessage ('strwaberry plan ";

Procedure Tgrape.grow (); Begin ShowMessage ('Grape Plant'); END;

Procedure Tgrape.harvest (); Begin ShowMessage ('Grape Plant'); END;

Procedure tgrape.plant (); begin showMessage ('grape plant'); end;

class function TFruitGarden.GetFruit (const strFruit: string): IFruitInterface; begin if strFruit = 'Apple' then Result: = TApple.create (nil) else if strFruit = 'StrawBerry' then Result: = TStrwaBerry.create (nil) else if Strfruit = 'grape' Then Result: = Tgrape.create (nil) else raise exception.create ('cannot create' strfruit); end; end.

// Test

AfruitGarden; AFRUIT: IFRUITINTERFACE; Begin

AfruitGarden: = tfruitgarden.create (); try afruit: = afruitgarden.getfruit (edit1.text); AFRUIT.GROW (); Except on fruitError: Exception do showMessage (fruitError.MESSAGE);

Freeandnil (AfRuitGarden);

转载请注明原文地址:https://www.9cbs.com/read-48766.html

New Post(0)