《设计模式解析》 第11章 Abstract Factory 模式 复习题

来源:互联网 发布:深圳种植牙 知乎 编辑:程序博客网 时间:2024/05/18 01:48

《设计模式解析》 第11章 Abstract Factory 模式 复习题

 

Review Questions

Observations

1:

Although using "switches" can be a reasonable solution to a problem that requires choosing among alternatives, it caused problems for the driver problem discussed in this chapter.

  • What were these problems?
    1、当需要添加另外一种驱动程序时,需要在多个地方进行修改。
    2、对于驱动选择的确定与驱动的使用代码混在一块,使得内聚性降低、耦合度变高。
  • What might a switch indicate the need for?
    1、利用多肽,进行抽象。
    2、利用反射,进行类的识别。

2:

Why is this pattern named "Abstract Factory"?
他所要创建的东西本身是由抽象所定义的,工厂各种变化的实现如何选择,模式没有具体规定。

3:

What are the three key strategies in the Abstract Factory?

“找到变化并封装之”

“优先利用组合而非继承”

“针对接口而不是实现进行设计”

 

4:

In this pattern, there are two kinds of factories.

  • What does the "Abstract Factory" class do?
    抽象工厂定义了各个要创建的对象的创建方法,提供了接口。
  • What do the "concrete factory" classes do?
    实现抽象工厂中的对象创建方法。

5:

What are the consequences of the Abstract Factory pattern?

使得对象的创建于对象的使用分离开来,使用者无需关心对象是如何创建的。

Interpretations

1:

The Gang of Four says that the intent of the Abstract Factory pattern is to "provide an interface for creating families of related or dependent objects without specifying their concrete classes."

  • What does this mean?
    使用抽象工厂模式,只需要使用接口通过多态进行对象的获取,不需要指定需要获取哪类的对象(在其他地方已经定义过),因此,可以使得客户代码免受外界的影响。
  • Give an example.
    使用抽象工厂获取不同的棋子和棋盘,并对其进行操作,这样,客户代码中,可以直接使用而无需判定是何种对象。

Opinions and Applications

1:

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

与工厂方法不同,抽象工厂模式仅仅定义了需要创建的对象的方法,利用抽象定义了对象本身。使用者无需理会具体的实现。

2:

How do you know when to use the Abstract Factory pattern?

1、当希望将某组对象的创建与对象的使用相隔离,因为该组对象有多重的形式。

2、当switch语句相互耦合时,考虑使用抽象工厂来进行解耦。

3、当你希望隐藏对象的创建方法时。