Recent

Author Topic: Procedural interface or interfaced object?  (Read 1744 times)

cpicanco

  • Hero Member
  • *****
  • Posts: 618
  • Behavioral Scientist and Programmer
    • Portfolio
Procedural interface or interfaced object?
« on: July 27, 2017, 05:48:57 pm »
TLDR: Should object composition be favored over de-encapsulation? Could you please recommend some good examples of interfaced objects and object composition?
_

I think a good example of a procedural interface is the input/output read/write system routines. They are self contained, versatile, and I am sure you could came up with other advantages of such procedural design. So I started implementing io routines. Once written, I realized many redundant code could benefit from a wrapper to avoid such redundancy. That is why I wrote this class:

https://github.com/cpicanco/stimulus_control/blob/master/src/units/loggers.datafile.pas

It is very simple. Open, Write, Close with some utilities. In this program, one class needs to Open/Close/Write, but all other classes needs only to write. My solution was to remove the encapsulation of this public procedure for all other classes by means of a property.

https://github.com/cpicanco/stimulus_control/blob/master/src/units/session.blocs.pas#L113
https://github.com/cpicanco/stimulus_control/blob/master/src/units/session.configuration.pas#L26

So what?

Now I am learning about interfaced objects and realized that this problem could be solved more intuitively using them. For example, using object composition instead of removing (from outside) the encapsulation of an existing class;  i.e., by composing other classes using interfaces. Do you agree?

P.S.: Of course, everything is working, and the point here is to be ready for future problems, not changing what is already working.

Be mindful and excellent with each other.
https://github.com/cpicanco/

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11353
  • FPC developer.
Re: Procedural interface or interfaced object?
« Reply #1 on: July 27, 2017, 06:13:03 pm »
Stdio is basically already a bit OO, since the file record is a record full of procedure variables (lowlevel equivalent to a class with virtual methods).

To see how to replace or amend I/O have a look at the streamex unit.


 

TinyPortal © 2005-2018