A module consists of two parts: interface and implementation. The interface specifies what the module is going to do. It declares the identifiers, types, and routines available for using the code of the module to achieve the goal of specifying how the module achieves its interface declaration, A given module usually has only one interface, but there may be many implementations that can provide the functions specified by the interface. Each implementation may use different algorithms and data structures, but they must comply with the instructions provided by the interface. A customer calls a program using a piece of code from a module. The customer calls the program import interface to implement the export interface. Because multiple clients call programs to share interfaces and implementations, the implemented target code avoids unnecessary code duplication and helps avoid errors, because the interface and implementation can be used multiple times by writing and debugging only once.
URL: http://www.cnblogs.com/archimedes/p/c-interfaces-implementations.html.
Interface
The interface only needs to specify the identifiers that the client call program may use. It is necessary to hide irrelevant representations and algorithms as much as possible, so that the customer call program does not have to rely on specific implementation details. This dependency-coupling between the client call program and the implementation may cause errors when the implementation changes. When this dependency is buried in assumptions about the implementation hidden or ambiguous, these errors may be difficult to fix, so a well-designed and precisely-described interface should minimize coupling.
C language only provides the most basic support for the separation of interfaces and implementations, but simple conventions can bring huge benefits to the interface/implementation methodology. In C, the interface is declared in the header file. The header file declares the macros, types, data structures, variables, and routines that can be used by the client call program. You can use the C-language pre-processing command # include import interface.
The following examples illustrate some conventions and interfaces used in the interface in this article:
Extern int Arith_max (int x, int y); extern int Arith_min (int x, int y); extern int Arith_div (int x, int y); extern int Arith_mod (int x, int y, int y); extern int Arith_ceiling (int x, int y); extern int Arith_floor (int x, int y );Arith. h
The Interface Name Is Arith, and the interface header file is also named arith. h accordingly. The interface name is prefixed and appears in each identifier of the interface. The module name not only provides a proper prefix, but also helps sort out the client call code.
The Arith interface also provides some functions that are not yet useful in the Standard C function library, and provides a good definition for the departure and modulus, standard C does not define these operations and only provides implementation-based definitions.
Implementation
An Implementation exports an interface, which defines the necessary variables and functions to provide the functions specified by the interface. In C, an implementation is composed of one or more. in file c, an implementation must provide the function specified by its export interface. The implementation should include the. h file of the interface to ensure its definition is consistent with the interface declaration.
Arith_min and Arith_max return the minimum and maximum values of their Integer Parameters:
int Arith_max(int x, int y) { return x > y ? x : y;}int Arith_min(int x, int y) { return x > y ? y : x;}
Arith_div returns the quotient obtained by dividing y by x, and Arith_mod returns the remainder. When x and y are the same, Arith_div (x, y) is equivalent to x/y. Arith_mod (x, y) is equivalent to x % y.
When the symbols of x and y are different, the return value of the C embedded operation depends on the specific implementation:
For example, if-13/5 = 2,-13% 5 =-3, if-13/5 =-3,-13% 5 = 2
Standard library functions are always rounded to zero, so the semantics of div (-13, 2) =-2, Arith_div and Arith_mod are also defined: they are always rounded to the left of the number axis, therefore, Arith_div (-13, 5) =-3, Arith_div (x, y) is the maximum integer that does not exceed the real number z, where z satisfies z * y = x.
Arith_mod (x, y) is defined as x-y * Arith_div (x, y ). Therefore, Arith_mod (-13, 5) =-13-5 * (-3) = 2
The Arith_ceiling and Arith_floor functions follow similar conventions. Arith_ceiling (x, y) returns the smallest integer not less than the real number quotient x/y.
Arith_floor (x, y) returns the maximum integer not greater than the real number quotient x/y.
The complete implementation code is as follows:
# Include "arith. h" int Arith_max (int x, int y) {return x> y? X: y;} int Arith_min (int x, int y) {return x> y? Y: x;} int Arith_div (int x, int y) {if (-13/5 =-2 & (x <0 )! = (Y <0) & x % y! = 0) return x/y-1; else return x/y;} int Arith_mod (int x, int y) {if (-13/5 =-2 & (x <0 )! = (Y <0) & x % y! = 0) return x % y + y; else return x % y;} int Arith_floor (int x, int y) {return Arith_div (x, y );} int Arith_ceiling (int x, int y) {return Arith_div (x, y) + (x % y! = 0 );}Arith. c abstract data type
Abstract data type (ADT) is an interface that defines the data type and provides various operations based on this type of value.
An advanced type is abstract, because the interface hides its representation details, so that the client calls the program to depend on these details. The following is a canonicalized example of an abstract data type (ADT)-stack, which defines this type and five operations:
# Ifndef STACK_INCLUDED # define T Stack_Ttypedef struct T * T; extern T Stack_new (void); extern int Stack_empty (T stk); extern void Stack_push (T stk, void * x); extern void * Stack_pop (T stk); extern void Stack_free (T * stk); # undef T # endifStack. h implementation
Includes related header files:
#include <stddef.h>#include "assert.h"#include "mem.h"#include "stack.h"#define T Stack_T
Stack_T is a structure in which a field points to the linked list of pointers in a stack and the count of these pointers:
struct T { int count; struct elem { void *x; struct elem *link; } *head;};
Stack_new allocates and initializes a new T:
T Stack_new(void) { T stk; NEW(stk); stk->count = 0; stk->head = NULL; return stk;}
NEW is an assignment macro command in another interface. NEW (p) allocates an instance of this structure and assigns its pointer to p. Therefore, using Stack_new, you can assign a NEW Stack_T
When count = 0, Stack_empty returns 1; otherwise, 0:
int Stack_empty(T stk) { assert(stk); return stk->count == 0;}
Assert (stk) implements a checkable runtime error. It prohibits null pointers from being passed to any function in the Stack.
Stack_push and Stack_pop add or remove elements from the head of the linked list pointed to by stk-> head:
void Stack_push(T stk, void *x) { struct elem *t; assert(stk); NEW(t); t->x = x; t->link = stk->head; stk->head = t; stk->count++;}void *Stack_pop(T stk) { void *x; struct elem *t; assert(stk); assert(stk->count > 0); t = stk->head; stk->head = t->link; stk->count--; x = t->x; FREE(t); return x;}
FREE is the release macro command defined in another interface. It releases the space pointed to by the pointer parameter, and then sets the parameter as a null pointer.
void Stack_free(T *stk) { struct elem *t, *u; assert(stk && *stk); for (t = (*stk)->head; t; t = u) { u = t->link; FREE(t); } FREE(*stk);}
The complete implementation code is as follows:
# Include <stddef. h> # include "assert. h "# include" mem. h "# include" stack. h "# define T Stack_Tstruct T {int count; struct elem {void * x; struct elem * link;} * head ;}; T Stack_new (void) {T stk; NEW (stk); stk-> count = 0; stk-> head = NULL; return stk;} int Stack_empty (T stk) {assert (stk ); return stk-> count = 0;} void Stack_push (T stk, void * x) {struct elem * t; assert (stk); NEW (t ); t-> x = x; t-> link = stk-> head; stk-> head = t; stk-> count ++;} void * Stack_pop (T stk) {void * x; struct elem * t; assert (stk); assert (stk-> count> 0); t = stk-> head; stk-> head = t-> link; stk-> count --; x = t-> x; FREE (t); return x;} void Stack_free (T * stk) {struct elem * t, * u; assert (stk & * stk); for (t = (* stk)-> head; t = u) {u = t-> link; FREE (t);} FREE (* stk );}Stack. c references
C language interface and implementation-technology for creating reusable software