中文字幕精品亚洲无线码二区,国产黄a三级三级三级看三级,亚洲七七久久桃花影院,丰满少妇被猛烈进入,国产小视频在线观看网站

說(shuo)說(shuo)設計模(mo)式~橋梁模(mo)式(Bridge)

返回目錄

在軟件系統中,某些類型由(you)于自身的(de)邏(luo)輯,它具有兩(liang)個或多(duo)個維(wei)(wei)度的(de)變(bian)化,那么(me)如(ru)何應(ying)對這種“多(duo)維(wei)(wei)度的(de)變(bian)化”?如(ru)何利(li)用面向對象的(de)技(ji)術來使(shi)得該類型能夠輕(qing)松的(de)沿著多(duo)個方向進行變(bian)化,而又不引入(ru)額(e)外的(de)復雜度?這就要使(shi)用Bridge模式。

意圖

【GOF95】在提出(chu)橋(qiao)梁模式的(de)時候指出(chu),橋(qiao)梁模式的(de)用意(yi)是"將(jiang)抽象化(hua)(Abstraction)與(yu)實現化(hua)(Implementation)脫(tuo)耦(ou),使得(de)二者可(ke)以獨立(li)地變化(hua)"。這句話有三個(ge)關鍵(jian)詞,也就是抽象化(hua)、實現化(hua)和脫(tuo)耦(ou)。

橋梁模式的成員

抽象化

存在于多個實體中的共同的概念性聯系,就是抽象化。作為一個過程,抽象化就是忽略一些信息,從而把不同的實體當做同樣的實體對待。

實(shi)現化

抽象化給出的具體實現,就是實現化,這里的實現化不是具體的實現,而是一個接口或者抽象類,它是對抽象化的擴展。

脫耦

所謂耦合,就是兩個實體的行為的某種強關聯。而將它們的強關聯去掉,就是耦合的解脫,或稱脫耦。在這里,脫耦是指將抽象化和實現化之間的耦合解脫開,或者說是將它們之間的強關聯改換成弱關聯。
將兩個角色之間的繼承關系改為聚合關系,就是將它們之間的強關聯改換成為弱關聯。因此,橋梁模式中的所謂脫耦,就是指在一個軟件系統的抽象化和實現化之間使用組合/聚合關系而不是繼承關系,從而使兩者可以相對獨立地變化。這就是橋梁模式的用意。

何時能用到它?

某些類型由于自身(shen)的邏(luo)輯,它(ta)具有兩(liang)個(ge)(ge)或(huo)多個(ge)(ge)維(wei)度的變化,這時使用橋梁模(mo)式(shi)

橋梁模式的結構圖

橋梁模式實現說明

Abstraction:抽(chou)象者,有對實現(xian)者的引用

RefinedAbstraction:更新(xin)抽象(xiang)者,對抽象(xiang)者進行擴展(zhan),它可以添加或(huo)者修(xiu)改抽象(xiang)者的部分(fen)功(gong)能

Implementor:實(shi)現者,它是一(yi)個(ge)接口或者功能類(lei),它是對實(shi)現進行(xing)的一(yi)個(ge)抽象(xiang)

ConcreteImplementorA:具體實現者,是實現Implementor的(de)一種方式

橋梁模式的C#實現

#region bridge pattern
    #region 抽象者
    // "Abstraction"
    class Abstraction
    {
        // Fields
        protected Implementor implementor;
        // Properties
        public Implementor Implementor
        {
            set { implementor = value; }
        }
        // Methods
        virtual public void Operation()
        {
            implementor.Operation();
        }
    }

    // "RefinedAbstraction"
    class RefinedAbstraction : Abstraction
    {
        // Methods
        override public void Operation()
        {
            implementor.Operation();
        }
    }
    #endregion

    #region 實現者
    // "Implementor"
    abstract class Implementor
    {
        // Methods
        abstract public void Operation();
    }
    // "ConcreteImplementorA"
    class ConcreteImplementorA : Implementor
    {
        // Methods
        override public void Operation()
        {
            Console.WriteLine("ConcreteImplementorA Operation");
        }
    }
    // "ConcreteImplementorB"
    class ConcreteImplementorB : Implementor
    {
        // Methods
        override public void Operation()
        {
            Console.WriteLine("ConcreteImplementorB Operation");
        }
    }
    #endregion
    #endregion

調用代碼

            Abstraction abstraction = new RefinedAbstraction();
            // Set implementation and call
            abstraction.Implementor = new ConcreteImplementorA();
            abstraction.Operation();
            // Change implemention and call
            abstraction.Implementor = new ConcreteImplementorB();
            abstraction.Operation();

結果截圖

返回目錄

posted @ 2014-09-05 16:10  張占嶺  閱讀(1547)  評論(0)    收藏  舉報