• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            emptysoul

              C++博客 :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
              25 Posts :: 0 Stories :: 23 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(18)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            AbstractFactory模式解決的問題是創(chuàng)建一組相關(guān)或者相互依賴的對象。
            我們以一個電腦產(chǎn)品的例子來說明。
            我們現(xiàn)在要生產(chǎn)電腦產(chǎn)品,假設(shè)電腦產(chǎn)品現(xiàn)在只有臺式機及筆記本兩種,我們需要建一個工廠用來生產(chǎn)電腦產(chǎn)品,而工廠中可以生產(chǎn)不同品牌的電腦,對于每個品牌,我們分別建立相應(yīng)的品牌工廠,負責(zé)生產(chǎn)各自的品牌產(chǎn)品,假設(shè)現(xiàn)在有DELL及IBM兩個品牌工廠,那么現(xiàn)在每個工廠都可以生產(chǎn)各自的臺式機及筆記本了。
            其類圖如下:
             

            以下是實現(xiàn)代碼:
            //DesktopProduct.h
            class DesktopProduct  
            {
            public:
                
            virtual ~DesktopProduct();
            protected:
                DesktopProduct();
            };

            //DesktopProduct.cpp
            #include "stdafx.h"
            #include 
            "DesktopProduct.h"

            DesktopProduct::DesktopProduct()
            {

            }

            DesktopProduct::
            ~DesktopProduct()
            {

            }

            //DELLDesktopProduct.h
            #include "DesktopProduct.h"

            class DELLDesktopProduct : public DesktopProduct  
            {
            public:
                DELLDesktopProduct();
                
            virtual ~DELLDesktopProduct();

            };

            //DELLDesktopProduct.cpp
            #include "stdafx.h"
            #include 
            "DELLDesktopProduct.h"
            #include 
            <iostream>

            using namespace std;

            DELLDesktopProduct::DELLDesktopProduct()
            {
                cout 
            << "創(chuàng)建DELL臺式機" << endl;
            }

            DELLDesktopProduct::
            ~DELLDesktopProduct()
            {

            }

            //IBMDesktopProduct.h
            #include "DesktopProduct.h"

            class IBMDesktopProduct : public DesktopProduct
            {
            public:
                IBMDesktopProduct();
                
            virtual ~IBMDesktopProduct();

            };

            //IBMDesktopProduct.cpp
            #include "stdafx.h"
            #include 
            "IBMDesktopProduct.h"
            #include 
            <iostream>

            using namespace std;

            IBMDesktopProduct::IBMDesktopProduct()
            {
                cout 
            << "創(chuàng)建IBM臺式機" << endl;
            }

            IBMDesktopProduct::
            ~IBMDesktopProduct()
            {

            }

            //NotebookProduct.h
            class NotebookProduct 
            {
            public:
                
            virtual ~NotebookProduct();
            protected:
                NotebookProduct();
            };

            //NotebookProduct.cpp
            #include "stdafx.h"
            #include 
            "NotebookProduct.h"

            NotebookProduct::NotebookProduct()
            {

            }

            NotebookProduct::
            ~NotebookProduct()
            {

            }

            //DELLNotebookProduct.h
            #include "NotebookProduct.h"

            class DELLNotebookProduct : public NotebookProduct  
            {
            public:
                DELLNotebookProduct();
                
            virtual ~DELLNotebookProduct();

            };

            //DELLNotebookProduct.cpp
            #include "stdafx.h"
            #include 
            "DELLNotebookProduct.h"
            #include 
            <iostream>

            using namespace std;

            DELLNotebookProduct::DELLNotebookProduct()
            {
                cout 
            << "創(chuàng)建DELL筆記本" << endl;
            }

            DELLNotebookProduct::
            ~DELLNotebookProduct()
            {

            }

            //IBMNotebookProduct.h
            #include "NotebookProduct.h"

            class IBMNotebookProduct : public NotebookProduct
            {
            public:
                IBMNotebookProduct();
                
            virtual ~IBMNotebookProduct();

            };

            //IBMNotebookProduct.cpp
            using namespace std;

            IBMNotebookProduct::IBMNotebookProduct()
            {
                cout 
            << "創(chuàng)建IBM筆記本" << endl;
            }

            IBMNotebookProduct::
            ~IBMNotebookProduct()
            {

            }

            //AbstractFactory.h
            class DesktopProduct;
            class NotebookProduct;
            class AbstractFactory  
            {
            public:
                
            virtual ~AbstractFactory();

                
            virtual DesktopProduct* CreateDesktopProduct() = 0;
                
            virtual NotebookProduct* CreateNotebookProduct() = 0;
            protected:
                AbstractFactory();
            };

            //AbstractFactory.cpp
            #include "stdafx.h"
            #include 
            "AbstractFactory.h"

            AbstractFactory::AbstractFactory()
            {

            }

            AbstractFactory::
            ~AbstractFactory()
            {

            }

            //DELLFactory.h
            #include "AbstractFactory.h"

            class DELLFactory : public AbstractFactory
            {
            public:
                DELLFactory();
                
            virtual ~DELLFactory();

                DesktopProduct
            * CreateDesktopProduct();
                NotebookProduct
            * CreateNotebookProduct();
            };

            //DELLFactory.cpp
            #include "stdafx.h"
            #include 
            "DELLFactory.h"
            #include 
            "DELLDesktopProduct.h"
            #include 
            "DELLNotebookProduct.h"

            DELLFactory::DELLFactory()
            {

            }

            DELLFactory::
            ~DELLFactory()
            {

            }

            DesktopProduct
            * DELLFactory::CreateDesktopProduct()
            {
                
            return new DELLDesktopProduct;
            }

            NotebookProduct
            * DELLFactory::CreateNotebookProduct()
            {
                
            return new DELLNotebookProduct;
            }

            //IBMFactory.h
            #include "AbstractFactory.h"

            class IBMFactory : public AbstractFactory
            {
            public:
                IBMFactory();
                
            virtual ~IBMFactory();

                DesktopProduct
            * CreateDesktopProduct();
                NotebookProduct
            * CreateNotebookProduct();
            };

            //IBMFactory.cpp
            #include "stdafx.h"
            #include 
            "IBMFactory.h"
            #include 
            "IBMDesktopProduct.h"
            #include 
            "IBMNotebookProduct.h"

            IBMFactory::IBMFactory()
            {

            }

            IBMFactory::
            ~IBMFactory()
            {

            }

            DesktopProduct
            * IBMFactory::CreateDesktopProduct()
            {
                
            return new IBMDesktopProduct;
            }
            NotebookProduct
            * IBMFactory::CreateNotebookProduct()
            {
                
            return new IBMNotebookProduct;
            }

            //Main.cpp
            #include "stdafx.h"
            #include 
            "AbstractFactory.h"
            #include 
            "DELLFactory.h"
            #include 
            "IBMFactory.h"
            #include 
            "DesktopProduct.h"
            #include 
            "DELLDeskProduct.h"
            #include 
            "IBMDesktopProduct.h"
            #include 
            "NotebookProduct.h"
            #include 
            "DELLNotebookProduct.h"
            #include 
            "IBMNotebookProduct.h"

            int main(int argc, char* argv[])
            {
                AbstractFactory
            * fac = new DELLFactory();
                fac
            ->CreateDesktopProduct();
                fac
            ->CreateNotebookProduct();
                delete fac;
                fac 
            = new IBMFactory();
                fac
            ->CreateDesktopProduct();
                fac
            ->CreateNotebookProduct();
                delete fac;

                return 0;
            }

            最后輸出為:
            創(chuàng)建DELL臺式機
            創(chuàng)建DELL筆記本
            創(chuàng)建IBM臺式機
            創(chuàng)建IBM筆記本
            posted on 2009-02-07 21:49 emptysoul 閱讀(446) 評論(1)  編輯 收藏 引用

            Feedback

            # re: 設(shè)計模式-抽象工廠 2009-02-19 13:17 zhjiawei_cn
            寫得很好,我可以轉(zhuǎn)載到我的博客上嗎?  回復(fù)  更多評論
              


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            亚洲第一永久AV网站久久精品男人的天堂AV | 久久精品这里只有精99品| 伊人久久精品无码av一区| 亚洲成色WWW久久网站| 久久国产免费观看精品3| 99国内精品久久久久久久| 国内精品久久久久影院亚洲| 999久久久免费精品国产| 亚洲国产香蕉人人爽成AV片久久| 91精品国产91久久久久福利| 国产精品日韩欧美久久综合| 国产精品久久午夜夜伦鲁鲁| 久久人爽人人爽人人片AV | 久久99国产精品成人欧美| 久久亚洲精品中文字幕| 99久久国产免费福利| 一本一本久久aa综合精品| 国产精品一区二区久久精品无码 | 99久久精品免费观看国产| 精品国产日韩久久亚洲| 久久久91精品国产一区二区三区 | 久久99精品国产麻豆不卡| 亚洲狠狠婷婷综合久久蜜芽 | 久久婷婷五月综合成人D啪| 热久久这里只有精品| 久久亚洲sm情趣捆绑调教| 精品国产青草久久久久福利| 久久久久久国产精品免费无码| 亚洲国产香蕉人人爽成AV片久久| 精品久久久久久国产91| 亚洲狠狠久久综合一区77777| 亚洲综合熟女久久久30p| 亚洲另类欧美综合久久图片区| 国产成人久久久精品二区三区| 国产麻豆精品久久一二三| 久久无码AV中文出轨人妻| 久久夜色精品国产www| 激情久久久久久久久久| 国产激情久久久久影院老熟女免费| 国产一级持黄大片99久久| 久久综合噜噜激激的五月天|