Q&A

  • Strategy Pattern 문의
폼위에 radiobutton 2개 버튼 하나 올리고덮어쓰기....
Radiobutton을 선택하고 버튼을 누르면 선택된 Class가 수행되도록 했는데 한두번 되다가 선택시 오류가 나네요.
뭐가 문제인지 모르겠습니다.
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TStrategy = class;

  ITalk = Interface
    procedure execute;
   End;
  TTalk1 = class(TInterfacedObject, ITalk)

    procedure Execute;
  end;
  TTalk2 = class(TInterfacedObject, ITalk)

    procedure Execute;
  end;

  TForm1 = class(TForm)
    Button1: TButton;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    procedure RadioButton1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure RadioButton2Click(Sender: TObject);
  private

  { Private declarations }
  { Public declarations }
  public var
    ///<link>aggregation</link>
    Talk1:TTalk1;
    ///<link>aggregation</link>
    Strategy:TStrategy;
    ///<link>aggregation</link>
    Talk2:TTalk2;
  end;


  TStrategy = class
  public
    function SetTalk(loTalk: ITalk): integer;

  var

    Talk:ITalk;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  Strategy.Talk.execute;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Strategy := TStrategy.Create;
end;

procedure TForm1.RadioButton1Click(Sender: TObject);
begin
  Strategy.SetTalk(Talk1);
end;

procedure TForm1.RadioButton2Click(Sender: TObject);
begin
  Strategy.SetTalk(Talk2)
end;

function TStrategy.SetTalk(loTalk: ITalk): integer;
begin
  Talk := loTalk;
end;

{ TTalk1 }

procedure TTalk1.Execute;
begin
  showMessage('TTalk1');
end;

{ TTalk2 }

procedure TTalk2.Execute;
begin
  ShowMessage('TTalk2');
end;

end.
0  COMMENTS