我该如何理解抽象工厂的动机?
How shall I understand the motivation of abstract factory?
来自 GoF 的设计模式
ABSTRACT FACTORY
Intent
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
Motivation
Consider a user interface toolkit that supports multiple look-and-feel
standards, such as Motif and Presentation Manager. Different
look-and-feels define different appearances and behaviors for user
interface "widgets" like scroll bars, windows, and buttons. To be
portable across look-and-feel standards, an application should not
hard-code its widgets for a particular look and feel. Instantiating
look-and-feel-specific classes of widgets throughout the application
makes it hard to change the look and feel later.
We can solve this problem by defining an abstract WidgetFactory class
that declares an interface for creating each basic kind of widget.
There's also an abstract class for each kind of widget, and
concrete subclasses implement widgets for specific look-and-feel
standards. WidgetFactory's interface has an operation that returns a
new widget object for each abstract widget class. Clients call these
operations to obtain widget instances, but clients aren't aware of the
concrete classes they're using. Thus clients stay independent of the
prevailing look and feel.
我想知道以下是什么意思:
To be portable across look-and-feel standards, an application should
not hard-code its widgets for a particular look and feel.
和
Instantiating look-and-feel-specific classes of widgets throughout the application makes it hard to change
the look and feel later.
特别是,在不使用抽象工厂的情况下 class,代码是什么样的,为什么不好?谢谢
当代码中的某些内容请求小部件时(比如 Window),not to hardcode
意味着您不应该这样做:
var window = new DesktopWindow()
大概你在很多地方创造了 windows,所以你会说,其中 100 个 window 创造。
如果您执行了上述操作,您就将您的应用程序硬编码为在任何地方都有桌面 windows。然后,如果你想要一个移动版本(或其他外观),你将不得不修改所有 100 个地方来做这样的事情:
if(environment.IsMobile){
window = new MobileWindow();
} else {
window = new DesktopWindow();
}
window创建的逻辑越复杂,上面的代码就会变得越难越大。你最终会得到很多重复的代码,它们做同样的事情,很难维护。这就是 makes it hard to change look and feel later
部分的意思。
使用抽象工厂,您的代码不关心它得到什么window:
IWindow window = windowFactory.Create();
并且工厂实际上根据上下文知道 window 到 return 是什么。调用者只是使用公共接口 IWindow
来实现其目标。 Factory 通过将创建逻辑放在一个地方并使添加新的 Window
类型(外观和感觉)更容易来简化代码。
来自 GoF 的设计模式
ABSTRACT FACTORY
Intent
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
Motivation
Consider a user interface toolkit that supports multiple look-and-feel standards, such as Motif and Presentation Manager. Different look-and-feels define different appearances and behaviors for user interface "widgets" like scroll bars, windows, and buttons. To be portable across look-and-feel standards, an application should not hard-code its widgets for a particular look and feel. Instantiating look-and-feel-specific classes of widgets throughout the application makes it hard to change the look and feel later.
We can solve this problem by defining an abstract WidgetFactory class that declares an interface for creating each basic kind of widget. There's also an abstract class for each kind of widget, and concrete subclasses implement widgets for specific look-and-feel standards. WidgetFactory's interface has an operation that returns a new widget object for each abstract widget class. Clients call these operations to obtain widget instances, but clients aren't aware of the concrete classes they're using. Thus clients stay independent of the prevailing look and feel.
我想知道以下是什么意思:
To be portable across look-and-feel standards, an application should not hard-code its widgets for a particular look and feel.
和
Instantiating look-and-feel-specific classes of widgets throughout the application makes it hard to change the look and feel later.
特别是,在不使用抽象工厂的情况下 class,代码是什么样的,为什么不好?谢谢
当代码中的某些内容请求小部件时(比如 Window),not to hardcode
意味着您不应该这样做:
var window = new DesktopWindow()
大概你在很多地方创造了 windows,所以你会说,其中 100 个 window 创造。 如果您执行了上述操作,您就将您的应用程序硬编码为在任何地方都有桌面 windows。然后,如果你想要一个移动版本(或其他外观),你将不得不修改所有 100 个地方来做这样的事情:
if(environment.IsMobile){
window = new MobileWindow();
} else {
window = new DesktopWindow();
}
window创建的逻辑越复杂,上面的代码就会变得越难越大。你最终会得到很多重复的代码,它们做同样的事情,很难维护。这就是 makes it hard to change look and feel later
部分的意思。
使用抽象工厂,您的代码不关心它得到什么window:
IWindow window = windowFactory.Create();
并且工厂实际上根据上下文知道 window 到 return 是什么。调用者只是使用公共接口 IWindow
来实现其目标。 Factory 通过将创建逻辑放在一个地方并使添加新的 Window
类型(外观和感觉)更容易来简化代码。