[fpc-pascal] How to have smaller units
DougC
doug at moosemail.net
Sun Nov 30 16:44:47 CET 2025
I agree with what Giuliano has said, but would also encourage Amir to just look at his object hierarchy critically. He does not have to abandon the OOP method just because he got into trouble here.
I have run into similar problems when designing my object hierarchies. The solution was found in recognizing my PARTIAL adoption of the OOP method. In other words, I followed OOP up to a certain point, and then saw an easy way to solve some data access problems by short-circuiting the OOP method and making use of the private access afforded by locating several classes in the same unit. But that is, indeed, a shortcut that violates the OOP principles.
One good exercise is to place each class in its own unit. Do the classes now work well together? If not, you have not designed your hierarchy according to proper OOP methodology.
---- On Sun, 30 Nov 2025 07:49:13 -0500 Giuliano Colla via fpc-pascal <fpc-pascal at lists.freepascal.org> wrote ---
Hi,
IMHO your basic problem is that you are trying to use objects in a way
which contradict the basic object concept. The basic concept is to
expose an interface and hide the implementation. Higher level code
should be able to use objects without caring of implementation.
Private object members are just private, implementation dependent, and
therefore should not be accessible. If they are, you are making your
high level code dependent from object implementation, which is wrong.
If for some reasons you cannot rearrange your code in such a way as to
be coherent with object philosophy, that means that you should not use
objects. You're not obliged to.
An object is nothing else but a data structure, which includes in
addition the procedures to deal with the data.
But you may write just data structures and procedures separately, and
make whatever you need be accessible form whatever else you need. And if
you don't want end users have the same rights, you just use an
interface. Your code will be loaded at run time, and end users will only
be able to access what the interface exposes.
That is the trick used by Lazarus, whose high level objects are
implemented in a totally different way depending on the selected widgetset.
Just my two cents.
Giuliano
Il 28/11/25 22:58, Amir via fpc-pascal ha scritto:
> Hi,
>
> One of the main thing bothering me while developing projects in
> Pascal is that my units are getting huge in size! The main reason is
> that the nice property that the classes could access each other
> private members if they are defined in the same unit motivates/forces
> me to define a lot of classes in the same unit. Then, all the
> functions/procedures implementation must go into the same unit. I know
> I can use inc file but I do not like it! The other option is to use
> "Cracker" pattern, in Delphi, which is fine but seems like a hack!
>
> In C++, the concept of header files vs c++ files is helpful. In
> Golang, one could implement the functions for a class in several
> files, as long as they are in the same directory (namespace).
>
> Wondering if there is a solution for this in Object-Pascal?
>
> Best,
> Amir
>
>
> _______________________________________________
> fpc-pascal maillist - mailto:fpc-pascal at lists.freepascal.org
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
--
Do not do to others as you would have them do to you.They might have different tastes.
_______________________________________________
fpc-pascal maillist - mailto:fpc-pascal at lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20251130/4f36e8db/attachment.htm>
More information about the fpc-pascal
mailing list