这是用户在 2024-9-30 16:10 为 https://edstem.org/au/courses/17312/lessons/55764/slides/377563 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?

8.1.2 Client and server classes
8.1.2 客户端和服务器类

When writing programs with multiple classes, the programmer usually finds that a class may be written to offer functionality to other classes or, alternatively, a class uses the functionality provided by other classes. In an application these classes work together in what is termed a client–server model. The term "client–server" is used to describe the relationship between cooperating classes in an application. The server class provides a function or service to one or many clients. The clients initiate requests for these services. Examples of computer applications that use the client–server model are email or the World Wide Web.
在编写具有多个类的程序时,程序员通常会发现编写一个类来为其他类提供功能,或者一个类使用其他类提供的功能。在应用程序中,这些类在所谓的客户端-服务器模型中协同工作。术语 “client-server” 用于描述应用程序中协作类之间的关系。server 类为一个或多个客户端提供函数或服务客户端启动对这些服务的请求。使用客户端-服务器模型的计算机应用程序示例包括电子邮件或万维网。

We will look at a simple class diagram involving Tutorial, Student and Book classes.
我们将看一个简单的类图,包括 Tutorial、Student 和 Book 类。

Client and Server Classes

Server classes 服务器类

When classes provide functionality that are used by another class, we generally call these classes server classes. As evident in the class diagram above, Student is a server class as its functionality is used by the Tutorial class. Similarly, Book is a server class as its functionality is used by the Student class.
当类提供另一个类使用的功能时,我们通常将这些类称为服务器类。如上面的类图所示,Student 是一个服务器类,因为它的功能由 Tutorial 类使用。同样,Book 是一个服务器类,因为它的功能由 Student 类使用。

Client classes 客户端类

When a class uses the functionality provided by another class, we generally call these classes client classes. As evident in the class diagram above, Tutorial is a client class as it uses the functionality offered by the Student class. Similarly, Student is a client class as it uses the functionality offered by the Book class.
当一个类使用另一个类提供的功能时,我们通常将这些类称为 client 类。如上面的类图所示,Tutorial 是一个客户端类,因为它使用 Student 类提供的功能。同样,Student 是一个客户端类,因为它使用 Book 类提供的功能。

Client-server model 客户端-服务器模型

As evident from the class diagram, any given class in a program can be a client class, a server class or can be both client and server. The interaction between the classes at any one time dictates whether a class is acting as a client class or as a server class.
从类图中可以明显看出,程序中的任何给定类都可以是客户端类、服务器类,或者既可以是客户端又是服务器。类之间的交互在任何时间都决定了类是充当客户端类还是服务器类。

Generally, programs will have a mix of client classes, server classes, and classes which can act as both. The main class of a program (or the class which contains the main method to begin the program) is typically a client class.
通常,程序将混合使用 Client 端类、Server 类和可以同时充当两者的类。程序的主类(或包含启动程序的主方法的类)通常是 Client 端类。

Additionally, classes which directly interact with the user either by displaying output on the screen or accepting input via the keyboard, are also referred to as client classes.
此外,通过在屏幕上显示输出或通过键盘接受输入来直接与用户交互的类也称为客户端类。

Optimising for design 优化设计

The concept of client and server classes in programming is derived from the client-server architecture in networking. In this type of application, clients are singular machines that can make connections to a server to request a resource. The client then has time to continue with other processing while waiting for a response. The server, on the other hand, is a powerful machine or set of machines that respond to hundreds or thousands of requests every minute.
编程中 client 和 server 类的概念源自网络中的 client-server 体系结构。在这种类型的应用程序中,客户端是可以连接到服务器以请求资源的单一计算机。然后,客户端有时间在等待响应的同时继续其他处理。另一方面,服务器是一台功能强大的机器或一组机器,每分钟响应数百或数千个请求。

The advantage of the client-server model is its efficiency in delivering resources to the client. With the centralisation of data, the system is more secure and provides added security to the data. Adopting the client-server approach helps programmers achieve a well designed program.
客户端-服务器模型的优势在于它向客户端交付资源的效率。通过数据集中化,系统更加安全,并为数据提供了额外的安全性。采用客户端-服务器方法有助于程序员实现设计良好的程序。

An important consideration in program design is that the number of classes that depend on or interact directly with the user are as few as possible. Should a client class be defined, it is made dependent on another server class rather than the user.
程序设计中的一个重要考虑因素是,依赖于用户或直接与用户交互的类的数量要尽可能少。如果定义了 Client 端类,则它依赖于另一个服务器类而不是用户。

This concept of client and server classes will become more important in a later lesson when we learn how to handle errors in a Java program.
在后面的课程中,当我们学习如何处理 Java 程序中的错误时,客户端类和服务器类的概念将变得更加重要。