Lazarus

Free Pascal => Beginners => Topic started by: pascal111 on May 08, 2021, 01:25:39 am

Title: which to use "object vs class"? [SOLVED]
Post by: pascal111 on May 08, 2021, 01:25:39 am
لقد قرأتُ أنّهُ من أجل تطبيق البرمجة غرضيّة التوجّه في Lazarus أنّهُ يُمكنني أن أُعلن عن الأصناف بإحدى كلمتين مُسجلتين وهما object وclass ولكنّني علمتُ كذلكـ أنّ بينهما فروقاً وبدا لي أنّ النّسخة الرسميّة للأصناف والمطبّقة في بنية Lazarus والتي بها تمّ بناء وتنظيم الـ components هي نسخة الـ class فهل أنا مُلزم عند تطبيقي للبرمجة غرضيّة التوجّه أن أستخدم نُسخة class بدلاً من object أم أنّ لكل نُسخةٍ حالات إستعمالها أو تكادان أن تكونا مُتكافئتين وهما بحسب إختيار وتفضيل المُبرمج؟

google translate:

"I have read that in order to implement the programming for the purpose of orientation (OOP) in Lazarus that I can announce "declare" the classes by one of two registered words, object and class, but I also learned that there are differences between them, and it seemed to me that the official version of the classes applied in the Lazarus structure in which the components were built and organized is the version of the class. Am I obligated, when I apply for purpose-oriented programming (OOP), to use the class version instead of the object, or does each version have its use cases or are almost equivalent, and they are according to the choice and preference of the programmer?"
Title: Re: which to use "object vs class"?
Post by: lucamar on May 08, 2021, 01:49:11 am
Am I obligated, when I apply for purpose-oriented programming (OOP), to use the class version instead of the object, or does each version have its use cases or are almost equivalent, and they are according to the choice and preference of the programmer?"

I can't explain in detail now (it's quite late here) but they are quite different both in how they are used and in how they are implemented. And IMHO both have their use cases: most of the times you'll probably be using classes but (old-style) objects can be more simple and controlable in some cases, though that is now a little more "diluted" due to the availability of advanced records.

And, of course, the programmer's preferences play also a rôle here: some feel that classes were a step in the wrong direction and will swore by objects, while others think that object should be deprecated and only classes (and advanced records) should be used. To each his own, I guess :-\
Title: Re: which to use "object vs class"?
Post by: pascal111 on May 08, 2021, 01:58:59 am
من توضيحكـ إستبان أنّ هذا الموضوع مُتشابكـ ومُتشعّب ويحتاج إلى نوع من التفصيل ولكن ليس وقتكـ الحالي يسمح بذلكـ ،فسأنتظر عندما تكون في سعة من الوقت.

google translate:

"From your explanation, it is clear that this topic is intertwined and complex and needs some kind of detail, but your current time does not allow it, so I will wait when you are in a large amount of time."
Title: Re: which to use "object vs class"?
Post by: Handoko on May 08, 2021, 04:28:52 am
You are free to use whatever you like. Here are the differences between advanced record, object and class:
https://forum.lazarus.freepascal.org/index.php/topic,30686.msg298518.html#msg298518 (https://forum.lazarus.freepascal.org/index.php/topic,30686.msg298518.html#msg298518)

The differences are too technical for most users to understand. Class is considered as the better version of object especially in the old operating system era, for example DOS, which had very limited stack memory for program to run. Object is still maintained but there will be no or little new features added.

But for performance reason, some programmers still use object nowadays. Class needs to allocate memory from heap, which is a very complex process. Object allocates memory from stack, so it is faster.

