Interfaces, Operations, and Exceptions

The central focus of Slice is on defining interfaces, for example:

Slice 的重点是定义接口,例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
module M
{
struct TimeOfDay
{
short hour; // 0 - 23
short minute; // 0 - 59
short second; // 0 - 59
}

interface Clock
{
TimeOfDay getTime();
void setTime(TimeOfDay time);
}
}

This definition defines an interface type called Clock. The interface supports two operations: getTime and setTime. Clients access an object supporting the Clock interface by invoking an operation on the proxy for the object: to read the current time, the client invokes the getTime operation; to set the current time, the client invokes the setTime operation, passing an argument of type TimeOfDay.

此定义定义了一种称为 Clock 的接口类型。该接口支持两种操作:getTime 和 setTime。客户端通过在对象的代理上调用操作来访问支持 Clock 接口的对象:为了读取当前时间,客户端调用 getTime 操作;若要设置当前时间,客户端将调用 setTime 操作,并传递 TimeOfDay 类型的参数。

Invoking an operation on a proxy instructs the Ice run time to send a message to the target object. The target object can be in another address space or can be collocated (in the same process) as the caller — the location of the target object is transparent to the client. If the target object is in another (possibly remote) address space, the Ice run time invokes the operation via a remote procedure call; if the target is collocated with the client, the Ice run time bypasses the network stack altogether to deliver the request more efficiently.

在proxy上调用操作会指示 Ice 运行时向目标对象发送消息。目标对象可以位于另一个地址空间中,也可以与调用方并置(在同一进程中) — 目标对象的位置对客户端是透明的。如果目标对象位于另一个(可能是远程)地址空间中,则 Ice 运行时通过远程过程调用调用该操作;如果目标与客户端并置,则 Ice 运行时将完全绕过网络堆栈,以更有效地传递请求。

You can think of an interface definition as the equivalent of the public part of a C++ class definition or as the equivalent of a Java interface, and of operation definitions as (virtual) member functions. Note that nothing but operation definitions are allowed to appear inside an interface definition. In particular, you cannot define a type, an exception, or a data member inside an interface. This does not mean that your object implementation cannot contain state — it can, but how that state is implemented (in the form of data members or otherwise) is hidden from the client and, therefore, need not appear in the object’s interface definition.

您可以将接口定义视为 C++ 类定义的公共部分的等效项,或者是 Java 接口的等效项,并将操作定义视为(虚拟)成员函数。请注意,接口定义中只允许显示操作定义。具体而言,不能在接口内部定义类型、异常或数据成员。这并不意味着您的对象实现不能包含状态 — 它可以,但是该状态的实现方式(以数据成员或其他形式)对客户端是隐藏的,因此不需要出现在对象的接口定义中。

An Ice object has exactly one (most derived) Slice interface type. Of course, you can create multiple Ice objects that have the same type; to draw the analogy with C++, a Slice interface corresponds to a C++ class definition, whereas an Ice object corresponds to a C++ class instance (but Ice objects can be implemented in multiple different address spaces).

一个 Ice 对象只有一个(大多数派生的)Slice 接口类型。当然,您可以创建多个具有相同类型的 Ice 对象;为了与 C++ 进行类比,Slice 接口对应于 C++ 类定义,而 Ice 对象对应于 C++ 类实例(但 Ice 对象可以在多个不同的地址空间中实现)。

Ice also provides multiple interfaces for the same Ice object via a feature called facets.

Ice 还通过称为 facets 的功能为同一 Ice 对象提供多个接口。

A Slice interface defines the smallest grain of distribution in Ice: each Ice object has a unique identity (encapsulated in its proxy) that distinguishes it from all other Ice objects; for communication to take place, you must invoke operations on an object’s proxy. There is no other notion of an addressable entity in Ice. You cannot, for example, instantiate a Slice structure and have clients manipulate that structure remotely. To make the structure accessible, you must create an interface that allows clients to access the structure.

Slice 接口定义了 Ice 中最小的分布粒度:每个 Ice 对象都有一个唯一的身份(封装在其代理中),将其与所有其他 Ice 对象区分开来;要进行通信,必须在对象的代理上调用操作。Ice 中没有其他可寻址实体的概念。例如,您不能实例化 Slice 结构并让客户端远程操作该结构。要使结构易于访问,您必须创建一个允许客户端访问结构的接口。