From: "philip...@gmail.com" <philip.munts@gmail.com>
Subject: Re: Simple example on interfaces
Date: Tue, 26 Jan 2021 17:48:03 -0800 (PST) [thread overview]
Message-ID: <546f7f19-cdda-4eb4-9202-9361905b8726n@googlegroups.com> (raw)
In-Reply-To: <rumtg6$j94$1@dont-email.me>
> "IMHO, Interfaces are worthless."
I find interfaces to be extremely valuable for abstracting I/O devices. For example in my Linux Simple I/O Library, there is code equivalent to the following (the actual code is different, as I sucked a lot of common boilerplate for I/O device interfaces into a generic package that is instantiated for each data item type):
package GPIO is
type Direction is (Input, Output);
type PinInterface is interface;
type Pin is access all PinInterface'Class;
procedure Put(Self : PinInterface; state : Boolean);
function Get(Self : PinInterface) return Boolean;
end GPIO;
I've probably defined a dozen packages that implement GPIO pins using everything from Linux kernel services to web servers. Every one of them contains a function like this:
function Create(...) return GPIO.Pin;
This allows code like the following:
GPIO1 : GPIO.Pin := GPIO.libsimpleio.Create(RaspberryPi.GPIO18, GPIO.Output);
GPIO2 : GPIO.Pin := GPIO.HTTP.Create("http://foo.munts.net", 5, GPIO.Output);
GPIO3 : GPIO.Pin := GPIO.RemoteIO.Create(server, 7, GPIO.Output);
This allows GPIO pins scattered far and near throughout the known universe to be treated exactly the same, even collected into an array or container.
I very seldom implement more than one interface in a type definition though, unless a single device has multiple sensors (temperature and humidity, for instance).
Microsoft's .Net uses this scheme pervasively, though I originally learned it in Ada and later applied the same thinking to .Net, Free Pascal, Java, Python, and C++ (and other languages).
next prev parent reply other threads:[~2021-01-27 1:48 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-01-25 16:08 Simple example on interfaces Mario Blunk
2021-01-25 16:41 ` Dmitry A. Kazakov
2021-01-25 17:51 ` Mario Blunk
2021-01-25 22:06 ` Dmitry A. Kazakov
2021-01-26 7:33 ` G.B.
2021-01-26 8:07 ` Dmitry A. Kazakov
2021-01-26 8:17 ` Mario Blunk
2021-01-26 8:55 ` Dmitry A. Kazakov
2021-01-26 9:37 ` J-P. Rosen
2021-01-26 10:25 ` Dmitry A. Kazakov
2021-01-26 11:15 ` AdaMagica
2021-01-26 11:53 ` Dmitry A. Kazakov
2021-01-26 16:46 ` AdaMagica
2021-01-26 19:44 ` Dmitry A. Kazakov
2021-01-26 20:04 ` Shark8
2021-01-26 21:34 ` Dmitry A. Kazakov
2021-01-27 3:11 ` Randy Brukardt
2021-01-27 22:51 ` Shark8
2021-01-30 8:33 ` Randy Brukardt
2021-01-27 3:09 ` Randy Brukardt
2021-01-27 8:05 ` Dmitry A. Kazakov
2021-01-26 10:02 ` Stephen Leake
2021-01-25 17:00 ` Jeffrey R. Carter
2021-01-27 1:48 ` philip...@gmail.com [this message]
2021-01-27 8:06 ` Dmitry A. Kazakov
2021-01-27 3:36 ` Randy Brukardt
2021-01-27 23:04 ` Shark8
2021-01-25 19:05 ` Stephen Leake
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox