计算机文章翻译(英译汉)第二部分 程序代码不用翻译

来源:百度知道 编辑:UC知道 时间:2024/09/23 10:33:37
EXAMPLE
During the discussion of the Singleton pattern, we designed a message logging
class FileLogger as a singleton. The FileLogger class maintains a class
variable logger of the FileLogger type. This variable is used to hold the singleton
FileLogger instance. The FileLogger class offers a class-level method get-
FileLogger that can be used by different client objects to access the singleton
FileLogger instance. As part of the getFileLogger method implementation,
the FileLogger checks to see if the singleton instance has already been created.
Checking to see if the class variable logger is null does this. If logger is found
to be uninitialized, a FileLogger instance is created by invoking its private
constructor and is assigned to the logger class variable. This implementation of
the getFileLogger method works fine in a single-threaded environment. In
a multithreaded environment, it is possible for two threads to simultaneou

为例
在讨论的单身模式,我们设计了一个消息日志
一流的作为一个单身。一流的保持一类
变量记录的类型。这个变数是用来举行单身
实例。一流的提供了一流水平的方法获得,
可用于不同的客户对象访问单身
实例。作为方法执行,
在检查是否单身例如已经建立。
检查,看看类变量记录是零,这是否。如果记录被发现
将未初始化的,一个例如创建援引其私人
构造和分配给记录级变量。这执行
该罚款方法在单线程环境。在
一个多线程环境中,是有可能的两个线程同时
执行方法,看看类变量记录为空
,因此,初始化记录两次。这意味着,私营
构造得到援引两次。
公共类实现记录器(
私营静态记录仪;
私营( ) (

公共静态( ) (
如果(记录==空) (
记录=新( ) ;

返回记录;

公众同步无效日志(字符串味精) (
;
( “ log.txt ” ,味精,真实,真实) ;


变量初始化记录两次在这个例子中不会导致错误。
这是因为私人构造并不做任何复杂的,
关键的初始化。相反,如果单身的构造方法执行此类行动作为打开一个套接字连接上特定的端口,执行两次构造可能会导致一个错误。

为例
在讨论的单身模式,我们设计了一个消息日志
一流的FileLogger作为一个单身。一流的FileLogger保持一类
变量记录的FileLogger类型。这个变数是用来举行单身
FileLogger实例。一流的FileLogger提供了一流水平的方法获得,
FileLogger ,可用于不同的客户对象访问单身
FileLogger实例。作为getFileLogger方法执行,
在FileLogger检查是否单身例如已经建立。
检查,看看类变量记录是零,这是否。如果记录被发现
将未初始化的,一个FileLogge