Recent

Author Topic: Interface delegation [SOLVED]  (Read 3939 times)

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Interface delegation [SOLVED]
« 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?"
« Last Edit: May 29, 2021, 05:55:17 pm by pascal111 »
La chose par la chose est rappelé.

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: Interface delegation
« Reply #1 on: May 29, 2021, 02:26:40 am »
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




The only true wisdom is knowing you know nothing

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Interface delegation
« Reply #2 on: May 29, 2021, 02:10:26 pm »
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

أشكُركـ على مُحاولتكـ! تخيّل أنّني أُريد شرح الـ 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."
« Last Edit: May 29, 2021, 02:12:26 pm by pascal111 »
La chose par la chose est rappelé.

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: Interface delegation
« Reply #3 on: May 29, 2021, 03:00:27 pm »
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..


The only true wisdom is knowing you know nothing

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Interface delegation
« Reply #4 on: May 29, 2021, 03:34:20 pm »
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..

إلى الآن لم تقُل شيئاً سيّئا ولكن المعلومات الجيّدة التي تقولها لا تتناسب مع الإنطباع الذي يؤخذ عنكـ ،ﻷنّ الصورة التي تؤخذ عنكـ أقل قدراً من المعرفة التي تتكلّم بها مثل أرقام الموبايل الخاطئة.

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."
La chose par la chose est rappelé.

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: Interface delegation
« Reply #5 on: May 29, 2021, 03:59:11 pm »
Imagine 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

  • Hero Member
  • *****
  • Posts: 6130
Re: Interface delegation
« Reply #6 on: May 29, 2021, 04:01:02 pm »
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.
The only true wisdom is knowing you know nothing

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: Interface delegation
« Reply #7 on: May 29, 2021, 04:10:41 pm »
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

  • Hero Member
  • *****
  • Posts: 4219
Re: Interface delegation
« Reply #8 on: May 29, 2021, 04:30:18 pm »
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. ;)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: Interface delegation
« Reply #9 on: May 29, 2021, 04:38:12 pm »
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

  • Hero Member
  • *****
  • Posts: 4219
Re: Interface delegation
« Reply #10 on: May 29, 2021, 04:49:50 pm »
But 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 :D
« Last Edit: May 29, 2021, 04:54:07 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

zamronypj

  • Full Member
  • ***
  • Posts: 133
    • Fano Framework, Free Pascal web application framework
Re: Interface delegation
« Reply #11 on: May 29, 2021, 05:11:34 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.
Fano Framework, Free Pascal web application framework https://fanoframework.github.io
Apache module executes Pascal program like scripting language https://zamronypj.github.io/mod_pascal/
Github https://github.com/zamronypj

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: Interface delegation
« Reply #12 on: May 29, 2021, 05:48:35 pm »
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

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Interface delegation
« Reply #13 on: May 29, 2021, 05:54:37 pm »
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.

لا أدري إن كانت المُشكلة في الإنجليزيّة أم العربيّة لكن عموماً الأمر أشبه باستيراد المزيد من الوظائف من الأصناف الأخرى عن طريق الـ 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."
La chose par la chose est rappelé.

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Interface delegation
« Reply #14 on: May 29, 2021, 06:00:11 pm »
And I won't forget to say thank you to pascal111 for bringging up this topic.

أُحاولُ دائماً البحث عن الكنوز ،boulder dash كانت من أوائل الألعاب التي لعبتها في طفولتي على الكمبيوتر المنزلي :D

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 :D"
La chose par la chose est rappelé.

 

TinyPortal © 2005-2018