天天看點

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

說明:我也是初學者,希望大家能提出寶貴意見。另外轉載請注明作者左洸和出處部落格園,畢竟花費了很長時間才完成。

不管是窮人還是富人,都要談戀愛結婚,而且每個人的婚戀經曆,步驟大體上都是一樣的,比如說:見面、吃飯、遊玩、婚禮、婚房   等等,這些步驟是社會已經給我們安排好了的,他就像一個大綱、一個模版,作為社會中的一個成員,不管是窮是富,都隻能按照步驟去做,誰也不能改變這些步驟,但是每個步驟的具體内容暫時還是抽象的,怎麼見面,怎麼吃飯,怎麼遊玩,婚禮怎麼辦,婚房是什麼樣的,一千個人可能有一千個情況,比如你窮,和女朋友吃飯一碗面條也能打發;如果你富有,山珍海味也是吃飯。這就是為什麼大家都戀愛結婚,卻有人歡喜有人憂啊。

按照一個已經列好的大綱,根據自己具體的情況,實作其中的抽象步驟,這就是模版方法模式。

<a href="http://myqiao.cnblogs.com/category/33478.html?Show=All">點選浏覽更多的“說故事、學模式”系列</a>

下面讓我們來看看,用C#語言怎麼描述窮人和富人的不同的婚戀曆程,這是一個典型的模版方法模式。

TemplateMethod.cs

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

using System;

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

using System.Collections.Generic;

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

using System.Text;

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

namespace Template

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

{

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

    //***************************************************************

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

    //用戶端

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

    class Client

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

        static void Main(string[] args)

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

            try

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

                Society rich = new Rich();

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

                Society poor = new Poor();

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

                Society et = new ET();

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

                Console.WriteLine("富人的婚戀過程。。。。。。。。\n");

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

                Marry(rich);

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

                Console.WriteLine();

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

                Console.WriteLine("窮人的婚戀過程。。。。。。。。\n");

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

                Marry(poor);

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

                Console.WriteLine("外星人的婚戀過程。。。。。。。。\n");

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

                Marry(et);

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

            }

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

            catch (Exception e)

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

                Console.WriteLine(e.Message);

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

                Console.Read();

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

        }

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

        static public void Marry(Society society)

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

            society.Marry();

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

    }

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

    //******************************************************************

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

    //社會公認的婚戀過程,他是抽象的,隻有步驟沒有具體内容

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

    public abstract class Society

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

        abstract public void Meet();    //第一次見面

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

        abstract public void Dinner();  //吃飯

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

        abstract public void Journey(); //遊玩約會

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

        abstract public void Wedding(); //婚禮

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

        abstract public void House();   //婚房

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

        //每一個人都要經曆的婚戀曆程,需要做哪些事情社會已經替你安排好了

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

        //人在江湖身不由己,不管你是窮人還是富人你都無法改變這個過程

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

        //但是,具體實施的時候我們還是可以掌握着自己的命運

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

        //這裡是戀愛結婚的模版方法

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

        public void Marry()

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

            Console.Write("見面:");

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

            Meet();

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

            Console.Write("吃飯:");

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

            Dinner();

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

            Console.Write("遊玩:");

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

            Journey();

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

            Console.Write("婚禮:");

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

            Wedding();

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

            Console.Write("婚房:");

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

            House();

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

    //我希望自己是一個富人,下面是我設想的富人的婚戀過程

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

    public class Rich : Society

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

        public override void Meet()

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

            Console.WriteLine("用半年的時間調查對方的詳細情況,見面這天簽訂婚姻合同,一個月後舉行婚禮\n");

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

        public override void Dinner()

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

            Console.WriteLine("18.8萬元的天價年夜飯一份,先湊合一下吧!\n");

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

        public override void Journey()

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

            Console.WriteLine("花2000萬美元坐神舟8号到太空去看星星\n");

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

        public override void Wedding()

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

            Console.WriteLine("克林頓作為特邀嘉賓主持,對全世界150個國家進行現場直播\n");

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

        public override void House()

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

            Console.WriteLine("給小布什打個招呼,借用白宮一個月\n");

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

    //雖然我很窮,但是我也要戀愛結婚

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

    public class Poor : Society

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

            Console.WriteLine("見面之前隻有對方的電話号碼,在天橋底下見面,十五分鐘啥也沒打聽出來\n");

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

            Console.WriteLine("兩份肯德雞套餐,花了五六十元,心疼啊!!!\n");

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

            Console.WriteLine("從公園後牆跳進去,裡面的長椅可以免費坐,驚險刺激又節約\n");

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

            Console.WriteLine("隻要認識的人一律發請帖,收了這麼多年的罰款單,得連本代利賺回來!!\n");

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

            Console.WriteLine("機關宿舍20來平米,暫時住住吧!媳婦,看看睡馬路的那哥們,不錯了!!!\n");

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

    //外星人怎麼戀愛結婚呢?讓你來當一回編劇吧。。。。

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

    public class ET : Society

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

        string msg = "劇本還沒有寫好,請自行解決!";

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

            throw new Exception(msg);

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程
模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

}

模版方法模式 Template Method Pattern — 窮人和富人的不同婚戀曆程

執行結果如下圖:

本文轉自左洸部落格園部落格,原文連結:http://www.cnblogs.com/myqiao/archive/2006/03/26/359478.html,如需轉載請自行聯系原作者

繼續閱讀