unit Unit1;

interface

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

type



  TfrmSingleton = class(TForm)
    btTeste1: TButton;
    btTeste2: TButton;
    btTEste3: TButton;
    btTeste4: TButton;
    btTeste6: TButton;
    Label1: TLabel;
    Label2: TLabel;
    btTeste5: TButton;
    Label3: TLabel;
    btTesteDerivado1: TButton;
    btTesteDerivado2: TButton;
    btTesteDerivado3: TButton;
    btTesteDerivado4: TButton;
    btTesteDerivado5: TButton;
    btTesteDerivado6: TButton;
    btTesteDerradeiro: TButton;
    Label4: TLabel;
    btCria1: TButton;
    btCria2: TButton;
    btTesta1: TButton;
    btTesta2: TButton;
    btDestroi1: TButton;
    btDestroi2: TButton;
    procedure btTeste1Click(Sender: TObject);
    procedure btTeste2Click(Sender: TObject);
    procedure btTEste3Click(Sender: TObject);
    procedure btTeste4Click(Sender: TObject);
    procedure btTeste6Click(Sender: TObject);
    procedure btTeste5Click(Sender: TObject);
    procedure btTesteDerivado3Click(Sender: TObject);
    procedure btTesteDerivado4Click(Sender: TObject);
    procedure btTesteDerivado1Click(Sender: TObject);
    procedure btTesteDerivado2Click(Sender: TObject);
    procedure btTesteDerivado5Click(Sender: TObject);
    procedure btTesteDerivado6Click(Sender: TObject);
    procedure btCria1Click(Sender: TObject);
    procedure btCria2Click(Sender: TObject);
    procedure btTesta1Click(Sender: TObject);
    procedure btTesta2Click(Sender: TObject);
    procedure btDestroi1Click(Sender: TObject);
    procedure btDestroi2Click(Sender: TObject);
    procedure btTesteDerradeiroClick(Sender: TObject);
  private
    { Private declarations }
  public
    FMySingleton: TMySingleton;
    FMySingleton1: TMySingleton;
    FMySingleton2: TMySingleton;

    FMySingletonDer1: TMySingletonDerivado;
    FMySingletonDer2: TMySingletonDerivado;

    teste1: TMySingleton;
    teste2: TMySingletonDerivado;
  end;



var
  frmSingleton: TfrmSingleton;

implementation



{$R *.dfm}




procedure TfrmSingleton.btTeste1Click(Sender: TObject);
begin

  FMySingleton1 := TMySingleton.Create;
  with FMySingleton1  do
  begin
    SetHello('teste1');
    SayHello;
  end;

  FMySingleton1.SayHello;

  FMySingleton1.Free;
end;

procedure TfrmSingleton.btTeste2Click(Sender: TObject);
begin
  FMySingleton2 := TMySingleton.Create;
  with FMySingleton2  do
  begin
    //não vamos mudar a mensagem, veremos que é a mesma do teste anterior
    //SetHello('teste2');
    SayHello;
  end;

  FMySingleton2.SayHello;
end;

procedure TfrmSingleton.btTEste3Click(Sender: TObject);
begin
  FMySingleton1 := TMySingleton.Instance;
  FMySingleton1.SayHello;
end;

procedure TfrmSingleton.btTeste4Click(Sender: TObject);
begin
  FMySingleton2 := TMySingleton.Instance;
  FMySingleton2.SayHello;
end;




procedure TfrmSingleton.btTesteDerivado6Click(Sender: TObject);
var fmst: TMySingletonDerivado;
begin

  fmst := TMySingletonDerivado.Create;
  fmst.SetHello('testeA');
  fmst.SayHello;

  //mesmo com várias tentativas de criação e destruição a instância se mantem a mesma

  fmst := TMySingletonDerivado.Create;
  fmst := TMySingletonDerivado.Create;
  fmst := TMySingletonDerivado.Create;

  fmst.SetHello('testeB');
  fmst.SayHello;

  fmst.Free;
  fmst.Free;
  fmst.Free;

  fmst.Destroy;
  fmst.Destroy;
  fmst.Destroy;

  fmst.SetHello('testeC');
  fmst.SayHello;
end;

