日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > asp.net >内容正文

asp.net

小话设计模式(四)生成器模式

發(fā)布時(shí)間:2023/12/31 asp.net 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 小话设计模式(四)生成器模式 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

生成器或者建造者(Builder)模式是將一個(gè)復(fù)雜對(duì)象的構(gòu)建與它的表示分離,使得同樣的構(gòu)建過(guò)程可以創(chuàng)建不同的表示。

舉個(gè)例子,假設(shè)我們正在制作一款機(jī)甲類的游戲,每個(gè)機(jī)器人由六個(gè)部分組成:頭部、身體、左手、右手、左腿和右腿,分為頭、身、手和腿四種類型。定義如下:

public interface IHead { }public interface IBody { }public interface IHand { }public interface ILeg { }public class Robot {public IBody Body { get; set;}public IHead Head { get; set;}public IHand LeftHand { get; set;}public IHand RightHand { get; set;}public ILeg LeftLeg { get; set;}public ILeg RightLeg { get; set;}public override string ToString (){return base.ToString ()+ Body.ToString ()+ Head.ToString ()+ LeftHand.ToString ()+ RightHand.ToString ()+ LeftLeg.ToString ()+ RightLeg.ToString ();} }
我們有不同的(繼承自頭、身、手、腿接口)部件可以組裝機(jī)器人,例如:

public class RobotHead : IHead { }public class RobotBody : IBody { }public class RobotHand : IHand { }public class RobotLeg : ILeg { }public class CyclopsHead : IHead { }public class SolarBody : IBody { }public class RazorHand : IHand { }public class LaserHand : IHand { }public class WheelLeg : ILeg { }public class LauncherLeg: ILeg { }
例如我需要一個(gè)亞當(dāng)機(jī)器人,所有的部件都是最基本的,那么如何組合起來(lái)呢?(我來(lái)組成頭部)

來(lái)看看生成器模式,首先創(chuàng)建一個(gè)生成器接口:

public interface IRobotBuilder{Robot robot {get;}void CreateHead();void CreateBody();void CreateLeftHand();void CreateRightHand();void CreateLeftLeg ();void CreateRightLeg (); }
編寫亞當(dāng)?shù)纳善?#xff1a;

public class AdamBuilder : IRobotBuilder{private Robot _robot = new Robot();public Robot robot {get {return _robot;}}public void CreateHead(){_robot.Head = new RobotHead ();}public void CreateBody(){_robot.Body = new RobotBody ();}public void CreateLeftHand(){_robot.LeftHand = new RobotHand ();}public void CreateRightHand(){_robot.RightHand = new RobotHand ();}public void CreateLeftLeg (){_robot.LeftLeg = new RobotLeg ();}public void CreateRightLeg (){_robot.RightLeg = new RobotLeg ();} }
或者我們需要一個(gè)獨(dú)眼剃刀機(jī)器人:

public class CyclopsRazorBuilder : IRobotBuilder {private Robot _robot = new Robot();public Robot robot {get {return _robot;}}public void CreateHead(){_robot.Head = new CyclopsHead ();}public void CreateBody(){_robot.Body = new RobotBody ();}public void CreateLeftHand(){_robot.LeftHand = new RobotHand ();}public void CreateRightHand(){_robot.RightHand = new RazorHand ();}public void CreateLeftLeg (){_robot.LeftLeg = new RobotLeg ();}public void CreateRightLeg (){_robot.RightLeg = new RobotLeg ();} }
這樣我們就可以調(diào)用這兩個(gè)類來(lái)生成不同的機(jī)器人了。

但是,每次都調(diào)用六個(gè)Create方法未免太麻煩了,而且往往我們希望構(gòu)建過(guò)程對(duì)用戶透明,那么你需要一個(gè)Director(策劃指揮者):

static public class RobotDirector {static public Robot Construct(IRobotBuilder rb){rb.CreateHead ();rb.CreateBody ();rb.CreateLeftHand ();rb.CreateRightHand ();rb.CreateLeftLeg ();rb.CreateRightLeg ();return rb.robot;} }
使用:

Robot adam = RobotDirector.Construct (new AdamBuilder ());

或許你并不希望用戶創(chuàng)建builder,那么可以改寫成這樣:

