《設計模式解析》 第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?

設計模式只是提供了一種解決問題的範式。具體的實現細節需要根據具體場景進行適應。即使他提出的一些方案在一些場所可能不適應,但我們能夠通過他的設計理念,去重新改造,提出適合自己的方案。

 

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