• <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综合合色蜜桃网 | 久久综合九色综合网站| 久久久久人妻一区二区三区vr | 久久精品国产乱子伦| 国产精品一区二区久久精品| 久久高潮一级毛片免费| 综合久久国产九一剧情麻豆| 久久精品亚洲男人的天堂| 国产亚洲精品自在久久| 久久人人爽人人爽人人av东京热 | 777午夜精品久久av蜜臀| 超级碰久久免费公开视频| 婷婷综合久久中文字幕蜜桃三电影| 精品久久人人爽天天玩人人妻| 2021国产精品午夜久久| 亚洲伊人久久成综合人影院| 97久久综合精品久久久综合| 亚洲伊人久久精品影院| 国内精品久久久久久久久电影网| 亚洲国产一成人久久精品| 伊人久久大香线蕉综合5g| 久久久久四虎国产精品| 国产午夜精品久久久久九九| 精品综合久久久久久97| 色婷婷久久久SWAG精品| 久久久久久久综合综合狠狠| 久久er国产精品免费观看2| 男女久久久国产一区二区三区| 久久婷婷色综合一区二区| 欧美久久久久久精选9999| 国产999精品久久久久久| 久久99精品国产麻豆宅宅| 国产亚洲欧美精品久久久 | 欧美日韩中文字幕久久伊人| 久久久久久午夜成人影院| 久久亚洲AV成人无码电影| 久久精品人人槡人妻人人玩AV| 77777亚洲午夜久久多喷| 久久久久人妻精品一区二区三区|