国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

設(shè)計(jì)模式之十二:適配器模式

2019-11-09 15:14:13
字體:
供稿:網(wǎng)友

      適配器模式(Adapter Pattern)又稱變壓器模式或者包裝模式:

      定義:Convert the interface of a class into another interface clints expect. Adapter lets classes work together that couldn't  otherwise because of incompatible interfaces。(將一個類的接口編程客戶端所期待的另一種接口,從而使原本因接口不匹配而無法在一起工作的兩個類能夠在一起工作。)

      通用類圖:

    

     Target目標(biāo)角色:該角色定義其他類轉(zhuǎn)換成我們期待的類或接口

      Adaptee源角色:你想把誰變成目標(biāo)角色,這個“誰”就是源角色

      Adapter適配器角色:適配器的核心角色,其他兩個角色都是已經(jīng)存在的角色,而適配器角色需要新建立的,它的職責(zé)非常簡單,就是通過繼承或者類關(guān)聯(lián)的方式把源角色轉(zhuǎn)換成目標(biāo)角色

      

      優(yōu)點(diǎn)

  1、通過適配器,客戶端可以調(diào)用同一接口,因而對客戶端來說是透明的。這樣做更簡單、更直接、更緊湊。

  2、 復(fù)用了現(xiàn)存的類,解決了現(xiàn)存類和復(fù)用環(huán)境要求不一致的問題。

  3 、將目標(biāo)類和適配者類解耦,通過引入一個適配器類重用現(xiàn)有的適配者類,而無需修改原有代碼。

  4、 一個對象適配器可以把多個不同的適配者類適配到同一個目標(biāo),也就是說,同一個適配器可以把適配者類和它的子類都適配到目標(biāo)接口。

  缺點(diǎn)

  對于對象適配器來說,更換適配器的實(shí)現(xiàn)過程比較復(fù)雜。

    

 //機(jī)器人類    abstract public class Robot    {        abstract public void RobotCry();        abstract public void RobotMove();        abstract public void SetAdaptee(object Value); //這么申明帶有一個參數(shù)的抽象方法。    }    //抽象的Adaptee    public class AnimalAdaptee    {        public virtual void Cry() { }        public virtual void Move() { }        }    //不同種類的動物    //狗    public class DogAdaptee : AnimalAdaptee    {        public override void Cry()        {            //base.Cry();            Console.WriteLine("這是狗叫:汪汪汪!");        }        public override void Move()        {            //base.Move();            Console.WriteLine("這是狗跳:跳跳跳!");        }    }    //鳥    public class BirdAdaptee : AnimalAdaptee    {        public override void Cry()        {            //base.Cry();            Console.WriteLine("這是鳥叫:嘰嘰嘰!");        }        public override void Move()        {            //base.Move();            Console.WriteLine("這是鳥飛:快快飛!");        }    }    //適配器類Adaptor    public class RobotAdaPPTor : Robot    {        PRivate AnimalAdaptee _myAdaptee;        public AnimalAdaptee MyAdaptee        {            get { return _myAdaptee; }            set { _myAdaptee = value; }        }        public override void SetAdaptee(Object Value)        {            _myAdaptee = (AnimalAdaptee)Value;        }        public override void RobotCry()        {            //throw new NotImplementedException();            if (_myAdaptee == null) _myAdaptee = new AnimalAdaptee();            _myAdaptee.Cry();        }        public override void RobotMove()        {            //throw new NotImplementedException();            if (_myAdaptee == null) _myAdaptee = new AnimalAdaptee();            _myAdaptee.Move();        }    }    class Program    {        static void Main(string[] args)        {            Robot MyRobot = new RobotAdapptor();            AnimalAdaptee dogAdaptee = new DogAdaptee();            MyRobot.SetAdaptee(dogAdaptee);            MyRobot.RobotCry();            MyRobot.RobotMove();            //申明為第二種鳥類            AnimalAdaptee birdAdaptee=new BirdAdaptee();            MyRobot.SetAdaptee(birdAdaptee);            MyRobot.RobotCry();            MyRobot.RobotMove();            Console.ReadKey();        }    }好了,這一章就寫到這,歡迎大家加入QQ群:280993838 。或者關(guān)注我的公眾號:


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 法库县| 林甸县| 乌恰县| 东平县| 安阳县| 巨鹿县| 康马县| 密山市| 泰州市| 南岸区| 五峰| 钦州市| 镇宁| 剑川县| 汉沽区| 曲周县| 磴口县| 朔州市| 琼结县| 鱼台县| 淮滨县| 航空| 南部县| 黄梅县| 永仁县| 天祝| 依兰县| 黔南| 怀来县| 海城市| 秦皇岛市| 德庆县| 宜良县| 靖远县| 固阳县| 于田县| 长兴县| 临泽县| 大关县| 吐鲁番市| 杭锦后旗|