• <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 閱讀(446) 評論(1)  編輯 收藏 引用

            Feedback

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

            99久久国产亚洲高清观看2024| 久久久久亚洲AV无码专区首JN | 狠狠色丁香久久婷婷综| 精品久久久无码人妻中文字幕豆芽| 久久久久久人妻无码| 日韩亚洲欧美久久久www综合网| 久久久久无码中| 性高湖久久久久久久久| 国产激情久久久久影院小草| 热99RE久久精品这里都是精品免费| 国产精品久久久久久| 久久精品免费一区二区| 精品多毛少妇人妻AV免费久久| 噜噜噜色噜噜噜久久| 97精品伊人久久久大香线蕉 | 国内精品久久久久久野外| 性高湖久久久久久久久AAAAA| 精品国产乱码久久久久久1区2区| 一本久久综合亚洲鲁鲁五月天| 久久99久久99小草精品免视看 | 国产精品亚洲综合久久| 久久这里只有精品久久| 亚洲国产精品无码久久久不卡| 亚洲精品久久久www| 久久九九久精品国产免费直播| 99国内精品久久久久久久| 精品国产乱码久久久久久郑州公司| 人妻无码αv中文字幕久久琪琪布| 91精品日韩人妻无码久久不卡| 国产精品美女久久久久久2018| 久久精品中文字幕一区| 久久人妻少妇嫩草AV蜜桃| 亚洲精品成人网久久久久久| 日韩久久久久中文字幕人妻| 久久久久无码国产精品不卡| 久久久久久国产精品免费免费| 热久久国产精品| 国产A级毛片久久久精品毛片| 国产精品午夜久久| 久久综合伊人77777| 久久久久亚洲爆乳少妇无|