À̹ø ½Ã°£Àº Observer ÆÐÅÏ¿¡ ´ëÇØ ¼³¸íµå¸®°Ú½À´Ï´Ù.
¸ÕÀú "Observer" ¶ó´Â ´Ü¾î¸¦ ¿µ¾î»çÀü¿¡¼ ã¾Æ º¸¸é ¾Æ·¡¿Í °°½À´Ï´Ù.
Observer - º¸´Â »ç¶÷, ¸ñ°ÝÀÚ, °üÂûÀÚ. Á¤Âûº´, Âü°üÀÎ
ÀÌ·± ¿©·¯°¡Áö ¸»ÀÌ ³ª¿À´Âµ¥ µðÀÚÀÎÆÐÅÏ¿¡¼ Åë¿ëµÇ´Â °¡Àå ÀûÇÕÇÑ
Çؼ®Àº ¹Ù·Î "°üÂûÀÚ" ¶Ç´Â "Á¤Âûº´" ÀÔ´Ï´Ù.
ÀÌ °üÂûÀÚÀÇ ÇÙ½ÉÀûÀÎ ¿ªÈ°Àº ´ÙÀ½°ú °°½À´Ï´Ù.
1. ¾î¶² »óȲÀÌ ¹ß»ýÇÏ´ÂÁö Àß °üÂûÇÏ´Â °Í
2. ƯÁ¤»óȲÀÌ ¹ß»ýÇÏ¸é ±× »ç½ÇÀ» ²À ¾Ë¾Æ¾ß ÇÏ´Â ´Ù¸¥ ´ë»óµé¿¡°Ô ÀüÆÄÇØ ÁÖ´Â °Í
ÀÌ·± °³³äÀÌ °³¹ß¿¡µµ ²À ÇÊ¿äÇÑ °æ¿ì°¡ ÀÖ½À´Ï´Ù.
äÆÃÇÁ·Î±×·¥ÀÌ Observe ÆÐÅÏÀ» ¾²´Â °¡Àå ÁÁÀº ¿¹ÀÔ´Ï´Ù. äÆù濡 10¸íÀÇ ´ëÈÀÚ°¡
Âü°¡ÁßÀε¥ ÀÌÁß¿¡ ´©±¸µç ±ÛÀ» ¾²¸é ´Ù¸¥ ´ëÈÀڵ鵵 ±× ³»¿ëÀ» º¼¼ö ÀÖ¾î¾ß Çϱ⠶§¹®ÀÔ´Ï´Ù.
Observe ÆÐÅÏÀÇ TObserve Ŭ·¡½ºÀÇ Çٽɱâ´ÉÀº ´ÙÀ½°ú °°½À´Ï´Ù.
1. TObserver Ŭ·¡½º´Â ÀüÆÄ´ë»óµéÀ» TList ¿¡ º¸°üÇÑ´Ù.
2. ¾î¶² »óȲÀÌ ÀϾ¸é TList ¾È¿¡ º¸°üµÈ ÀüÆÄ´ë»óÀÇ Æ¯Á¤ À̺¥Æ®¸¦ ¹ß»ý½ÃÄÑ ÁØ´Ù.
±×·³ ¾Æ·¡ÀÇ °£´ÜÇÑ ¿¹Á¦¸¦ º¸µµ·Ï ÇÏ°Ú½À´Ï´Ù. [¿¹Á¦´Â ÷ºÎÆÄÀÏ¿¡ ÀÖ½À´Ï´Ù.]
¾Æ·¡ ¿¹Á¦´Â ¾î¶² TObsv Ŭ·¡½ºÀÇ ÀνºÅϽº°¡ ÀüÆÄ´ë»óµéÀ» º¸°üÇÏ°í ÀÖ´Ù°¡
±× ´ë»óµé¿¡°Ô ƯÁ¤À̺¥Æ®¸¦ Åë°íÇØ ÁÖ´Â ¿ªÈ°À» ÇÕ´Ï´Ù.
Âü°í·Î TObsv ¶ó´Â Ŭ·¡½º ¸íĪÀ» ¿ø·¡´Â TObserver ¶ó°í ¾²°í ½Í¾ú´Âµ¥, ÀÌ·¸°Ô ¾²´Ï±ñ
Windows Defender °¡ ³» Å×½ºÆ® ÇÁ·Î±×·¥À» ¹ÙÀÌ·¯½º·Î ¿ÀÀÎÇÏ´Â ¾îó±¸´Ï ¾ø´Â »óȲÀÌ ¹ß»ýÇÏ¿´½À´Ï´Ù.
ÀÌ ¹®Á¦¸¦ ÇØ°áÇϱâ À§ÇØ Å¬·¡½º À̸§À» TObsv ¶ó°í ¹Û¿¡ ÇÒ ¼ö ¾ø¾úÀ½À» ¾çÇعٶø´Ï´Ù.
¾Æ¸¶µµ ¹ÙÀÌ·¯½º ÇÁ·Î±×·¥ÀÇ Å¬·¡½º ¸íĪµéÀÌ Observer ·Î µÇ¾î ÀÖ´Â °æ¿ì°¡ ¸¹Àº ¸ð¾çÀÔ´Ï´Ù.
Âü°íµé ÇϽñ⠹ٶø´Ï´Ù.
unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls;
type
TObsv = class(TObject) private FBtns : TList; //ÀüÆÄ ´ë»óÀÚµéÀ» TList ¿¡ º¸°ü protected procedure SpreadEventToBtns; //ÀüÆÄ´ë»óÀÚµéÀÇ Æ¯Á¤ À̺¥Æ® ¹ß»ý ½ÃÄÑÁÜ. public constructor Create; destructor Destroy;override; procedure RegBtn( aBtn : TButton); //ÀüÆÄ ´ë»óÀÚ Ãß°¡ procedure UnRegBtn( aBtn : TButton);//ÀüÆÄ ´ë»óÀÚ »èÁ¦ end;
TForm1 = class(TForm) btnObsv: TButton; btn1: TButton; btn2: TButton; Bevel1: TBevel; Bevel2: TBevel; Bevel3: TBevel; Bevel4: TBevel; Bevel5: TBevel; btn3: TButton; edtText: TEdit; procedure FormDestroy(Sender: TObject); procedure btnObsvClick(Sender: TObject); procedure btn1Click(Sender: TObject); procedure btn2Click(Sender: TObject); procedure btn3Click(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end;
var Form1: TForm1;
implementation
var sObsv : TObsv;
{$R *.dfm}
{ TObsv }
constructor TObsv.Create; begin FBtns := TList.Create; end;
destructor TObsv.Destroy; begin FBtns.Free; inherited; end;
procedure TObsv.RegBtn( aBtn : TButton ); begin if FBtns.IndexOf(aBtn) = -1 then FBtns.Add( aBtn ); end;
procedure TObsv.UnRegBtn( aBtn : TButton ); begin FBtns.Remove(aBtn); end;
procedure TObsv.SpreadEventToBtns;//<= À̺¥Æ® ÀüÆÄ [ÀÌ ¸Þ¼µå°¡ Observer ÆÐÅÏÀÇ ÇÙ½ÉÀÔ´Ï´Ù. ] var i: Integer; aBtn: TButton; begin for i := 0 to FBtns.Count - 1 do begin aBtn := FBtns[i]; aBtn.OnClick(self); end; end;
{Form}
procedure TForm1.FormCreate(Sender: TObject); begin sObsv := TObsv.Create; sObsv.RegBtn( btn1 ); sObsv.RegBtn( btn2 ); sObsv.RegBtn( btn3 ); end;
procedure TForm1.FormDestroy(Sender: TObject); begin sObsv.Free; end;
procedure TForm1.btnObsvClick(Sender: TObject); begin sObsv.SpreadEventToBtns; end;
procedure TForm1.btn1Click(Sender: TObject); begin btn1.Caption := edtText.Text + 'ÀÇ ÇÊÅë'; end;
procedure TForm1.btn2Click(Sender: TObject); begin btn2.Caption := edtText.Text + 'ÀÇ ¿¬ÇÊ'; end;
procedure TForm1.btn3Click(Sender: TObject); begin btn3.Caption := edtText.Text + 'ÀÇ Áö¿ì°³'; end;
end.
³»¿ëÀÌ Á» ºÎ½ÇÇÑ °Í °°±â Çϳª ½Ç·ÂÀÌ ºÎÁ·ÇÑ Á¡ ¾çÇØ ºÎŹµå¸®¸ç,
ÀÌ»óÀ¸·Î Observer ÆÐÅÏ °Á¸¦ ¸¶Ä§´Ï´Ù.
Ȥ½Ã¶óµµ ³»¿ë¿¡ ¹®Á¦µÇ´Â Á¡ÀÌ ÀÖÀ¸¸é ¾ðÁ¦µç ¸»¾¸ÇØ Áֽñ⠹ٶø´Ï´Ù.
|