虽不能至,心向往之

—— 巴人也,操C++口音,混迹于京师,勉强度日……《史记·corelito列传》
posts - 8, comments - 15, trackbacks - 0, articles - 0

相对前一篇来说,这一篇中所涉及的例子程序,在使用抽象工厂方法的前后进行了一个比较,我们能发现的是:在使用抽象工厂方法之后,程序的实现显得更加优雅。

这是原来的:

 1 #define MOTIF
 2 
 3 class Widget {
 4 public:
 5    virtual void draw() = 0;
 6 };
 7 
 8 class MotifButton : public Widget {
 9 public:
10    void draw() { cout << "MotifButton\n"; }
11 };
12 class MotifMenu : public Widget {
13 public:
14    void draw() { cout << "MotifMenu\n"; }
15 };
16 
17 class WindowsButton : public Widget {
18 public:
19    void draw() { cout << "WindowsButton\n"; }
20 };
21 class WindowsMenu : public Widget {
22 public:
23    void draw() { cout << "WindowsMenu\n"; }
24 };
25 
26 void display_window_one() {
27 #ifdef MOTIF
28    Widget* w[] = { new MotifButton,
29                    new MotifMenu };
30 #else // WINDOWS
31    Widget* w[] = { new WindowsButton,
32                    new WindowsMenu };
33 #endif
34    w[0]->draw();  w[1]->draw();
35 }
36 
37 void display_window_two() {
38 #ifdef MOTIF
39    Widget* w[] = { new MotifMenu,
40                    new MotifButton };
41 #else // WINDOWS
42    Widget* w[] = { new WindowsMenu,
43                    new WindowsButton };
44 #endif
45    w[0]->draw();  w[1]->draw();
46 }
47 
48 int main() {
49 #ifdef MOTIF
50    Widget* w = new MotifButton;
51 #else // WINDOWS
52    Widget* w = new WindowsButton;
53 #endif
54    w->draw();
55    display_window_one();
56    display_window_two();
57 }

输出为:
MotifButton
MotifButton
MotifMenu
MotifMenu
MotifButton


下面是使用抽象工厂方法后的程序:
 1 #define WINDOWS
 2 
 3 class Widget {
 4 public:
 5    virtual void draw() = 0;
 6 };
 7 
 8 class MotifButton : public Widget {
 9 public:
10    void draw() { cout << "MotifButton\n"; }
11 };
12 class MotifMenu : public Widget {
13 public:
14    void draw() { cout << "MotifMenu\n"; }
15 };
16 
17 class WindowsButton : public Widget {
18 public:
19    void draw() { cout << "WindowsButton\n"; }
20 };
21 class WindowsMenu : public Widget {
22 public:
23    void draw() { cout << "WindowsMenu\n"; }
24 };
25 
26 class Factory {
27 public:
28    virtual Widget* create_button() = 0;
29    virtual Widget* create_menu() = 0;
30 };
31 
32 class MotifFactory : public Factory {
33 public:
34    Widget* create_button() {
35       return new MotifButton; }
36    Widget* create_menu()   {
37       return new MotifMenu; }
38 };
39 
40 class WindowsFactory : public Factory {
41 public:
42    Widget* create_button() {
43       return new WindowsButton; }
44    Widget* create_menu()   {
45       return new WindowsMenu; }
46 };
47 
48 Factory* factory;
49 
50 void display_window_one() {
51    Widget* w[] = { factory->create_button(),
52                    factory->create_menu() };
53    w[0]->draw();  w[1]->draw();
54 }
55 
56 void display_window_two() {
57    Widget* w[] = { factory->create_menu(),
58                    factory->create_button() };
59    w[0]->draw();  w[1]->draw();
60 }
61 
62 int main() {
63 #ifdef MOTIF
64    factory = new MotifFactory;
65 #else // WINDOWS
66    factory = new WindowsFactory;
67 #endif
68 
69    Widget* w = factory->create_button();
70    w->draw();
71    display_window_one();
72    display_window_two();
73 }

同样的在最后的输出:
WindowsButton
WindowsButton
WindowsMenu
WindowsMenu
WindowsButton

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理