Normally you should use class, unless you have reasons to use the other.
Title: Re: which to use "object vs class"?
Post by: lucamar on May 08, 2021, 12:11:52 pm
A rather thorough discussion of the two models can be found in the topic: Object in FPC vs class in objFPC (https://forum.lazarus.freepascal.org/index.php/topic,43622.0.html).

Particularly, 440bx's answer (https://forum.lazarus.freepascal.org/index.php/topic,43622.msg305634.html#msg305634) contains a very nice summary of the pros and contras of each of them from a person that knows them well.
Title: Re: which to use "object vs class"?
Post by: marcov on May 08, 2021, 12:49:24 pm
As a beginner, if you are writing code for the Turbo/Free Vision framework, or work on other old TP codebases, use object.

In all other cases use (delphi styled) classes.

Title: Re: which to use "object vs class"?
Post by: pascal111 on May 08, 2021, 01:10:33 pm
You are free to use whatever you like. Here are the differences between advanced record, object and class:
https://forum.lazarus.freepascal.org/index.php/topic,30686.msg298518.html#msg298518 (https://forum.lazarus.freepascal.org/index.php/topic,30686.msg298518.html#msg298518)

The differences are too technical for most users to understand. Class is considered as the better version of object especially in the old operating system era, for example DOS, which had very limited stack memory for program to run. Object is still maintained but there will be no or little new features added.

But for performance reason, some programmers still use object nowadays. Class needs to allocate memory from heap, which is a very complex process. Object allocates memory from stack, so it is faster.

Normally you should use class, unless you have reasons to use the other.

ذهبتُ إلى الرابط الذي زوّدتني به ووجدت من المقارنة بين السجلات ونوعي الأصناف أنّ الفروق تقنيّة حقّاً وهي مفاهيم برمجيّة مُتقدمة أي أنّهُ للمُبتدئين لا يُوجد فرق ملحوظ أو مفهوم ولكن الأمر سيتضح بعد دراسة مُتقدمة وبعدها يستطيع المُبرمج المُبتدئ أن يُحدد أي نسخةٍ يُريدها ،لا يُمكن إتخاذ قرار إلا بعد الإحاطة بمفاهيم الفروق التقنية بين السجلات ونوعي الأصناف.

google translate:

"I went to the link that you provided me with and found from the comparison between the records and the two types of items(classes) that the differences are really technical, which are advanced programming concepts, meaning that for beginners there is no noticeable difference or concept, but the matter will become clear after an advanced study and then the novice programmer can determine which version he wants, no decision can be made Except after understanding the concepts of the technical differences between the records and the two types of items(classes)."
Title: Re: which to use "object vs class"?
Post by: pascal111 on May 08, 2021, 01:40:59 pm
A rather thorough discussion of the two models can be found in the topic: Object in FPC vs class in objFPC (https://forum.lazarus.freepascal.org/index.php/topic,43622.0.html).

Particularly, 440bx's answer (https://forum.lazarus.freepascal.org/index.php/topic,43622.msg305634.html#msg305634) contains a very nice summary of the pros and contras of each of them from a person that knows them well.

@damieiro in the link considers the two models quite similar, but they have some similarity, especially some technical points.
Title: Re: which to use "object vs class"?
Post by: Zoran on May 08, 2021, 02:35:54 pm
If I'm not mistaken, you are a beginner in oop concept.

Just take the classes approach and forget about old style objects. You don't need this confusion.
Title: Re: which to use "object vs class"?
Post by: PascalDragon on May 08, 2021, 03:10:50 pm
"I have read that in order to implement the programming for the purpose of orientation (OOP) in Lazarus that I can announce "declare" the classes by one of two registered words, object and class, but I also learned that there are differences between them, and it seemed to me that the official version of the classes applied in the Lazarus structure in which the components were built and organized is the version of the class. Am I obligated, when I apply for purpose-oriented programming (OOP), to use the class version instead of the object, or does each version have its use cases or are almost equivalent, and they are according to the choice and preference of the programmer?"

While we don't declare TP-style objects as deprecated like Delphi did, they are essentially in "maintenance mode". So if you want to take advantage of newer language features, it's more likely that the Delphi-style classses will get them than TP-style objects.

Also if you want to interact with Lazarus' LCL you pretty much must use Delphi-style classes.
Title: Re: which to use "object vs class"?
Post by: pascal111 on May 08, 2021, 06:44:34 pm
If I'm not mistaken, you are a beginner in oop concept.

Just take the classes approach and forget about old style objects. You don't need this confusion.


لست مُخطأً في كوني مُبتدأً في البرمجة غرضيّة التوجّه وسأعمل بنصيحتكـ بتعلّم إصدارة الـ class بدلاً من إصدارة الـ object.


google translate:

"I am "You are" not at fault for "I'm" being a beginner in object-oriented programming, and I will follow your advice on learning the class version instead of the object version."
Title: Re: which to use "object vs class"?
Post by: pascal111 on May 08, 2021, 07:04:31 pm
"I have read that in order to implement the programming for the purpose of orientation (OOP) in Lazarus that I can announce "declare" the classes by one of two registered words, object and class, but I also learned that there are differences between them, and it seemed to me that the official version of the classes applied in the Lazarus structure in which the components were built and organized is the version of the class. Am I obligated, when I apply for purpose-oriented programming (OOP), to use the class version instead of the object, or does each version have its use cases or are almost equivalent, and they are according to the choice and preference of the programmer?"

While we don't declare TP-style objects as deprecated like Delphi did, they are essentially in "maintenance mode". So if you want to take advantage of newer language features, it's more likely that the Delphi-style classses will get them than TP-style objects.

Also if you want to interact with Lazarus' LCL you pretty much must use Delphi-style classes.

يبدو كما قلت أنّهُ للإستفادة من الجوانب الإضافيّة والمميزات الجديدة المزيدة في اللغة مثل مكتبة LCL فيلزم التعامل مع نسخة الـ class لا نسخة الـ object التقليديّة.

google translate:

"It seems, as I "you" said, that in order to take advantage of the additional aspects and new and more features in the language such as the LCL library, it is necessary to deal with the class version and not the traditional object version."
Title: Re: which to use "object vs class"? [SOLVED]
Post by: pascal111 on May 08, 2021, 08:09:23 pm
I use a mix of things, Classes, Objects, Records etc..

The reason is that at times I like heap objects, those that do not need to be created at time of need.


من الجيّد أنّكـ وجدت توظيفاً لكل نوع سواءاً كان سجلات أم الأصناف بنسختيها الـ calss والـ object.

google translate:

"It is good that you found a job for each type, whether it is records or classes, in its calss and object versions."

Now you can do this with the use of RECORDS however, Records do not inherit unless you are doing helpers which at this time I believe works only in Fpc and not Delphi.

لقد استخدمت السجلات من قبل والسجلات المتقدمة كذلكـ وأظنّ أنّ السجلات مناسبة جدّاً للمبتدئين وحتّى لكي يفهموا الفكرة العامة للأصناف ،ولكنّني لم أستخدم ميزة الـ helpers بعد ولكن سمعت عنها منكـ ومن خلال ذكرها في صفحات ويب أخرى ويبدو أنّي سأقرأ عنها بالتفصيل من خلال بحثي في google إن عثرت على بعض الشروح لهذه الميزة في السجلات.

google translate:

"I have used the records before and the advanced records as well - and I think that the records are very suitable for beginners and even for them to understand the general idea of ​​the classes, but I have not used the helpers feature yet but I heard about it from you and through mentioning it in other web pages and it seems that I will read about it in detail through my search on google if I found some explanations for this feature in the logs"of the records"."

Also, the Methods of a class are of type OBJECT The difference is the CLASSes are purly dynamic and need to be created always.

ماذا تعني بكون الـ  methods من نوع الـ object؟ هل تعني الـ methods خاصّة نسخة الـ  object وليس نسخة الـ class؟ عموماً كما أشرتَ أنّ نسخة الـ class تبدو حقّاً ديناميكيّة أكثر من نسخة الـ object.


google translate:

"What do you mean that methods are of type an object? Does "Do you mean" the methods specifically mean the object version and not the class instance "version"? In general, as you indicated, the class version really appears to be more dynamic than the object version."
Title: Re: which to use "object vs class"? [SOLVED]
Post by: lucamar on May 08, 2021, 08:50:07 pm
"What do you mean that methods are of type an object? "Do you mean" the methods specifically mean the object version and not the class instance "version"? In general, as you indicated, the class version really appears to be more dynamic than the object version."

Jamies's English is sometimes a little obscure :-X. My guess is that he's referring to event types which are usually declared as "of object", as in:
Code: Pascal  [Select][+][-]
  1. type
  2.   TNotifyEvent = procedure(Sender: TObject) of object;
which basically means that the event handler has to be declared as a method of a class (that is, part of the "type" of methods is being "of object").

This in turn means that to use those "types" you have to use classes rather than "old-style" objects, despite the notation being "of object".
Title: Re: which to use "object vs class"? [SOLVED]
Post by: pascal111 on May 08, 2021, 09:32:04 pm
Jamies's English is sometimes a little obscure :-X.

إن كان الغموض قادراً على أن يُصيب الإنجليزيّة فإنّهُ كان قراراً صائباً أن أكتب بالعربيّة حيث العبارات مُستقيمة المعنى لا عوج فيها قادرةٌ على التوجيه الصحيح للفكر وتضبط عمل العقل.

google translate:

"If the ambiguity was able to afflict the English, then it was a correct decision to write in Arabic, where the expressions are straight in meaning and not crooked, capable of directing the correct thought and controlling the work of the mind."


My guess is that he's referring to event types which are usually declared as "of object", as in:
Code: Pascal  [Select][+][-]
  1. type
  2.   TNotifyEvent = procedure(Sender: TObject) of object;
which basically means that the event handler has to be declared as a method of a class (that is, part of the "type" of methods is being "of object").

This in turn means that to use those "types" you have to use classes rather than "old-style" objects, despite the notation being "of object".

لقد سمعتُ عن الأحداث وهي ميزة حديثة وهذه الأيام يعتبرون من لا يعرف البرمجة غرضيّة التوجّه والبرمجة المسوقة بالأحداث ليس بمبرمج حقيقي ،عليّ أن أُلم بمثل هذه مواضيع.

google translate:

"I have heard about events, which is a recent feature, and these days they consider those who do not know programming for the purpose of orientation (OOP) and programming that are marketed by events (Event driven p.) are not a real programmer, I have to be familiar with such topics."
Title: Re: which to use "object vs class"? [SOLVED]
Post by: pascal111 on May 08, 2021, 11:36:43 pm
obscure ?

 I don't think so..

 Methods are of object type which have a background variable (self) that points to the instance of the body for any fields, etc.

 Using OBJECT or using Classes, they both work the same way. Difference is that Classes are dynamic only and must always be created to access any member fields excluding Class types. OBJECTS are not bound to this, they can live on the heap or stack and the instance body exist for them or they can be dynamically created, its your choice.

 AS for what you were referring to, that is a declared variable (TMethod) if memory serves, and its just a pointer to the a type METHOD. and that can be assigned for events or runtime variations functions to a single method pointer..

 I am sure a 100+ people will come here and twist everything I just said because they can.

That's fine. This is my final comment on this subject anyways..

have a good day and may the bugs be with you.

 

ربّما كان نعت الكلام بالغموض مِزاحاً أو لتهوين الوقت والنقاشات الجادّة ولا أرى سوءاً فيما تفضّلت وقُلت.

التوضيح الذي قدمته الآن يحتوي على معلومات مُفيدة وأعتقد أنّ المقصود بالديناميكيّة في نسخة الـ object هو عن طريق إستخدام المؤشرات للإعلان عن عينات منها بدلاً من مُتغيرات مُعتادة ساكنة تظل تشغل الذاكرة طيلة الـ run-time.

google translate:

"Perhaps the word was called ambiguous as a joke or to belittle the time and serious discussions, and I do not see anything wrong with what I you said and preferred "exerted with".

The illustration I "you" presented now contains useful information and I think what is meant by dynamic in the object version is by using pointers to announce "declare" their samples "instances" instead of regular static variables that keep running the memory throughout the run-time."
Title: Re: which to use "object vs class"?
Post by: dseligo on May 10, 2021, 04:36:56 pm
As a beginner, if you are writing code for the Turbo/Free Vision framework, or work on other old TP codebases, use object.

Or if he works with AVR target (and possibly other embedded targets without OS to provide memory allocation) then he has to use (static) object.

Quote from: marcov
In all other cases use (delphi styled) classes.

I agree.
TinyPortal © 2005-2018