• <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>

            Design&Art

            C++博客 首頁 新隨筆 聯系 聚合 管理
              26 Posts :: 0 Stories :: 38 Comments :: 0 Trackbacks

            工廠方法和抽象工廠實際上是從不同角度在描述問題。
            工廠方法描述了具體產品的創建,而抽象工廠描述的是產品系列的組織。

            // ?Computer.cpp?:?Defines?the?entry?point?for?the?console?application.
            //

            #include?
            " stdafx.h "
            #include?
            < string >
            #include?
            < iostream >

            using ? namespace ?std;

            class ?Ram {} ;
            class ?IBMRam:? public ?Ram {} ;
            class ?HPRam:? public ?Ram {} ;
            class ?Cpu {} ;
            class ?IBMCpu:? public ?Cpu {} ;
            class ?HPCpu:? public ?Cpu {} ;

            class ?Computer
            {
            public :
            ?Computer(
            string ?strName,?Ram * ?pRam,?Cpu * ?pCpu)
            ?
            {
            ??m_strName?
            = ?strName;
            ??m_pRam?
            = ?pRam;
            ??m_pCpu?
            = ?pCpu;
            ??cout
            << " A? " << ?m_strName? << " ?computer?is?produced " << endl;
            ?}

            ?
            ~ Computer()
            ?
            {
            ??delete?m_pRam;
            ??delete?m_pCpu;
            ??cout
            << " A? " << ?m_strName? << " ?computer?is?deleted " << endl;
            ?}

            public :
            ?
            string ?m_strName;
            private :
            ?Ram
            * ?m_pRam;
            ?Cpu
            * ?m_pCpu;
            }
            ;

            class ?ComputerProducer
            {
            public :
            ?Computer
            * ?createComputer()
            ?
            {
            ??
            return ? new ?Computer(setName(),?createRam(),?createCpu());
            ?}

            ?
            virtual ?Ram * ?createRam()? = ? 0 ;
            ?
            virtual ?Cpu * ?createCpu()? = ? 0 ;
            ?
            virtual ? string ?setName()? = ? 0 ;
            }
            ;

            class ?IBMProducer:? public ?ComputerProducer
            {
            public :
            ?
            virtual ?Ram * ?createRam()
            ?
            {
            ??cout
            << " A?IBMRam?is?producted " << endl;
            ??
            return ? new ?IBMRam;
            ?}

            ?
            virtual ?Cpu * ?createCpu()
            ?
            {
            ??cout
            << " A?IBMCPU?is?producted " << endl;
            ??
            return ? new ?IBMCpu;
            ?}

            ?
            virtual ? string ?setName()
            ?
            {
            ??
            return ? string ( " IBM " );
            ?}

            }
            ;

            class ?HPProducer:? public ?ComputerProducer
            {
            public :
            ?
            virtual ?Ram * ?createRam()
            ?
            {
            ??cout
            << " A?HPRam?is?producted " << endl;
            ??
            return ? new ?HPRam;
            ?}

            ?
            virtual ?Cpu * ?createCpu()
            ?
            {
            ??cout
            << " A?HPCPU?is?producted " << endl;
            ??
            return ? new ?HPCpu;
            ?}

            ?
            virtual ? string ?setName()
            ?
            {
            ??
            return ? string ( " HP " );
            ?}

            }
            ;

            int ?main( int ?argc,? char * ?argv[])
            {
            ?
            // ?client?code
            ?ComputerProducer * ?pIBMFac = ? new ?IBMProducer;
            ?ComputerProducer
            * ?pHPFac? = ? new ?HPProducer;
            ?Computer
            * ?pIBMComputer? = ?pIBMFac -> createComputer();
            ?Computer
            * ?pHPComputer? = ?pHPFac? -> createComputer();
            ?delete?pIBMComputer;
            ?delete?pHPComputer;
            ?delete?pIBMFac;
            ?delete?pHPFac;
            ?
            return ? 0 ;
            }


            這個例子比較清楚了,不同的工廠生產不同的計算機,但計算機的基本組成(這里假設計算機僅由ram和cpu組成)是一樣的,這樣的產品系列很適合用抽象工廠來組織。
            而在實際生產計算機的時候,createRam()和createCpu()這兩個工廠方法又起到了作用。

            posted on 2007-03-25 23:20 安帛偉 閱讀(1108) 評論(0)  編輯 收藏 引用 所屬分類: 設計模式
            久久久噜噜噜久久熟女AA片| 亚洲精品高清国产一久久| 手机看片久久高清国产日韩| 久久久人妻精品无码一区| 伊人精品久久久久7777| 久久综合给久久狠狠97色| 久久91精品国产91久久小草| 九九热久久免费视频| 亚洲午夜久久久影院| 欧美精品一本久久男人的天堂| 久久黄视频| 久久精品aⅴ无码中文字字幕不卡| 青青国产成人久久91网| 2021国内精品久久久久久影院| 7777久久亚洲中文字幕| 一级做a爰片久久毛片看看| 99久久免费国产特黄| 亚洲人成无码www久久久| 久久97精品久久久久久久不卡| 97视频久久久| 久久亚洲国产成人影院网站| 久久精品人成免费| 久久久久亚洲AV成人网人人网站| 99久久精品国产一区二区三区| 亚洲综合伊人久久综合| 人妻无码精品久久亚瑟影视| 97精品伊人久久大香线蕉app| 99久久无色码中文字幕人妻| 久久精品成人欧美大片| 久久亚洲国产欧洲精品一| 欧美精品久久久久久久自慰| 中文字幕亚洲综合久久菠萝蜜| 狠狠久久综合伊人不卡| 久久精品国产91久久麻豆自制 | 无码人妻久久一区二区三区| 色综合久久天天综合| 中文字幕无码久久精品青草 | 亚洲日韩中文无码久久| 国产精品成人久久久| 日产精品久久久久久久| 区久久AAA片69亚洲|