SqlServer中解析字符串的表值函数
2015-09-21 16:35
246 查看
经典Singletons
Sometimes you want to override the default way in which Ruby creates objects. As an
example, let’s look at our jukebox. Because we’ll have many jukeboxes, spread all over
the country, we want to make maintenance as easy as possible. Part of the requirement
is to log everything that happens to a jukebox: the songs played, the money received,
the strange fluids poured into it, and so on. Because we want to reserve the network
bandwidth for music, we’ll store these log files locally. This means we’ll need a class
that handles logging. However, we want only one logging object per jukebox, and we
want that object to be shared among all the other objects that use it.
Enter the Singleton pattern, documented in Design Patterns [GHJV95]. We’ll arrange
things so that the only way to create a logging object is to call MyLogger.create, and
we’ll ensure that only one logging object is ever created.
class MyLogger
private_class_method :new
@@logger = nil
def MyLogger.create
@@logger = new unless @@logger
@@logger
end
end
By making MyLogger’s new method private, we prevent anyone from creating a logging
object using the conventional constructor. Instead, we provide a class method,
MyLogger.create. This method uses the class variable @@logger to keep a reference
to a single instance of the logger, returning that instance every time it is called.3 We
can check this by looking at the object identifiers the method returns.
Sometimes you want to override the default way in which Ruby creates objects. As an
example, let’s look at our jukebox. Because we’ll have many jukeboxes, spread all over
the country, we want to make maintenance as easy as possible. Part of the requirement
is to log everything that happens to a jukebox: the songs played, the money received,
the strange fluids poured into it, and so on. Because we want to reserve the network
bandwidth for music, we’ll store these log files locally. This means we’ll need a class
that handles logging. However, we want only one logging object per jukebox, and we
want that object to be shared among all the other objects that use it.
Enter the Singleton pattern, documented in Design Patterns [GHJV95]. We’ll arrange
things so that the only way to create a logging object is to call MyLogger.create, and
we’ll ensure that only one logging object is ever created.
class MyLogger
private_class_method :new
@@logger = nil
def MyLogger.create
@@logger = new unless @@logger
@@logger
end
end
By making MyLogger’s new method private, we prevent anyone from creating a logging
object using the conventional constructor. Instead, we provide a class method,
MyLogger.create. This method uses the class variable @@logger to keep a reference
to a single instance of the logger, returning that instance every time it is called.3 We
can check this by looking at the object identifiers the method returns.
相关文章推荐
- oracle 11g对大表中添加DEFAULT值的NOT NULL字段速度有大幅度的提升
- 如何修复VUM在客户端启用之后报数据库连接失败的问题
- zz entity framework vs linq to sql
- mysql编译问题集
- 常见SQL注入函数
- mysql之视图
- 如何修复VUM在客户端启用之后报数据库连接失败的问题
- ASE中的主要数据库
- Memcache系列(一)Linux安装memcache
- Code Frist for Mysql 实例
- mysql数据库5.6.24安装与优化配置
- redis主从切换的重订阅
- EXPLAIN 关键字可以 查看 sql执行 的详细过程
- EXPLAIN 关键字可以 查看 sql执行 的详细过程
- CodeFirst for MySql
- 数据库性能复习
- 中国 省市区县 数据库SQL 脚本(含邮编)
- redis修改的源代码zincrby,hincrby命令
- oracle10G 数据泵技术
- sql去重查询