I am reading Real-Time UML Workshop for Embedded Systems and I do not understand what they are saying here. I have bolded the parts of the text that I do not fully understand. I am trying to understand this from a C stand point of view.
An interface is a named collection of services. Services come in two
basic flavors. Operations are synchronous services that are invoked,
or called by clients. Event receptions are asynchronous services that
are invoked by sending an asynchronous signal to the object accepting
that event. An interface can contain either or both operations and
event receptions. Both operations and event signals can contain data,
called parameters, passed with the invocation. The set of parameters,
along with the name of the service, is called the signature of the
service. A class that is compliant with an interface provides a
method for every operation and an event reception for every signal
specified in the interface.Interfaces allow you to separate the specification of services that may be called on a class from the implementation of those services. A
class defines methods (for operations) and event receptions (for
signals, specified on the state machine). Both methods and event
receptions include the lines of code that implement the service. An
operation or signal is a specification only, and does not include such
implementation detail.Interfaces may not have implementation (either attributes or methods) and are not directly instantiable. A class is said to realize
an interface if it provides a method for every operation specified in
the interface, and those methods have the same names, parameters,
return values, preconditions, and postconditions of the corresponding
operations in the interface.
What is an example (or two!) of implementation of an interface in C ?
From what it seems like, its just a prototype of a specific functions in the header file.
I removed UML and added interface and object-oriented-design because I think the question turns around this subjects rather than UML
Best Answer
OOP languages do it much better, and even in C there are better ways to do this, but here is a crude example. I'll show an interface with methods only - the concept of event receptions is more complex, but orthogonal to the one of interfaces, and also beside methods and event receptions there can be other things an interface's implementation needs to fulfill, and I can't show them all here(definitely not in C!)
PrintInterface
is the interface. It has two methods -printIntItem
andprintStringItem
. Their contract can be divided into two parts:I've also written two implementations -
PrintJson
andPrintXml
. What they do is simply to put functions inside anPrintInterface
struct. Proper OOP languages/libraries have much more efficient and convenient ways to do this, but this one is for the sake of being easy to follow. They both return a value of the same type -PrintInterface
- and that type can be passed around to users of the interface - likeprintStuff
.When you run this program, this will be printed:
First
printStuff
is called with the JSON implementation. It uses the API to print the key-value pair"one"->1
and then the key-value pair"two"->"2"
, and thus{"one": 1}
and{"two": "2"}
are printed because it uses a JSON implementation.Then
printStuff
is called with the XML implementation. It uses the API to print the key-value pair"one"->1
and then the key-value pair"two"->"2"
, and thus<one>1</one>
and<two>2</two>
are printed because it now uses an XML implementation.