OOP and Delphi/Lazarus

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
minesadorada
Posts: 68
Joined: Sun 11 Sep 2011, 19:10

OOP and Delphi/Lazarus

#1 Post by minesadorada »

Thanks to Puppy and Lazarus, I have got back into Pascal programming (only as a hobby nowadays - I'm retired) I'd almost forgotten how simple, elegant and powerful it was.

The application is an USGA/EGA Golf Handicap calculator.

It uses data from a golf club (different for each course and tee position, and dynamic in size too) to calculate EGA handicaps, playing handicaps and adjustments to handicaps.

I wrote the logic as a self-contained object (with a separate GUI interface which gets/sets the properties and calls the methods), and it had an abstract method 'GetCourseData', since the data could come via direct input, a database, an XML file - anywhere.

So using the original object as ancestor I extended it into a child object that implemented GetCourseData to read golfml (open source XML standard) files.

The advantage of this OOP method is that I could just as easily extend the ancestor to implement GetCourseData as a method to read a database, or any other data format, and the ancestor properties and methods would be the same, as would any GUI based on it.

When designing a course data writer, again the OOP method has advantages.
1 ) Write a data-entry user-friendly GUI
2 ) Write (or re-use) an ancestor object to receive and store all the data from the GUI (via property access) and have an abstract method 'WriteCourseData'
3 ) Subclass it and implement the WriteCourseData method to write the stored course data in particular formats.

This means the same ancestor object can be used as a base to load and save golf course data in as many formats as you have subclasses, with a standard GUI for input, and various GUIs to display or use the data.

Lazarus object pascal makes this process of isolating input, processing and output particularly easy, and the GUIs will compile for a good variety of platforms without having to alter code. Of course it makes maintainance and bugfixing a lot simpler, too.

I'm currently implementing the writer subclass for the golfml format.

Does anyone else here use OOP as their first-choice solution?

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#2 Post by musher0 »

Euh... what does the acronym "OOP" stand for, please?

Thanks in advance.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
RSH
Posts: 2397
Joined: Mon 05 Sep 2011, 14:21
Location: Germany

#3 Post by RSH »

musher0 wrote:Euh... what does the acronym "OOP" stand for, please?

Thanks in advance.
Object Oriented Programming.

In German: Objektorientierte Programmierung.

I am not sure, but i think it mostly means GUI-Building, but can also mean Objects like Classes etc.

Please, minesadorada, do correct me, if i am totally or partially wrong.
[b][url=http://lazy-puppy.weebly.com]LazY Puppy[/url][/b]
[b][url=http://rshs-dna.weebly.com]RSH's DNA[/url][/b]
[url=http://murga-linux.com/puppy/viewtopic.php?t=91422][b]SARA B.[/b][/url]

User avatar
minesadorada
Posts: 68
Joined: Sun 11 Sep 2011, 19:10

OOP

#4 Post by minesadorada »

The basic idea is to isolate data within a block of code called a class or object. The object then has a public API which manipulates the hidden data.

Access to the private data is only via public methods and properties (the API). By controlling data access in this way, the code to access and manipulate it can be thoroughly debugged with access to the data invisibly type, range and exception checked. (development debugging is usually using a simple and temporary GUI)

There's a decent wikipedia article on it here: http://en.wikipedia.org/wiki/Object-ori ... rogramming

The trick with OOP is to plan ahead, and consider data structures and objects before starting a programming project. In that way, you can save yourself not only lots of redundant code, but also have a simple and clear development/debugging/optimising path to follow. OOP also makes you think about scope issues from the start.

You don't get instant results when coding in this way, as the object hierarchy has to be built from the base upwards, and the initial coding of properties and methods can be quite boring, but believe me - the results are worth it, and the objects are often re-usable for other projects.

The real power of OOP comes with inheritance and polymorphism via abstraction which you'd be better off Googling for a full explanation. My golf application class/subclasses used all three.

Some people make out that OOP is difficult, but that's a complete myth, and as I said in the first post - Lazarus/FPC can help you do OOP with ease.

User avatar
minesadorada
Posts: 68
Joined: Sun 11 Sep 2011, 19:10

#5 Post by minesadorada »

Project golfml stage 1 finished now. I ended up rolling up the golfml Writer and Reader class into one object.

Here's a summary of the class: http://www.charcodelvalle.com/golfmlweb ... class.html

Applications and Lazarus source code at http://www.charcodelvalle.com/golfmlweb/

The apps run on Linux 32 and 64 bit (as well as windows). All opensource, so feel free to download and play.

Stage 2 is making up xsl stylesheets, and it's been some years since I did xml/xsl transformations.

User avatar
Moose On The Loose
Posts: 965
Joined: Thu 24 Feb 2011, 14:54

#6 Post by Moose On The Loose »

minesadorada wrote:Project golfml stage 1 finished now. I ended up rolling up the golfml Writer and Reader class into one object.

Here's a summary of the class: http://www.charcodelvalle.com/golfmlweb ... class.html

Applications and Lazarus source code at http://www.charcodelvalle.com/golfmlweb/

The apps run on Linux 32 and 64 bit (as well as windows). All opensource, so feel free to download and play.

Stage 2 is making up xsl stylesheets, and it's been some years since I did xml/xsl transformations.
I tend to use FreePascal.

On large projects, the parent type I usually start the tree with is one that knows how to read and write its self from and to the configuration file. Really the base type only writes its type value out and nothing more. The children know who to write the properties that matter. This way it is easy to write a thing to save the session and load a saved session and things like that.

If you do it right, the saved sessions work across versions. This way, when you find a bug, you can save the session and reload it for testing in the corrected version.

The only hard part is writing the bit that goes through the types looking for the matching one. This is done with a linked list of the methods that check for a match and then conditionally call the constructor on a match.

User avatar
minesadorada
Posts: 68
Joined: Sun 11 Sep 2011, 19:10

#7 Post by minesadorada »

Moose On The Loose wrote:]

I tend to use FreePascal.

On large projects, the parent type I usually start the tree with is one that knows how to read and write its self from and to the configuration file. Really the base type only writes its type value out and nothing more. The children know who to write the properties that matter. This way it is easy to write a thing to save the session and load a saved session and things like that.

If you do it right, the saved sessions work across versions. This way, when you find a bug, you can save the session and reload it for testing in the corrected version.

The only hard part is writing the bit that goes through the types looking for the matching one. This is done with a linked list of the methods that check for a match and then conditionally call the constructor on a match.
@Moose On The Loose: Have you example code to share?

I just installed Tortoise SVN for windows. Oh my! What an improvement from years ago when I last used it for an open-source project. Now much easier than Visual Sourcesafe 5 :( which I was forced to use when I was working commercially.

I can add and commit via right-click in explorer and it even hooks onto the move-copy drags in Windows.

Hopefully someone will compile a Puppy GUI PET for svn (I couldn't find one on the forum)

I'm working with code.google.com which is first-rate and free.

Post Reply