{"id":168,"date":"2019-09-30T22:22:16","date_gmt":"2019-09-30T19:22:16","guid":{"rendered":"http:\/\/www.potansif.com\/?p=168"},"modified":"2019-10-01T17:26:38","modified_gmt":"2019-10-01T14:26:38","slug":"interposer-class-nedir-nasil-kullanilir","status":"publish","type":"post","link":"http:\/\/www.potansif.com\/?p=168","title":{"rendered":"Interposer Class Nedir, Nas\u0131l Kullan\u0131l\u0131r"},"content":{"rendered":"<p>Interposer kelimesinin kar\u015f\u0131l\u0131\u011f\u0131 arac\u0131, arac\u0131l\u0131k yapan demektir. Yani ba\u015fl\u0131k asl\u0131nda &#8220;Arac\u0131 S\u0131n\u0131f Nedir&#8221; \u015feklinde de yaz\u0131labilirdi.<br \/>\n<!--more--><br \/>\nBir Interposer S\u0131n\u0131f\u0131, (hiyerar\u015fik anlamda konu\u015facaksak) bir s\u0131n\u0131f\u0131n (kendisinden t\u00fcredi\u011fi) bir ata s\u0131n\u0131f ile bu s\u0131n\u0131f\u0131n t\u00fcr\u00fcne ait bir nesnenin beyan\u0131 aras\u0131nda konumland\u0131r\u0131l\u0131\u015f bir &#8220;Arac\u0131 s\u0131n\u0131ft\u0131r&#8221;. Ana karakteristi\u011fi, ata s\u0131n\u0131f ile ayn\u0131 isme sahip olmas\u0131d\u0131r. Bu sayede uygulamalarda ata s\u0131n\u0131f\u0131n kullan\u0131ld\u0131\u011f\u0131 durumlarda s\u00fcreci basitle\u015ftirmek hedeflenmi\u015ftir. Bu teknik asl\u0131nda 1998&#8217;den beri var ve Delphi ile yeteri kadar ha\u015f\u0131rne\u015fir olanlar\u0131n \u00e7ok s\u0131k kulland\u0131\u011f\u0131 bir teknik diyebilirim.<\/p>\n<p>Bunu bir hikaye \u00fczerinden a\u00e7\u0131klamaya \u00e7al\u0131\u015fay\u0131m; Mesela, diyelimki bir uygulaman\u0131z var ve bu uygulaman\u0131z\u0131n bir \u00e7ok yerinde TPanel nesnesini kullanm\u0131\u015fs\u0131n\u0131z. Bir vakit sonra sizden istisnas\u0131z, bu uygulamadaki t\u00fcm panellerin sa\u011f \u00fcst k\u00f6\u015fesine bir kapatma butonu koyman\u0131z isteniyor ve size deniliyor ki; &#8220;Karde\u015f, biz bu kapatma butonunu koyuyoruz ama kullan\u0131c\u0131lar bunu be\u011fenmezse bu talepten vaz da ge\u00e7ebiliriz&#8221; diyorlar&#8230; Al bakal\u0131m, sakal m\u0131 b\u0131y\u0131k m\u0131! Bu problemi nas\u0131l \u00e7\u00f6zeriz?<\/p>\n<p>Normalde b\u00f6yle bir durumda tasar\u0131m ekran\u0131n\u0131 a\u00e7\u0131p t\u00fcm TPanel nesnelerinin sa\u011f \u00fcst k\u00f6\u015fesine tek tek buton ekler ve i\u00e7ine de ilgili kodu yazars\u0131n\u0131z. Peki, g\u00fczel&#8230; Ya projenizde form say\u0131s\u0131 onlarca, hatta y\u00fczlerceyse, o zaman ne olacak? T\u00fcm bu TPanellere ayn\u0131 i\u015flemi tek tek yapmak bariz bir \u015fekilde hamall\u0131k de\u011fil de nedir?<\/p>\n<p>Baz\u0131 arkada\u015flar &#8220;Class Helper&#8221; tekni\u011fine ba\u015fvurmak isteyebilir ama hepimiz biliyoruz ki S\u0131n\u0131f Yard\u0131mc\u0131lar\u0131 sadece yard\u0131m ettikleri s\u0131n\u0131f\u0131n yerel de\u011fi\u015fkenleri ve property&#8217;leri \u00fczerinde i\u015flem yapabiliyor, as\u0131l nesnenin private b\u00f6l\u00fcm\u00fcndeki de\u011fi\u015fkenlere eri\u015fimde s\u0131k\u0131nt\u0131 ya\u015fat\u0131yor. Ayr\u0131ca Class Helper&#8217;larda yeni bir de\u011fi\u015fken tan\u0131mlayamazs\u0131n\u0131z as\u0131l s\u0131n\u0131f\u0131n mevcudunda olanlar\u0131 kullanmak durumundas\u0131n\u0131z. Interposer Class ile Class Helper aras\u0131ndaki bu temel fark sizin Class Helper veya Interposer Class aras\u0131ndaki tercihinizi etkiler&#8230; Dolay\u0131s\u0131yla \u015fimdiki senaryoya uymayan bir durum oldu\u011fu i\u00e7in bu noktada class helper tekni\u011fi kullan\u0131\u015fl\u0131 bir se\u00e7enek de\u011fildir&#8230;<\/p>\n<p>Di\u011ferleri de &#8220;TPanel&#8217;den t\u00fcretilmi\u015f ba\u015fka bir tip tan\u0131mlar\u0131z&#8221; diyebilir, yine de t\u00fcm formlar\u0131 tek tek gezmeniz ve ilgili e\u015fle\u015fmeleri tek tek d\u00fczeltmeniz gerekir. Yani sonu\u00e7 olarak yine d\u00f6n\u00fcp dola\u015f\u0131p bu hamall\u0131k sekans\u0131na geri geliyoruz&#8230;<\/p>\n<p>Bunun daha basit bir \u00e7\u00f6z\u00fcm\u00fc yok mu? Yani bu hamall\u0131ktan kurtulamayacak m\u0131y\u0131z? Hamall\u0131ktan kurtulu\u015f yok ama y\u00fck\u00fcm\u00fcz\u00fc buzdolab\u0131 \u00f6l\u00e7e\u011finden, aspirin kutusu \u00f6l\u00e7e\u011fine kadar k\u00fc\u00e7\u00fcltebiliriz&#8230;<\/p>\n<p>\u0130\u015fte bunun gibi durumlarda sizden &#8220;YAPISAL OLARAK&#8221; mevcut bir \u015feyi de\u011fi\u015ftirmeniz isteniyorsa bu noktada devreye Interposer s\u0131n\u0131flar giriyor. Peki bu Interposer S\u0131n\u0131f denen \u015fey tam olarak nedir? Cevap basit: Asl\u0131nda, s\u0131n\u0131f\u0131n kendisini yine kendisinden t\u00fcretmektir diye \u00f6zetleyebiliriz.<\/p>\n<p>Yukar\u0131daki senaryoyu ele alacak olursak yine o \u00e7er\u00e7evede bir \u00f6rneklendirme yapal\u0131m.<\/p>\n<p>Yapaca\u011f\u0131m\u0131z \u015fey \u00e7ok basit, projemizdeki t\u00fcm TPanel nesnelerinin sa\u011f \u00fcst k\u00f6\u015fesine bir buton koyaca\u011f\u0131z ve bu butona bas\u0131ld\u0131\u011f\u0131nda TPanel nesnesi kendisini imha edecek. Normalde b\u00f6yle bir \u00f6zellik TPanel nesnesinde yok ve bizden bu \u00f6zellik projedeki t\u00fcm TPanel nesnelerinde olsun isteniyor&#8230;<\/p>\n<p>Bunun i\u00e7in ad\u0131 InterposerObjects adl\u0131 bo\u015f bir unit olu\u015fturuyoruz ve i\u00e7ini a\u015fa\u011f\u0131daki gibi dolduruyoruz. A\u00e7\u0131klamalar ise kod i\u00e7inde devam ediyor;<\/p>\n<pre><code>unit InterposerObjects;\r\n\r\ninterface\r\n\r\nuses\r\n&nbsp; &nbsp;Vcl.StdCtrls &nbsp; &nbsp;\/\/ &nbsp;TButton\r\n&nbsp;, System.Classes &nbsp;\/\/ &nbsp;TComponent\r\n&nbsp;, Vcl.Controls &nbsp; &nbsp;\/\/ &nbsp;Anchors\r\n&nbsp;, Vcl.ExtCtrls &nbsp; &nbsp;\/\/ &nbsp;TPanel normalde bu unit'te tan\u0131ml\u0131d\u0131r. Biz bu unitteki TPanel'e yeni bir \u00f6zellik ekleyece\u011fiz a\u015fa\u011f\u0131da...\r\n&nbsp;;\r\n\r\ntype\r\n&nbsp;TPanel = class(Vcl.ExtCtrls.TPanel) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ Bu bizim INTERPOSER s\u0131n\u0131f\u0131m\u0131z oluyor.\r\n&nbsp; &nbsp;private\r\n&nbsp; &nbsp; &nbsp;FButon: TButton; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ Ger\u00e7ek s\u0131n\u0131fta olmayan bir buton nesnemiz var\r\n&nbsp; &nbsp;protected\r\n&nbsp; &nbsp; &nbsp;procedure PaneliYokEt(Sender: TObject); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ Yine Ger\u00e7ek s\u0131n\u0131fta olmayan bir olay tan\u0131ml\u0131yoruz.\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ Bunun g\u00f6revi paneli yok etmek olacak...\r\n&nbsp; &nbsp;public\r\n&nbsp; &nbsp; &nbsp;constructor Create(AOwner: TComponent); override; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ Butonumuzu bu noktada olu\u015fturuyoruz...\r\n&nbsp; &nbsp; &nbsp;destructor Destroy; override; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ Butonumuzu bu noktada yok ediyoruz...\r\n&nbsp;end;\r\n\r\nimplementation\r\n\r\nuses\r\n&nbsp; &nbsp;System.SysUtils\r\n&nbsp;, Vcl.Dialogs\r\n&nbsp;;\r\n\r\n{ TPanel }\r\n\r\nconstructor TPanel.Create(AOwner: TComponent);\r\nbegin\r\n&nbsp;\/\/ Bu k\u0131s\u0131m inheritance \/ miras alma yoluyla klasik TPanel'de ne varsa onu zaten \u00fcretiyor.\r\n&nbsp;inherited Create(aOwner);\r\n\r\n\r\n&nbsp;\/\/ Bu noktadan sonras\u0131nda ise geli\u015ftirdi\u011fimiz s\u0131n\u0131f\u0131m\u0131zda yeni butonumuzu yerle\u015fik olarak olu\u015fturuyoruz\r\n&nbsp;FButon := TButton.Create(Self);\r\n&nbsp;with &nbsp;FButon do begin\r\n&nbsp; &nbsp; &nbsp; &nbsp;Parent &nbsp;:= Self;\r\n&nbsp; &nbsp; &nbsp; &nbsp;Width &nbsp; := 20;\r\n&nbsp; &nbsp; &nbsp; &nbsp;Height &nbsp;:= 20;\r\n&nbsp; &nbsp; &nbsp; &nbsp;Caption := 'xx';\r\n&nbsp; &nbsp; &nbsp; &nbsp;Top &nbsp; &nbsp; := 0;\r\n&nbsp; &nbsp; &nbsp; &nbsp;Left &nbsp; &nbsp;:= Self.Width - 20;\r\n&nbsp; &nbsp; &nbsp; &nbsp;Anchors := [akTop, akRight];\r\n&nbsp; &nbsp; &nbsp; &nbsp;OnClick := PaneliYokEt;\r\n&nbsp;end;\r\nend;\r\n\r\ndestructor TPanel.Destroy;\r\nbegin\r\n&nbsp;\/\/ \u00d6nce yeni butonumuzu yok ediyoruz\r\n&nbsp;FreeAndNil(FButon);\r\n\r\n\r\n&nbsp;\/\/ sonra yine miras yoluyla klasik yok etme prosed\u00fcr\u00fcn\u00fc \u00e7a\u011f\u0131r\u0131yoruz...\r\n&nbsp;inherited Destroy;\r\nend;\r\n\r\nprocedure TPanel.PaneliYokEt(Sender: TObject);\r\nbegin\r\n&nbsp;ShowMessage('INTERPOSER SINIF arac\u0131l\u0131\u011f\u0131yla b\u00f6yle bir yetenek kazand\u0131rm\u0131\u015f olduk...');\r\n&nbsp;Destroy; \/\/ ve TPANEL kendi kendini yok eder...\r\nend;\r\n\r\nend.<\/code><\/pre>\n<p>Bu noktadan sonra ise yapaca\u011f\u0131m\u0131z tek bir \u015fey var. Bu uniti projedeki t\u00fcm pas dosyalar\u0131n\u0131n INTERFACE b\u00f6l\u00fcm\u00fcne eklemek olacak. Yaln\u0131z burada dikkat etmemiz gereken bir nokta var o da \u015fu, normalde TPanel &#8220;Vcl.ExtCtrls.pas&#8221; dosyas\u0131nda tan\u0131ml\u0131d\u0131r, biz ise kendi unitimizi bu unitten hemen sonra eklemeliyiz yoksa yapt\u0131klar\u0131m\u0131z bir i\u015fe yaramaz.<\/p>\n<pre><code>unit Ana_;\r\n\r\ninterface\r\n\r\nuses\r\n&nbsp; &nbsp;Winapi.Windows, Winapi.Messages\r\n&nbsp;, System.SysUtils, System.Variants, System.Classes\r\n&nbsp;, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs\r\n&nbsp;, Vcl.StdCtrls\r\n&nbsp;, Vcl.ExtCtrls &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ TPanel normalde bu unit'te tan\u0131ml\u0131d\u0131r...\r\n&nbsp;, InterposerObjects &nbsp; &nbsp; &nbsp; &nbsp;\/\/ YAPMAMIZ GEREKEN \u0130SE SADECE BU UN\u0130T\u0130 BURAYA EKLEMEKT\u0130R...\r\n&nbsp;;\r\n\r\ntype\r\n&nbsp;TForm2 = class(TForm)\r\n&nbsp; &nbsp;Panel1: TPanel;\r\n&nbsp;private\r\n&nbsp; &nbsp;{ Private declarations }\r\n&nbsp;public\r\n&nbsp; &nbsp;{ Public declarations }\r\n&nbsp;end;\r\n\r\nvar\r\n&nbsp;Form2: TForm2;\r\n\r\nimplementation\r\n\r\n{$R *.dfm}\r\n\r\nend.<\/code><\/pre>\n<p>Tasar\u0131m ekran\u0131nda g\u00f6rmeseniz bile projenizi \u00e7al\u0131\u015ft\u0131rd\u0131\u011f\u0131n\u0131zda g\u00f6rd\u00fc\u011f\u00fcn\u00fcz her TPanel nesnesinin sa\u011f \u00fcst k\u00f6\u015fesinde bir kapatma butonuna sahip oldu\u011funuzu g\u00f6receksiniz.<\/p>\n<p>Faydal\u0131 olmas\u0131 dile\u011fiyle,<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Interposer kelimesinin kar\u015f\u0131l\u0131\u011f\u0131 arac\u0131, arac\u0131l\u0131k yapan demektir. Yani ba\u015fl\u0131k asl\u0131nda &#8220;Arac\u0131 S\u0131n\u0131f Nedir&#8221; \u015feklinde de yaz\u0131labilirdi.<\/p>\n","protected":false},"author":1,"featured_media":266,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[29,4,30,31,11],"class_list":["post-168","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","tag-class","tag-delphi","tag-interposer","tag-oop","tag-programlama"],"_links":{"self":[{"href":"http:\/\/www.potansif.com\/index.php?rest_route=\/wp\/v2\/posts\/168","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.potansif.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.potansif.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.potansif.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.potansif.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=168"}],"version-history":[{"count":1,"href":"http:\/\/www.potansif.com\/index.php?rest_route=\/wp\/v2\/posts\/168\/revisions"}],"predecessor-version":[{"id":169,"href":"http:\/\/www.potansif.com\/index.php?rest_route=\/wp\/v2\/posts\/168\/revisions\/169"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.potansif.com\/index.php?rest_route=\/wp\/v2\/media\/266"}],"wp:attachment":[{"href":"http:\/\/www.potansif.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.potansif.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=168"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.potansif.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}