《设计模式解析》 第21章 Singleton 和 Double-checked模式

《设计模式解析》 第21章 Singleton 和 Double-checked模式

Review Questions

Observations

1:

What type of pattern is the Singleton? What general category of pattern does it belong to?

工厂模式

2:

What is the intent of the Singleton pattern?

让对象只初始化一次,并且让其负责自己的状态管理。且能够在全局使用相同的对象。

3:

How many objects is the Singleton responsible for creating?

可以多个对象,但均是单个实例。

4:

The Singleton uses a special method to instantiate objects. What is special about this method?

先判断是否为空,再进行实例化。保证只实例化一次。

5:

What is the difference in when to use the Singleton and Double-Checked Locking patterns?

单线程和多线程的使用。

Interpretations

1:

Is it better to have objects be responsible for handling their own single instantiation than to do it globally for the objects? Why or why not?

自己负责自己的状态管理,这样更易于处理、跟踪等。

Opinions and Applications

1:

Why do you think the Gang of Four call this pattern "Singleton"? Is it an appropriate name for what it is doing? Why or why not?

单例,在运行中,只有一个该类的实例,并且自身能够保证这点。算是名副其实了。

2:

When it was discovered that the Double-Checked Locking pattern as initially described did not work in Java, many people saw it as evidence that patterns were overhyped. I drew exactly the opposite conclusion. Do you agree with this logic? Why or why not?

设计模式只是提供了一种解决问题的范式。具体的实现细节需要根据具体场景进行适应。即使他提出的一些方案在一些场所可能不适应,但我们能够通过他的设计理念,去重新改造,提出适合自己的方案。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章