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

            C++ Programmer's Cookbook

            {C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

            模式設計c#--創建型--AbstractFactory

            名稱 Abstract Factory
            結構 o_Abstractfactory.bmp
            意圖 提供一個創建一系列相關或相互依賴對象的接口,而無需指定它們具體的類。
            適用性
            • 一個系統要獨立于它的產品的創建、組合和表示時。
            • 一個系統要由多個產品系列中的一個來配置時。
            • 當你要強調一系列相關的產品對象的設計以便進行聯合使用時。
            • 當你提供一個產品類庫,而只想顯示它們的接口而不是實現時。
            ??????
            Code Example
            ???????
            namespace?AbstractFactory_DesignPattern
            {
            ????
            using?System;

            ????
            //?These?classes?could?be?part?of?a?framework,
            ????
            //?which?we?will?call?DP
            ????
            //?===========================================
            ????
            ????
            abstract?class?DPDocument?
            ????
            {
            ????????
            abstract?public?void?Dump();????????
            ????}


            ????
            abstract?class?DPWorkspace
            ????
            {
            ????????
            abstract?public?void?Dump();
            ????}

            ????
            ????
            abstract?class?DPView?
            ????
            {
            ????????
            abstract?public?void?Dump();
            ????}
            ????
            ????
            ????
            abstract?class?DPFactory?
            ????
            {
            ????????
            abstract?public?DPDocument?CreateDocument();
            ????????
            abstract?public?DPView?CreateView();
            ????????
            abstract?public?DPWorkspace?CreateWorkspace();
            ????}


            ????
            abstract?class?DPApplication?
            ????
            {
            ????????
            protected?DPDocument?doc;
            ????????
            protected?DPWorkspace?workspace;
            ????????
            protected?DPView?view;
            ????????
            ????????
            public?void?ConstructObjects(DPFactory?factory)
            ????????
            {
            ????????????
            //?Create?objects?as?needed
            ????????????doc?=?factory.CreateDocument();
            ????????????workspace?
            =?factory.CreateWorkspace();
            ????????????view?
            =?factory.CreateView();
            ????????}
            ????????
            ????????
            ????????
            abstract?public?void?Dump();

            ????????
            public?void?DumpState()
            ????????
            {
            ????????????
            if?(doc?!=?null)?doc.Dump();
            ????????????
            if?(workspace?!=?null)?workspace.Dump();
            ????????????
            if?(view?!=?null)?view.Dump();
            ????????}

            ????}


            ????
            //?These?classes?could?be?part?of?an?application?
            ????class?MyApplication?:?DPApplication?
            ????
            {
            ????????MyFactory?myFactory?
            =?new?MyFactory();

            ????????
            override?public?void?Dump()
            ????????
            {
            ????????????Console.WriteLine(
            "MyApplication?exists");
            ????????}


            ????????
            public?void?CreateFamily()
            ????????
            {
            ????????????MyFactory?myFactory?
            =?new?MyFactory();
            ????????????ConstructObjects(myFactory);????????????
            ????????}

            ????}
            ????

            ????
            class?MyDocument?:?DPDocument?
            ????
            {
            ????????
            public?MyDocument()
            ????????
            {
            ????????????????Console.WriteLine(
            "in?MyDocument?constructor");????????????
            ????????}

            ????????
            ????????
            override?public?void?Dump()
            ????????
            {
            ????????????Console.WriteLine(
            "MyDocument?exists");
            ????????}

            ????}


            ????
            class?MyWorkspace?:?DPWorkspace?
            ????
            {
            ????????
            override?public?void?Dump()
            ????????
            {
            ????????????Console.WriteLine(
            "MyWorkspace?exists");
            ????????}

            ????}


            ????
            class?MyView?:?DPView?
            ????
            {
            ????????
            override?public?void?Dump()
            ????????
            {
            ????????????Console.WriteLine(
            "MyView?exists");
            ????????}

            ????}


            ????
            class?MyFactory?:?DPFactory?
            ????
            {
            ????????
            override?public?DPDocument?CreateDocument()
            ????????
            {
            ????????????
            return?new?MyDocument();
            ????????}

            ????????
            override?public?DPWorkspace?CreateWorkspace()
            ????????
            {
            ????????????
            return?new?MyWorkspace();
            ????????}
            ????????
            ????????
            override?public?DPView?CreateView()
            ????????
            {
            ????????????
            return?new?MyView();
            ????????}

            ????}


            ????
            ///?<summary>
            ????
            ///????Summary?description?for?Client.
            ????
            ///?</summary>

            ????public?class?Client
            ????
            {
            ????????
            public?static?int?Main(string[]?args)
            ????????
            {
            ????????????MyApplication?myApplication?
            =?new?MyApplication();

            ????????????myApplication.CreateFamily();

            ????????????myApplication.DumpState();
            ????????????
            ????????????
            return?0;
            ????????}

            ????}

            }


            //?Abstract?Factory?pattern?--?Structural?example??


            using?System;

            namespace?DoFactory.GangOfFour.Abstract.Structural
            {
            ??
            //?MainApp?test?application?

            ??
            class?MainApp
            ??
            {
            ????
            public?static?void?Main()
            ????
            {
            ??????
            //?Abstract?factory?#1?
            ??????AbstractFactory?factory1?=?new?ConcreteFactory1();
            ??????Client?c1?
            =?new?Client(factory1);
            ??????c1.Run();

            ??????
            //?Abstract?factory?#2?
            ??????AbstractFactory?factory2?=?new?ConcreteFactory2();
            ??????Client?c2?
            =?new?Client(factory2);
            ??????c2.Run();

            ??????
            //?Wait?for?user?input?
            ??????Console.Read();
            ????}

            ??}


            ??
            //?"AbstractFactory"?

            ??
            abstract?class?AbstractFactory
            ??
            {
            ????
            public?abstract?AbstractProductA?CreateProductA();
            ????
            public?abstract?AbstractProductB?CreateProductB();
            ??}


            ??
            //?"ConcreteFactory1"?

            ??
            class?ConcreteFactory1?:?AbstractFactory
            ??
            {
            ????
            public?override?AbstractProductA?CreateProductA()
            ????
            {
            ??????
            return?new?ProductA1();
            ????}

            ????
            public?override?AbstractProductB?CreateProductB()
            ????
            {
            ??????
            return?new?ProductB1();
            ????}

            ??}


            ??
            //?"ConcreteFactory2"?

            ??
            class?ConcreteFactory2?:?AbstractFactory
            ??
            {
            ????
            public?override?AbstractProductA?CreateProductA()
            ????
            {
            ??????
            return?new?ProductA2();
            ????}

            ????
            public?override?AbstractProductB?CreateProductB()
            ????
            {
            ??????
            return?new?ProductB2();
            ????}

            ??}


            ??
            //?"AbstractProductA"?

            ??
            abstract?class?AbstractProductA
            ??
            {
            ??}


            ??
            //?"AbstractProductB"?

            ??
            abstract?class?AbstractProductB
            ??
            {
            ????
            public?abstract?void?Interact(AbstractProductA?a);
            ??}


            ??
            //?"ProductA1"?

            ??
            class?ProductA1?:?AbstractProductA
            ??
            {
            ??}


            ??
            //?"ProductB1"?

            ??
            class?ProductB1?:?AbstractProductB
            ??
            {
            ????
            public?override?void?Interact(AbstractProductA?a)
            ????
            {
            ??????Console.WriteLine(
            this.GetType().Name?+?
            ????????
            "?interacts?with?"?+?a.GetType().Name);
            ????}

            ??}


            ??
            //?"ProductA2"?

            ??
            class?ProductA2?:?AbstractProductA
            ??
            {
            ??}


            ??
            //?"ProductB2"?

            ??
            class?ProductB2?:?AbstractProductB
            ??
            {
            ????
            public?override?void?Interact(AbstractProductA?a)
            ????
            {
            ??????Console.WriteLine(
            this.GetType().Name?+?
            ????????
            "?interacts?with?"?+?a.GetType().Name);
            ????}

            ??}


            ??
            //?"Client"?-?the?interaction?environment?of?the?products?

            ??
            class?Client
            ??
            {
            ????
            private?AbstractProductA?AbstractProductA;
            ????
            private?AbstractProductB?AbstractProductB;

            ????
            //?Constructor?
            ????public?Client(AbstractFactory?factory)
            ????
            {
            ??????AbstractProductB?
            =?factory.CreateProductB();
            ??????AbstractProductA?
            =?factory.CreateProductA();
            ????}


            ????
            public?void?Run()
            ????
            {
            ??????AbstractProductB.Interact(AbstractProductA);
            ????}

            ??}

            }

            ?
            Output
            ProductB1 interacts with ProductA1
            ProductB2
            interacts with ProductA2

            posted on 2006-01-03 14:43 夢在天涯 閱讀(1050) 評論(3)  編輯 收藏 引用 所屬分類: Design pattern

            評論

            # re: 模式設計c#--創建型--AbstractFactory 2006-04-24 10:10 夢在天涯

            definition
            Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
              回復  更多評論   

            # re: 模式設計c#--創建型--AbstractFactory 2006-04-24 10:34 夢在天涯

            提供一個創建一組相關或無關對象的接口,而沒有指定具體的類.  回復  更多評論   

            # re: 模式設計c#--創建型--AbstractFactory 2006-04-24 10:38 夢在天涯

            實際上,有些時候兩者是結合使用的。我見過BuilderFactory。在Java的DOM模型中就使用了BuilderFactory。

            Java 環境中,解析文件是一個三步過程:

            1、創建 DocumentBuilderFactory。 DocumentBuilderFactory 對象創建 DocumentBuilder。
            2、創建 DocumentBuilder。 DocumentBuilder 執行實際的解析以創建 Document 對象。
            3、解析文件以創建 Document 對象。

            關鍵代碼如下:

            import javax.xml.parsers.DocumentBuilder;
            import javax.xml.parsers.DocumentBuilderFactory;
            import java.io.File;
            import org.w3c.dom.Document;

            public class OrderProcessor {
            public static void main (String args[]) {
            File docFile = new File("orders.xml");
            Document doc = null;
            try {

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.parse(docFile);

            } catch (Exception e) {
            System.out.print("Problem parsing the file: "+e.getMessage());
            }
            }
            }

            很有創意的!   回復  更多評論   

            公告

            EMail:itech001#126.com

            導航

            統計

            • 隨筆 - 461
            • 文章 - 4
            • 評論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804434
            • 排名 - 5

            最新評論

            閱讀排行榜

            久久婷婷午色综合夜啪| 亚洲va久久久噜噜噜久久| www.久久热.com| 国产成人精品免费久久久久| 久久99久久99精品免视看动漫| 久久久久久综合网天天| 99久久精品国产一区二区| 99久久精品国产一区二区 | 91精品国产高清久久久久久91| 国产精品毛片久久久久久久| 久久成人影院精品777| 青青青青久久精品国产| 精品无码久久久久久久动漫| 亚洲乱码日产精品a级毛片久久| yy6080久久| 国产成人精品白浆久久69| 国内精品久久久久久久久电影网 | 91久久精品91久久性色| 精品综合久久久久久97超人| 国产女人aaa级久久久级| 亚洲国产成人久久综合一区77| 久久综合久久美利坚合众国| 久久一日本道色综合久久| 女人香蕉久久**毛片精品| 热久久国产欧美一区二区精品 | 一级做a爱片久久毛片| 欧美久久综合九色综合| 日韩久久久久久中文人妻| 91精品久久久久久无码| 99精品国产99久久久久久97| 久久婷婷久久一区二区三区| 久久婷婷午色综合夜啪| 久久久久久久亚洲Av无码| 久久国产V一级毛多内射| 日本强好片久久久久久AAA| 91麻豆精品国产91久久久久久| 久久久国产精华液| 夜夜亚洲天天久久| 99久久久精品免费观看国产| 久久精品国产亚洲av麻豆蜜芽| 亚洲综合精品香蕉久久网97|