Pointers on C——7 Functions.4

7.4 ADTs and Black Boxes


C facilitates the design and implementation of abstract data types, or ADTs, because of its ability to limit the scope of function and data definitions. This technique is also referred to as black box design. The basic idea behind an abstract data type is simple—a module has a functional specification, which states the work that the module will perform, and an interface specification, which defines how the module is used.However, users of the module do not need to know any details of its implementation and are prevented from accessing the module in any way other than with the defined interface.

C 可以用于设计和实现抽象数据类型(ADT. abstract data type). 因为它可以限制函数和数据定义的作用域。这个技巧也被称为黑盒(black box)设计。抽象数据类型的基本想法是很简单的——模块具有功能说明和接口说明,前者说明模块所执行的任务,后者定义模块的使用。但是,模块的用户并不需要知道模块实现的任何细节,而且除了那些定义好的接口之外,用户不能以任何方式访问模块。


Limiting access to the module is accomplished through the judicious use of the static keyword to restrict the accessibility of functions and data that are not part of the interface. For example, consider a module that maintains an address/phone number list. The module must provide functions to look up an address and to look up a phone number for a specific name. However, the manner in which the list is stored is implementation dependent, so this information is kept private within the module and is not available to the client.

限制对模块的访问是通过static 关键字的合理使用实现的,它可以限制对那些并非接口的函数和数据的访问。例如,考虑一个用于维护一个地址/电话号码列表的模块。模块必须提供函数,根据一个指定的名字查找地址和电话号码。但是,列表存储的方式是依赖于具体实现的,所以这个信息为模块所私有,客户并不知情。


The next example illustrates one possible implementation of this module.Program 7.5a shows the include file that defines the interface used by the client, and Program 7.5b shows the implementation.

下一个例子程序说明了这个模块的一种可能的实现方法。程序7.5a 定义了一个头文件,它定义了一些由客户使用的接口。程序7.6b 展示了这个模块的实现 。


/*

** Declarations for the address list module.

*/

/*

** Data characteristics

**

** Maximum lengths of the various data (includes space for the

** terminating NUL byte), and maximum number of addresses.

*/

#define NAME_LENGTH 30 /* longest name allowed */

#define ADDR_LENGTH 100 /* longest address allowed */

#define PHONE_LENGTH 11 /* longest phone # allowed */

#define MAX_ADDRESSES 1000 /* # of addresses allowed */

/*

** Interface functions

**

** Given a name, find the corresponding address.

*/

char const *

lookup_address( char const *name );

/*

** Given a name, find the corresponding phone number.

*/

char const *

lookup_phone( char const *name );

Program 7.5a Address list module: header file   addrlist. h


/*

** Abstract data type to maintain an address list.

*/

#include "addrlist.h"

#include <stdio.h>s

/*

** The three parts to each address are kept in corresponding

** elements of these three arrays.

*/

static char name[MAX_ADDRESSES][NAME_LENGTH];

static char address[MAX_ADDRESSES][ADDR_LENGTH];

static char phone[MAX_ADDRESSES][PHONE_LENGTH];

/*

** This routine locates a name in the array and returns the

** subscript of the location found. If the name does not exist,

** -1 is returned.

*/

static int

find_entry( char const *name_to_find )

{

int entry;

for( entry = 0; entry < MAX_ADDRESSES; entry += 1 )

if( strcmp( name_to_find, name[ entry ] ) == 0 )

return entry;

return -1;

}

/*

** Given a name, look up and return the corresponding address.

** If the name was not found, return a NULL pointer instead.

*/

char const *

lookup_address( char const *name )

{

int entry;

entry = find_entry( name );

if( entry == -1 )

return NULL;

else

return address[ entry ];

}

/*

** Given a name, look up and return the corresponding phone

** number. If the name was not found, return a NULL pointer

** instead.

*/

char const *

lookup_phone( char const *name )

{

int entry;

entry = find_entry( name );

if( entry == -1 )

return NULL;

else

return phone[ entry ];

}

Program 7.5b Address list module: implementation      addrlist.c


Program 7.5 is a good example of a black box. The functionality in the box is accessed through the specified interface, which in this case are the function lookup_address and lookup_phone. However, the user cannot directly access data that relate to the implementation, such as the array or the support function lookup_entrybecause these items are declared static.

程序7.5 是一个黑盒的好例子。黑盒的功能通过规定的接口访问,在这个例子里,接口是函数lookup address 和lookup__phone 。但是,用户不能直接访问和模块实现有关的数据,如数组或辅助函数find entry ,因为这些内容被声明为static 。


The power of this type of implementation is that it makes the various parts of the program more independent of one another. For example, as the address list grows larger, the simple sequential search may become too slow, or the table might become full. At that time you can rewrite the finding function to be more efficient, perhaps by using some form of hashed lookup, or you could even scrap the array altogether and dynamically allocate the space for the entries. But if the client program had been able to access the table directly, changing the organization of the table would cause the program to fail in each of these places.

这种类型的实现威力在于它使程序的各个部分相互之间更加独立。例如,随着地址列表的记录条数越来越多,简单的线性查找可能太慢,或者用于存储记录的表可能装满。此时你可以重新编写查找函数,使它更富效率,可能是通过使用某种形式的散列表查找来实现。或者,你甚至可以放弃使用数组,转而为这些记录动态分自己内存空间。但是,如果用户程序可以直接访问存储记录的表,表的组织形式如采进行了修改,就有可能导致用户程序失败。


The black box concept removes the temptation to use the implementation details by making those details unavailable. Thus, the only possible way to access the module is through its defined interface.

黑盒的概念使实现细节与外界隔绝,这就消除了用户试图直接访问这些实现细节的诱惑。这样,访问模块唯一可能的方法就是通过模决所定义的接口。


上一章 Pointers on C——7 Functions.3


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值