procedure TfrmSingleton.btTesteDerradeiroClick(Sender: TObject);
begin

  FMySingleton := TMySingleton.Create;
  FMySingleton1 := TMySingleton.Create;
  FMySingleton2 := TMySingleton.Create;

  FMySingleton.SetHello('teste 1');
  FMySingleton1.SetHello('teste 2');
  FMySingleton2.SetHello('teste 3');

  FMySingleton.SayHello;
  FMySingleton1.SayHello;
  FMySingleton2.SayHello;

  FMySingleton.Destroy;
  FMySingleton1.Destroy;
  FMySingleton2.Destroy;

  FMySingleton.SayHello;
  FMySingleton1.SayHello;
  FMySingleton2.SayHello;

  FMySingleton.SetHello('teste 1');
  FMySingleton1.SetHello('teste 2');
  FMySingleton2.SetHello('teste 3');

  FMySingletonDer1 := TMySingletonDerivado.Create;
  FMySingletonDer2 := TMySingletonDerivado.Create;

  FMySingletonDer1.SetHello('teste 4');
  FMySingletonDer2.SetHello('teste 5');

  FMySingletonDer1.SayHello;
  FMySingletonDer2.SayHello;

  FMySingletonDer1.Destroy;
  FMySingletonDer2.Destroy;

  FMySingletonDer1.SayHello;
  FMySingletonDer2.SayHello;


end;

procedure TfrmSingleton.btCria1Click(Sender: TObject);
begin
  teste1 := TMySingleton.Create;
  teste1.SetHello('hello 1');
end;

procedure TfrmSingleton.btCria2Click(Sender: TObject);
begin
  teste2 := TMySingletonDerivado.Create;
  teste2.SetHello('hello 2');
end;

procedure TfrmSingleton.btTesta1Click(Sender: TObject);
begin
    teste1.SayHello;
end;

procedure TfrmSingleton.btTesta2Click(Sender: TObject);
begin
  teste2.SayHello;
end;

procedure TfrmSingleton.btDestroi1Click(Sender: TObject);
begin
  teste1.Free;
end;

procedure TfrmSingleton.btDestroi2Click(Sender: TObject);
begin
  teste2.Free;
end;

procedure TfrmSingleton.btTesteDerivado1Click(Sender: TObject);
begin
  FMySingletonDer1 := TMySingletonDerivado.Create;
  with FMySingletonDer1  do
  begin
    SetHello('teste4');
    SayHello;
  end;

  FMySingletonDer1.SayHello;

  FMySingletonDer1.Free;
end;

procedure TfrmSingleton.btTesteDerivado2Click(Sender: TObject);
begin
  FMySingletonDer2 := TMySingletonDerivado.Create;
  with FMySingletonDer2  do
  begin
    //SetHello('teste5');
    SayHello;
  end;

  FMySingletonDer2.SayHello;

end;

procedure TfrmSingleton.btTesteDerivado3Click(Sender: TObject);
begin
  //FMySingletonDer1 := TMySingletonDerivado.InstanciaPadrao;
  FMySingletonDer1.SayHello;
end;

procedure TfrmSingleton.btTesteDerivado4Click(Sender: TObject);
begin
  //FMySingletonDer2 := TMySingletonDerivado.InstanciaPadrao;
  FMySingletonDer2.SayHello;
end;

procedure TfrmSingleton.btTesteDerivado5Click(Sender: TObject);
begin
  TMySingletonDerivado.Instance.SetHello('teste6');
  TMySingletonDerivado.Create.SayHello;
  TMySingletonDerivado.Instance.SayHello;
end;

procedure TfrmSingleton.btTeste6Click(Sender: TObject);
var fmst: TMySingleton;
begin

  fmst := TMySingleton.Create;
  fmst.SetHello('testeA');
  fmst.SayHello;

  //mesmo com várias tentativas de criação e destruição a instância se mantem a mesma

  fmst := TMySingleton.Create;
  fmst := TMySingleton.Create;
  fmst := TMySingleton.Create;

  fmst.SetHello('testeB');
  fmst.SayHello;

  fmst.Free;
  fmst.Free;
  fmst.Free;

  fmst.Destroy;
  fmst.Destroy;
  fmst.Destroy;

  fmst.SetHello('testeC');
  fmst.SayHello;

end;

procedure TfrmSingleton.btTeste5Click(Sender: TObject);
begin
  TMySingleton.Instance.SetHello('teste3');
  TMySingleton.Create.SayHello;
  TMySingleton.Instance.SayHello;
end;

end.
