《設計模式解析》 第20章 來自設計模式的教益:各種工廠模式

《設計模式解析》 第20章 來自設計模式的教益:各種工廠模式

Review Questions

Observation

1:

Define a "factory."

工廠是用於封裝對象的初始化過程和對象的創建使用規則。使得使用對象的客戶只需要關注對象的使用。

2:

Name one factory pattern that was shown in a previous chapter. Name the factory patterns mentioned in this chapter

抽象工廠模式

單例模式

3:

When it comes to managing object creation, what is a good, universal rule to use?

將對象的創建和對象的使用分離。

Interpretation

1:

Developers who are new to object-oriented programming often lump the management of object creation in with object instantiation. What is wrong with doing this?

將對象創建的管理和對象實例化混在一起,這使得一個對象擁有了至少2個職責,違背了“一個對象,一個職責”的原則。當對象變動的時候,不得不修改使用對象的源代碼。

2:

Factories increase cohesion. What is the reason for this?

工廠模式將對象創建的管理和使用分開,使得各部分負責自己的職責,只處理自己的問題,而不處理其他問題,增加了內聚性。

3:

Factories also help in testing. In what ways is this true?

如果將對象的創建與使用放在一起,你需要對於每中情況進行測試。如果有m個使用規則,有n個對象,則需要測試m*n種情況。而由於對象創建管理與對象使用分離,因此,是相互獨立的,只需要進行單獨的測試即:m+n次。

Opinion and Application

1:

Factories are useful for more than just deciding which object to create or use. They also help with encapsulating design by solving a problem created by patterns? Evaluate this argument.

使用模式,會導致類的層次的變高,而隨着越來越多的類的層次增高,使用起來也變得複雜,使用工廠模式,可以將創建類的工作獨立出來,使得使用這些層次變高的類時,無需關注具體的是哪個類,簡化工作。

 

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