《设计模式解析》 第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.

使用模式,会导致类的层次的变高,而随着越来越多的类的层次增高,使用起来也变得复杂,使用工厂模式,可以将创建类的工作独立出来,使得使用这些层次变高的类时,无需关注具体的是哪个类,简化工作。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值