static public class TRobotDirector {static public Robot Construct<T>() where T: IRobotBuilder, new(){T rb = new T();rb.CreateHead ();rb.CreateBody ();rb.CreateLeftHand ();rb.CreateRightHand ();rb.CreateLeftLeg ();rb.CreateRightLeg ();return rb.robot;} } 使用:

Robot cr = TRobotDirector.Construct<CyclopsRazorBuilder> ();
然而,你或許注意到了,每當(dāng)我們需要一個(gè)新的機(jī)器人的時(shí)候都要新建一個(gè)生成器,當(dāng)不同的部件有千千萬(wàn)(例如噴火的手臂、復(fù)眼頭部和原子能身體)的時(shí)候,又可能會(huì)有萬(wàn)萬(wàn)億的生成器,這樣碼農(nóng)可能會(huì)累死(給你工資是干啥的,趕緊碼起來(lái))。這樣也無(wú)法動(dòng)態(tài)生成機(jī)器人,并且當(dāng)我們需要增加新類型的組件(例如武器)時(shí),這些萬(wàn)萬(wàn)億的生成器都需要修改,這顯然不合理。

那么,就讓我們?nèi)拥羲械纳善靼?#xff0c;使用反射來(lái)解決這些問(wèn)題:

public static class SRobotBuilder {static private object CreateObjectWithName(string classname){Type type = Type.GetType (classname);return Activator.CreateInstance(type);}static private IHead CreateHead(string cn){return (IHead)CreateObjectWithName (cn);}static private IBody CreateBody(string cn){return (IBody)CreateObjectWithName (cn);}static private IHand CreateLeftHand(string cn){return (IHand)CreateObjectWithName (cn);}static private IHand CreateRightHand(string cn){return (IHand)CreateObjectWithName (cn);}static private ILeg CreateLeftLeg (string cn){return (ILeg)CreateObjectWithName (cn);}static private ILeg CreateRightLeg (string cn){return (ILeg)CreateObjectWithName (cn);}public enum RobotKey{Head,Body,LeftHand,RightHand,LeftLeg,RightLeg,}static public Robot Construct(Dictionary<RobotKey, string> robotDict){Robot robot = new Robot ();robot.Head = CreateHead (robotDict [RobotKey.Head]);robot.Body = CreateBody (robotDict [RobotKey.Body]);robot.LeftHand = CreateLeftHand (robotDict [RobotKey.LeftHand]);robot.RightHand = CreateRightHand (robotDict [RobotKey.RightHand]);robot.LeftLeg = CreateLeftLeg (robotDict [RobotKey.LeftLeg]);robot.RightLeg = CreateRightLeg (robotDict [RobotKey.RightLeg]);return robot;} }
使用:

Dictionary<SRobotBuilder.RobotKey,string> robotDict = new Dictionary<SRobotBuilder.RobotKey, string> ();robotDict[SRobotBuilder.RobotKey.Head] = "CyclopsHead";robotDict[SRobotBuilder.RobotKey.Body] = "SolarBody";robotDict[SRobotBuilder.RobotKey.LeftHand] = "RazorHand";robotDict[SRobotBuilder.RobotKey.RightHand] = "LaserHand";robotDict[SRobotBuilder.RobotKey.LeftLeg] = "WheelLeg";robotDict[SRobotBuilder.RobotKey.RightLeg] = "LauncherLeg";Robot nb = SRobotBuilder.Construct (robotDict);
這樣我們就可以使用配置文件來(lái)生成默認(rèn)的機(jī)器人,而且可以通過(guò)運(yùn)行時(shí)生成Dictionary來(lái)動(dòng)態(tài)的創(chuàng)建機(jī)器人(例如玩家自定義的機(jī)器人)。


最后說(shuō)兩句:

生成器模式一般就是用來(lái)構(gòu)建變形金剛用的,如果非要抽象來(lái)講,那么還是變形金剛,就是某個(gè)類型由不同的部分組成,而這些部分可以獨(dú)立變化成不同的子類(例如創(chuàng)建迷宮或者組裝賽車,再或者孢子)。

總結(jié)

以上是生活随笔為你收集整理的小话设计模式(四)生成器模式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。