Interface delegation [SOLVED] lazarus / 61-Beginners ---------------------------------------------------------------------- pascal111 · 2021-05-28 15:37 ما معنى الـ  Interface delegation أو ما هي فكرته أو فائدته فلقد ترجمتُ هذه الوثيقة https://www.freepascal.org/docs- html/current/ref/refse48.html#x101-1250007.5 ولم تصلني الفكرة بعد؟ google translate: "What is the meaning of Interface delegation or what is its idea or its usefulness I have translated this document https://www.freepascal.org/docs- html/current/ref/refse48.html#x101-1250007.5 The idea did not reach me yet?" ---------------------------------------------------------------------- jamie · 2021-05-29 00:26 I see that no one answered you on this one... I am not a interface expert but I can maybe shed some light on it.. looking at the reference url you pointed out, the Delegate is like a remote class that implements the interface + methods and the "implements" instructs the compiler what interfaces it has.  Basically its just a property pointer to another class interface..  in the end, you still need to create the actual method bodies for P1 and P2 and in this case it would be for the TDelegateClass ---------------------------------------------------------------------- pascal111 · 2021-05-29 12:10 Quote from: jamie on May 29, 2021, 02:26:40 amI see that no one answered you on this one... I am not a interface expert but I can maybe shed some light on it.. looking at the reference url you pointed out, the Delegate is like a remote class that implements the interface + methods and the "implements" instructs the compiler what interfaces it has.  Basically its just a property pointer to another class interface..  in the end, you still need to create the actual method bodies for P1 and P2 and in this case it would be for the TDelegateClass أشكُركـ على مُحاولتكـ! تخيّل أنّني أُريد شرح الـ Interface delegation للمبرمجين المُبتدئين السعوديين وأُريدهم أنْ يفهموا الموضوع بالضبط كما هو كي يستطيعوا تطبيقه فيما يُريدون ولكنّني للأسف لستُ أفهم الموضوع بالضبط فكيف سأشرحه وكيف سيُنشئون برامجهم ،بالتالي سيُضطرون للإعتماد على مصادر مثلاً السوريين والتي لن تُعطيهم فهم الموضوع فيُضطروا للإعتماد على السوريين في إنشاء البرامج بالطريقة التي يُقررها السوريون ﻷنّ السوريون أخذوا المعلومات الصحيحة من المصادر الأمريكيّة ومنعوها عن السعوديين ليجعلوا السعوديين دائماً في حاجةٍ إليهم. google translate: "Thank you for "your" trying! Imagine that I want to explain the Interface delegation to the novice Saudi programmers and I want them to understand the topic exactly as it is so that they can apply it in what they want, but unfortunately I do not understand the exact topic, how will I explain it and how they will create their programs, thus they will have to rely on sources such as the Syrians, which will not give them an understanding of the subject, so the Syrians"Sadui people" will have to rely on them, in creating programs in the manner decided by the Syrians, because the Syrians took the correct information from American sources and prevented it from the Saudis in order to make the Saudis always in need of them." ---------------------------------------------------------------------- jamie · 2021-05-29 13:00 interfaces is the closes you will come to having multiple inheritance for classes, at least for the object pascal language as it stands now. with interfaces you can chain a series of interfaces within a class definition that are all accessible at the same inheritance level. Interfaces work very much the same way as does classes but are handled differently in the background. one note with interfaces is that methods that are declared in the interface are like "Abstract" declared methods in a class, meaning you don't need to implement the actual body of the procedure and the compiler will allow you to get away with it.   But in short the term Delegate in this case would be like having a TBitmap as a property and field within a class, just think of the Tbitmap as a interface elsewhere but its really a class , etc.. There are two different types supported too.. ---------------------------------------------------------------------- pascal111 · 2021-05-29 13:34 Quote from: jamie on May 29, 2021, 03:00:27 pminterfaces is the closes you will come to having multiple inheritance for classes, at least for the object pascal language as it stands now. with interfaces you can chain a series of interfaces within a class definition that are all accessible at the same inheritance level. Interfaces work very much the same way as does classes but are handled differently in the background. one note with interfaces is that methods that are declared in the interface are like "Abstract" declared methods in a class, meaning you don't need to implement the actual body of the procedure and the compiler will allow you to get away with it.   But in short the term Delegate in this case would be like having a TBitmap as a property and field within a class, just think of the Tbitmap as a interface elsewhere but its really a class , etc.. There are two different types supported too.. إلى الآن لم تقُل شيئاً سيّئا ولكن المعلومات الجيّدة التي تقولها لا تتناسب مع الإنطباع الذي يؤخذ عنكـ ،ﻷنّ الصورة التي تؤخذ عنكـ أقل قدراً من المعرفة التي تتكلّم بها مثل أرقام الموبايل الخاطئة. google translate: "So far "For now", you have not said anything bad, but the good information you say does not match the impression that is taken on you, because the picture that is taken of you is less than the knowledge that you speak, such as the wrong mobile numbers." ---------------------------------------------------------------------- Handoko · 2021-05-29 13:59 Quote from: pascal111 on May 29, 2021, 02:10:26 pmImagine that I want to explain the Interface delegation to the novice Saudi programmers ... Novice programmers should not learn interface. Many skilled programmers even have problems understanding what/when/why interface is needed. jamie mentioned 2 things: multiple inheritance and abstract methods. So I believe if you want to understand what interface delegation is, you first need to understand what multiple inheritance and abstract methods are. ---------------------------------------------------------------------- jamie · 2021-05-29 14:01 I guess we have a language barrier problem here. I can not think of any other way to explain it to you.. I use them for multiple inheritance and I also use the COM model over the corba at times. other than using them for multiple inheritance effects I don't see any other use for them other than accessing the COM models from the OS or some libs creating in MS tools..  Ok, have fun trying to unravel the mystery on your side. ---------------------------------------------------------------------- Handoko · 2021-05-29 14:10 Sorry to hijack the discussion. Multiple inheritance. I know what it is but I haven't use it. Is there any good example case when using multiple inheritance is better than single inheritance? Or is there any case that using multiple inheritance cannot be solved using single inheritance? ---------------------------------------------------------------------- lucamar · 2021-05-29 14:30 Actually, it's relatively easy to explain. Suppose someone calls you and asks you to repair a leaky faucet. You don't have any idea how that's done but your brother happens to be a plumber, so you say: "yeah, no problem, I'll take care" and you call your brother to do it. Now you get the fame of (or advertise as) being able to repair faucets apart from your normal work as a programmer but the actual work is done by another; i.e. you "implement" the plumber interface by delegating it to an instance of your "brother". Basicaly that's it. ---------------------------------------------------------------------- Handoko · 2021-05-29 14:38 Yes, I got the basic idea now. Simple but good explanation. But why don't we design the classes carefully before writing the code. If I planned my future carefully, I should have studied plumbing so I now can repair the leaky faucet. ---------------------------------------------------------------------- lucamar · 2021-05-29 14:49 Quote from: Handoko on May 29, 2021, 04:38:12 pmBut why don't we design the classes carefully before writing the code. Imagine there is already a class implementing the interface in a perfectly acceptable way w.r.t. the needs of the new class but you can't justify descending from it (e.g. because you need to descend from another). Interfaces and delegation are the solution to this: your class says it "implements" some interface but the actual job is done by that first class code. Taking interfaces into account, and using them where and as appropiate is actually considered "careful design". Whenever you'd find yourself thinking: "this would be way easier if we had multiple inheritance ...", you can now confidently go back to the design table and carefully lay out some interfaces ---------------------------------------------------------------------- zamronypj · 2021-05-29 15:11 Quote from: pascal111 on May 28, 2021, 05:37:12 pmما معنى الـ  Interface delegation أو ما هي فكرته أو فائدته فلقد ترجمتُ هذه الوثيقة https://www.freepascal.org/docs- html/current/ref/refse48.html#x101-1250007.5 ولم تصلني الفكرة بعد؟ google translate: "What is the meaning of Interface delegation or what is its idea or its usefulness I have translated this document https://www.freepascal.org/docs- html/current/ref/refse48.html#x101-1250007.5 The idea did not reach me yet?" Interface delegation is used if you want a concrete class to support several interfaces but you do not want to implement all interfaces in one class because  it may cause class become complex and do many things. To make class simple and avoid doing many things, you delegate to an external implementation. example scenario: you want to make a pencil case product which has ability to charge your phone.  This multifunctional product can act as IPencilCase or as a IPowerBank interface. You can make all powerbank electronic circuitry+batteries in this product all by yourself or you can just buy third party ready-made powerbank and embed it in pencil case (delegate powerbank functionality to external powerbank). The latter approach makes your pencil case simple in design while providing more features. ---------------------------------------------------------------------- Handoko · 2021-05-29 15:48 Thank you zamronypj, lucamar and jamie for the clear explanations. I am sure know why interface and interface delegation can be useful: - adding features from other's ready made classes - for example COM objects (I know what COM object is) - combining the features from interfaces I do not see any usage to me now but I'm sure I will use it someday. And I won't forget to say thank you to pascal111 for bringging up this topic. ---------------------------------------------------------------------- pascal111 · 2021-05-29 15:54 Quote from: jamie on May 29, 2021, 04:01:02 pmI guess we have a language barrier problem here. I can not think of any other way to explain it to you.. I use them for multiple inheritance and I also use the COM model over the corba at times. other than using them for multiple inheritance effects I don't see any other use for them other than accessing the COM models from the OS or some libs creating in MS tools..  Ok, have fun trying to unravel the mystery on your side. لا أدري إن كانت المُشكلة في الإنجليزيّة أم العربيّة لكن عموماً الأمر أشبه باستيراد المزيد من الوظائف من الأصناف الأخرى عن طريق الـ Interfaces أي أنّ الصنف الفعلي لا يحتوي على المميزات المعروضة وإمكانيّاته تختلف عمّا هو معروض ،ربّما تَقصد أن تقول ذلكـ. google translate: "I do not know if the problem is in English or Arabic, but in general it is like importing more jobs "functions" from other types "classes" through the Interfaces, meaning that the actual class does not contain the features offered and its capabilities differ from what is presented, you may mean to say that." ---------------------------------------------------------------------- pascal111 · 2021-05-29 16:00 Quote from: Handoko on May 29, 2021, 05:48:35 pmAnd I won't forget to say thank you to pascal111 for bringging up this topic. أُحاولُ دائماً البحث عن الكنوز ،boulder dash كانت من أوائل الألعاب التي لعبتها في طفولتي على الكمبيوتر المنزلي google translate: "I always try to hunt for treasures, boulder dash was one of the first games I played as a kid on my home computer " ---------------------------------------------------------------------- pascal111 · 2021-05-29 16:01 Quote from: zamronypj on May 29, 2021, 05:11:34 pmQuote from: pascal111 on May 28, 2021, 05:37:12 pmما معنى الـ  Interface delegation أو ما هي فكرته أو فائدته فلقد ترجمتُ هذه الوثيقة https://www.freepascal.org/docs- html/current/ref/refse48.html#x101-1250007.5 ولم تصلني الفكرة بعد؟ google translate: "What is the meaning of Interface delegation or what is its idea or its usefulness I have translated this document https://www.freepascal.org/docs- html/current/ref/refse48.html#x101-1250007.5 The idea did not reach me yet?" Interface delegation is used if you want a concrete class to support several interfaces but you do not want to implement all interfaces in one class because  it may cause class become complex and do many things. To make class simple and avoid doing many things, you delegate to an external implementation. example scenario: you want to make a pencil case product which has ability to charge your phone.  This multifunctional product can act as IPencilCase or as a IPowerBank interface. You can make all powerbank electronic circuitry+batteries in this product all by yourself or you can just buy third party ready-made powerbank and embed it in pencil case (delegate powerbank functionality to external powerbank). The latter approach makes your pencil case simple in design while providing more features. أظنّني فهمتُ الآن. google translate: "I guess I now understand." ---------------------------------------------------------------------- lucamar · 2021-05-29 16:03 Quote from: Handoko on May 29, 2021, 05:48:35 pmI am sure know why interface and interface delegation can be useful: - adding features from other's ready made classes - for example COM objects (I know what COM object is) - combining the features from interfaces I do not see any usage to me now but I'm sure I will use it someday. Note that some of those can be better accomplished by object composition, which is probably why interfaces are not that much used in Pascal: most times an abstract base class with a default implementation one (like e.g. TStrings/TStringList) and composition do the work nicely. ---------------------------------------------------------------------- jamie · 2021-05-29 16:22 I just put together a simple demo using a form as the show piece. two buttons to show how you can select the interface instance you want to use where each procedure of the instance can have a totally different meaning... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;{$interfaces corba}typeIMyinterface = interface  procedure crap;end;IDelegate1 = Class(TinterfacedObject, ImyInterface)  procedure crap;end;IDelegate2 = Class(TInterfacedObject,ImyInterface)  procedure Crap;end;  { TForm1 }  TForm1 = class(TForm)    Button1: TButton;    Button2: TButton;    procedure Button1Click(Sender: TObject);    procedure Button2Click(Sender: TObject);    procedure FormCreate(Sender: TObject);    procedure FormDestroy(Sender: TObject);  private    fDelegator :ImyInterface;    fFirst :IDelegate1;    fSecond:IDelegate2;  public  end; var  Form1: TForm1; implementation {$R *.lfm} procedure TForm1.Button1Click(Sender: TObject);begin  fDelegator := ImyInterface(fFirst);  fdelegator.crap;end; procedure TForm1.Button2Click(Sender: TObject);begin  fDelegator := ImyInterface(fSecond);  fdelegator.Crap;end; procedure TForm1.FormCreate(Sender: TObject);begin fFirst := Idelegate1.Create; fsecond:= iDelegate2.Create;end; procedure TForm1.FormDestroy(Sender: TObject);begin  fFirst.Free;  fSecond.Free;end; Procedure IDelegate1.Crap;Begin  Form1.Caption := 'First Delegate used';End;Procedure IDelegate2.Crap;Begin  Form1.Caption := 'Second Delegate used';end;end.   here you can see where all you need to do is simply set a single delegator proxy field and all existing code that accesses the delegator needs not to know the difference.  With this in mind, you can have a set of procedures all named the same all taking the same parameters but each method could have totally different code in them.   Not to get confused with Generics where the types can be different but with the same code, here you can really mix it up! ---------------------------------------------------------------------- Handoko · 2021-05-29 16:55 @lucamar Understood. What I'm thinking is, I ever wrote a simple pdf viewer for my friend so he can preview, print, browse and manage the pdf files into categories. I used ActiveXContainer to 'import' Acrobat Reader 7 into the program. That worked and still working good. I wondered how that can be done into Lazarus so I looked into the source code. It used a lot of interface 'things'. So, if I am good in using interface, I will able to, not simply 'import' but better control the objects. @jamie Thank you for the demo. It show the usage of interface delegation and the "like abstract method" you mentioned. It is a bit hard for me to 'digest' for now. I believe I can fully understand it when I really use it into my code in the future.