お知らせ

電子会議

ライブラリ

パレット

Delphi FAQ検索

Delphi FAQ一覧

サンプル蔵





FDelphi FAQ
16番会議室「玉石混淆みんなで作るSample蔵」に寄せられたサンプル

"関連させた複数のコンポを更新"



{ observerパターンのもっともシンプルな実装例 }


 フォーム上の複数のコンポ(Link)をあるひとつのコンポ(Source)に関連
づけして,Sourceのイベントで関連するコンポ全部を一斉に更新する仕組み.

○使い方
・ TFooLink を複数フォーム等に貼り付ける
・ TFooSourceを一個貼り付ける.他のフォームやデータモジュールでも良い
・ FooLinkのSourceプロパティにFooSourceを打ち込むか選んで指定する
・ イベントプロパティの例も一応あります

 FooSource1の NotifyLinks メソッドを呼ぶと,関連づけられている
すべてのFooLinkのUpdateメソッドが呼び出されるのが確認できます.

------------------------------------------------------------------------
unit FooLink;

interface

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

type
  TFooSource = class;

  TFooLink = class(TComponent)
  private
    { Private 宣言 }
    FSource: TFooSource;
    FOnUpdating: TNotifyEvent;
  protected
    { Protected 宣言 }
    procedure SetSource(ASource: TFooSource);
  public
    { Public 宣言 }
    destructor Destroy; override;
    procedure Update;
  published
    { Published 宣言 }
    property Source: TFooSource read FSource write SetSource;
    property OnUpdating: TNotifyEvent
             read FOnUpdating write FOnUpdating;
  end;

  TFooSource = class(TComponent)
  private
    { Private 宣言 }
    FLinks: TList;
  protected
    { Protected 宣言 }
  public
    { Public 宣言 }
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure AddLink(ALink: TFooLink);
    procedure RemoveLink(ALink: TFooLink);
    procedure NotifyLinks;
  published
    { Published 宣言 }
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('TN', [TFooSource,TFooLink]);
end;

destructor TFooLink.Destroy;
begin
  SetSource(nil);
  inherited Destroy;
end;

procedure TFooLink.Update;
begin
  if Assigned(FOnUpdating) then OnUpdating(Self);
  // do as you want to

end;

procedure TFooLink.SetSource(ASource: TFooSource);
begin
  if FSource <> ASource then
  begin
    if FSource <> nil then FSource.RemoveLink(Self);
    if ASource <> nil then ASource.AddLink(Self);
  end;
end;

{ TFooSource }
constructor TFooSource.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FLinks := TList.Create;
end;

destructor TFooSource.Destroy;
begin
  while FLinks.Count > 0 do RemoveLink(FLinks.Last);
  FLinks.Free;
  inherited Destroy;
end;

procedure TFooSource.AddLink(ALink: TFooLink);
begin
  FLinks.Add(ALink);
  ALink.FSource := Self;
end;

procedure TFooSource.RemoveLink(ALink: TFooLink);
begin
  ALink.FSource := nil;
  FLinks.Remove(ALink);
end;

procedure TFooSource.NotifyLinks;
var
  i: Integer;
begin
  for i := 0 to FLinks.Count - 1 do
    TFooLink(FLinks[i]).Update;
end;

end.
----------------------------------------------------------
フォームにLinkを2個貼り付けて,データモジュールにSourceを貼り付けて
実験する例

procedure TForm1.FormClick(Sender: TObject);
begin
  DataModule2.FooSource1.NotifyLinks;  // イベントの発生
end;

procedure TForm1.FooLink1Updating(Sender: TObject);
begin
  MessageBeep(0);
end;

procedure TForm1.FooLink2Updating(Sender: TObject);
begin
  Color := not Color;
end;


                                                  TN(CQJ01721)

Original document by TN            氏 ID:(CQJ01721)


ここにあるドキュメントは NIFTY SERVEの Delphi Users' Forum の16番会議室「玉石混淆みんなで作るSample蔵」に投稿されたサンプルです。これらのサンプルはボーランド株式会社がサポートする公式のものではありません。また、必ずしも動作が検証されているものではありません。これらのサンプルを使用したことに起因するいかなる損害も投稿者、およびフォーラムスタッフはその責めを負いません。使用者のリスクの範疇でご使用下さい。

Copyright 1996-2002 Delphi Users' Forum