6

What is exactly an interface in a Component diagram in C?

I am an embedded systems tester understanding software development architecture. I have seen Component diagrams of the project I perform Black Box testing. I have seen that components are represented by blocks that joins with "sockets" and "lollipos". I know that those are interfaces, and components provide and request interfaces. I have read different articles but i do not find a practical example of it. Are they functions and variables, and the component that request such interface calls it and the provider has the definition of the function?

PySerial Killer
  • 428
  • 1
  • 9
  • 26
  • I suggest getting a book or joining a class to learn some OO basics before asking such a basic question here. – qwerty_so Dec 08 '17 at 23:10
  • 3
    Or, rather than spending money, you could google around some more. For example, there is helpful information [here](https://stackoverflow.com/questions/2866987/what-is-the-definition-of-interface-in-object-oriented-programming). Also, if you are familiar with C, you might look into interfaces as they are implemented in C++ (try [this](https://www.tutorialspoint.com/cplusplus/cpp_interfaces.htm)). And I disagree with Mr. Killian (to be fair, I'm a teacher by nature). Feel free to ask whatever questions you want here, especially if you have been doing your best to find an answer on your own. – BobRodes Dec 09 '17 at 19:27
  • @BobRodes Well, if someone came up with "What is this UML anyway?", then what would you think? This one isn't too far off. Sure, ask here if you got a question. But there's also that "show some effort on your own before doing so". – qwerty_so Dec 10 '17 at 15:08
  • @ThomasKilian It seems pretty clear to me that the OP has done some research into interfaces, and is having trouble using the examples he has looked at to tie the concept to what he already knows (an embedded systems tester has a lot of experience with C, which isn't OO). When he asks "are they functions and variables" he's getting close. So, it seems to me that he has gone to some trouble to figure this out on his own, and it seems to you that he hasn't. FWIW, I don't have much patience for people who aren't willing to put any effort in either, but I don't think the OP is one of these. – BobRodes Dec 10 '17 at 19:14

3 Answers3

4

Definition of a component according to the UML standard:

A Component represents a modular part of a system that encapsulates its contents and whose manifestation is replaceable within its environment.

A Component is a self-contained unit that encapsulates the state and behavior (...). A Component specifies a formal contract of the services that it provides to its clients and those that it requires from other Components or services in the system in terms of its provided and required Interfaces.

A Component is a substitutable unit that can be replaced at design time or run-time by a Component that offers equivalent functionality based on compatibility of its Interfaces. (...)

A component defined by its API

In C a component can be a set of compilation units that define functions and global variables that are visible to other compilation units, and that may require the existence of functions or global variables in other components:

  • the set of global variables and functions implemented by the component (and available for others) is the interface that is offered.
  • the set of extern global variables and functions that are expected by the component to be defined elsewhere is the required interface.

A component defined by a structure

In C you can also develop using an object oriented style. Of course, it's not so nice and convenient as C++. But it allows to develop interchangeable components.

The trick is to use a structure that defines data members or pointers to data members (state) and function pointers obeying certain signatures (behavior). In this case, the definition of the structure defines the interface provided by the component.

Microsoft is for example using such an approach for its COM technology

A component defined by its system interface

A C component doesn't need to be a part of a larger programme and offer a source code or an object code interface. It can be itself an independent programme, that offers an interface at runtime using OS features, such as a network protocols (listening to sockets, or implementing HTTP or other network protocols), remote function calls or other IPC techniques (e.g. shared memory, mutexes, etc...).

The interface is a contract

As the examples above have shown, the interface is not limited to a specific language feature. An interface is a contract about what is provided and what is expected to communicate with the component.

Christophe
  • 68,716
  • 7
  • 72
  • 138
4

What is exactly an interface in a Component diagram in C? [...] I have read different articles but i do not find a practical example of it. [...]

I believe there are cases where it could be low-level interfaces (for a C module) or something higher level, such as a REST API or communications protocol. I think the general idea is that an interface is something published, which allows implementations to be swapped in and out.

Here's an example used in Craig Larman's Applying UML and Patterns book, which is concrete:

PlantUML component diagram with two components

The MyApp system requires two components, one for the messaging, one for the database (DB). The interface of each is standard (in this example), namely JMS and SQL respectively.

In your case with C, the interfaces are probably defined in one or more .h files for each component (which could be one or more .c files, again there is some flexibility on what is a component). They likely are not as "standard" as JMS, SQL, etc., but if you're doing testing, you should be able to test on both sides of an interface.

Fuhrmanator
  • 11,459
  • 6
  • 62
  • 111
1

Basically an interface is a declaration of attributes and operations someone might offer or need. A class can implement an interface which means it must implement the operations and have the attributes accessible. Now a component is something that is composed of 1..n classes and interface can be made visible outside the component. So finally a component can show a number of different interfaces which a number of its internal classes offer to the outer world.

Regarding sockets and lollipops: The lollipops represent provided interfaces (classes that have implemented it). The sockets represent required interfaces. That is there must be a counterpart somewhere to connect which offers an implementation of the interface.

qwerty_so
  • 35,448
  • 8
  • 62
  • 86
  • About lollipops, there is something tricky for me, the difference between the assembly connector ( fig 11.8) and the link with association (fig 11.46) – granier Dec 11 '17 at 10:06
  • @granier I use the assembly connector as conceptual interfaces. Like "there needs to be an interface, but I haven't set up the details yet." Later these will be replaces by real interfaces. Or you have a transformed model where one has assembly connectors and the other uses interfaces with dependencies. – qwerty_so Dec 11 '17 at 13:05
  • @Kilian, thanks for your solution and have you check that's the usage defined by the specification ? – granier Dec 11 '17 at 13:09
  • @granier Honestly, no. However, I feel comfortable with that :-) I'll look up the specs, though. – qwerty_so Dec 11 '17 at 13:17
  • The specs are wishy-washy here. See p. 212 for an example. – qwerty_so Dec 11 '17 at 13:21
  • If i can say that, i am happy that someone else than me is confused about this point ... – granier Dec 11 '17 at 14:46
  • @Kilian, re the assembly connectors: it is a proper usage. Just bear in mind that the assembly connectors can also be used to direct an interface to a component within the component - if you want to model at that level. – CharlesRivet Dec 13 '17 at 15:05
  • @CharlesRivet I usually use a `<>` to forward component interfaces to class interfaces inside the component. – qwerty_so Dec 13 '17 at 20:58
  • @Thomas Kilian I agree for classes, but I'm taking components. And this way, with good tooling, the connector could be validated to match the interface. And as a personal preference, it makes it look more like composite structures (or IBDs in SysML). – CharlesRivet Jan 03 '18 at 16:31
  • @CharlesRivet D'accord. There are always many aspects that can/should be looked at with models. I was writing this answer for an obvious rookie (no offense). – qwerty_so Jan 03 '18 at 19:52