《设计模式解析》 第20章 来自设计模式的教益:各种工厂模式
Review Questions
Define a "factory." 工厂是用于封装对象的初始化过程和对象的创建使用规则。使得使用对象的客户只需要关注对象的使用。 | |
Name one factory pattern that was shown in a previous chapter. Name the factory patterns mentioned in this chapter 抽象工厂模式 单例模式 | |
When it comes to managing object creation, what is a good, universal rule to use? 将对象的创建和对象的使用分离。 |
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个职责,违背了“一个对象,一个职责”的原则。当对象变动的时候,不得不修改使用对象的源代码。 | |
Factories increase cohesion. What is the reason for this? 工厂模式将对象创建的管理和使用分开,使得各部分负责自己的职责,只处理自己的问题,而不处理其他问题,增加了内聚性。 | |
Factories also help in testing. In what ways is this true? 如果将对象的创建与使用放在一起,你需要对于每中情况进行测试。如果有m个使用规则,有n个对象,则需要测试m*n种情况。而由于对象创建管理与对象使用分离,因此,是相互独立的,只需要进行单独的测试即:m+n次。 |
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. 使用模式,会导致类的层次的变高,而随着越来越多的类的层次增高,使用起来也变得复杂,使用工厂模式,可以将创建类的工作独立出来,使得使用这些层次变高的类时,无需关注具体的是哪个类,简化工作。 |