• <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++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              25 Posts :: 0 Stories :: 23 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(18)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

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

            以下是實現代碼:
            //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 
            << "創建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 
            << "創建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 
            << "創建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 
            << "創建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;
            }

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

            Feedback

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

            色播久久人人爽人人爽人人片aV| 狠狠色噜噜色狠狠狠综合久久| 久久亚洲精精品中文字幕| 国产婷婷成人久久Av免费高清| 久久精品一本到99热免费| 亚洲一区中文字幕久久| 久久亚洲精品无码观看不卡| 久久中文字幕人妻丝袜| 77777亚洲午夜久久多喷| 欧美成a人片免费看久久| 人妻少妇久久中文字幕| 国产精品一区二区久久精品无码 | 91精品观看91久久久久久| 亚洲另类欧美综合久久图片区| 久久香综合精品久久伊人| 久久久久99精品成人片| 欧洲成人午夜精品无码区久久| 99久久国产综合精品成人影院| 久久无码专区国产精品发布| 91久久精一区二区三区大全| 亚洲国产成人精品女人久久久| 久久国产精品成人影院| 中文字幕久久亚洲一区| 国产精品日韩深夜福利久久| 久久99精品久久久久久hb无码| 亚洲美日韩Av中文字幕无码久久久妻妇| 久久久久人妻精品一区二区三区| 久久成人小视频| 香蕉久久影院| 午夜福利91久久福利| 久久国产午夜精品一区二区三区| 香港aa三级久久三级| WWW婷婷AV久久久影片| 久久久久亚洲精品无码蜜桃| 亚洲国产精品久久久天堂 | 精品午夜久久福利大片| 国产婷婷成人久久Av免费高清| 色婷婷综合久久久中文字幕| 久久国产劲爆AV内射—百度| 欧美伊人久久大香线蕉综合| 亚洲欧洲中文日韩久久AV乱码|