国产睡熟迷奷白丝护士系列精品,中文色字幕网站,免费h网站在线观看的,亚洲开心激情在线

      <sup id="hb9fh"></sup>
          1. 千鋒教育-做有情懷、有良心、有品質(zhì)的職業(yè)教育機構

            手機站
            千鋒教育

            千鋒學習站 | 隨時隨地免費學

            千鋒教育

            掃一掃進入千鋒手機站

            領取全套視頻
            千鋒教育

            關注千鋒學習站小程序
            隨時隨地免費學習課程

            當前位置:首頁  >  技術干貨  > unity游戲開發(fā):責任模式

            unity游戲開發(fā):責任模式

            來源:千鋒教育
            發(fā)布人:wjy
            時間: 2022-07-19 17:37:06 1658223426

              在開發(fā)游戲過程中,當玩家合成一種道具的時候,對于不痛的道具,需要的碎片個數(shù),類型是不同的。用傳統(tǒng)的寫法,就是使用if...else...語句來判斷。如果后面,策劃修改了道具合成機制,我們就需要更改if結構判斷了,這就違背了設計模式原則中的對擴展的開發(fā),對修改的關閉,為此,我們引入責任鏈模式。

              責任鏈模式(Chain of Responsibility Pattern)為請求創(chuàng)建了一個接收者對象的鏈。通常每個接收者都包含對另一個接收者的引用。如果一個對象不能處理該請求,那么它會把相同的請求傳給下一個接收者,依此類推。

            unity游戲開發(fā):責任模式

              1.抽象處理者(Handler):定義出一個處理請求的接口。如果需要,接口可以定義 出一個方法以設定和返回對下家的引用。

              2.具體處理者(ConcreteHandler):具體處理者接到請求后,可以選擇將請求處理掉,或者將請求傳給下家。由于具體處理者持有對下家的引用,因此,如果需要,具體處理者可以訪問下家。

              3.請求類(Request):處理者需要處理的請求信息;

              這里我們還是用上面的例子,使用責任鏈模式來實現(xiàn)獎品的分發(fā)機制。

            //1.請求類,請求合成道具

                public class SyntheticRequest

                {

                    /// 當前擁有的碎片數(shù)量

                    public int DebrisNum{ get; set; }

                    

                    public SyntheticRequest(int num) 

                    {

                        this.DebrisNum= num;

                    }

                }

            //2.創(chuàng)建抽象角色類,可以通過合成得到的道具

                public abstract class Prop

                {

                 //下一級道具,更低一級的道具

                    public Prop NextProp{ get; set; }

                    //當前道具類型

                    public string PropType{ get; set; }

                    //構造函數(shù)

                    public Prop(string type)

                    { this.PropType= type; }

             

                    /// 該角色的執(zhí)行行為

                    public abstract void Behaviour(SyntheticRequest request);

                }

                //3.創(chuàng)建具體角色類

                public class Prop1:Prop

                {

                    public Prop1(string type) : base(type) { }

             

                    public override void Behaviour(SyntheticRequest request)

                    {

                        if (request.DebrisNum>= 1000)

                        {

                            Console.WriteLine("獲得{0},消耗{1}碎片", this.PropType,request.DebrisNum);

                        }

                        else if (NextProp != null)

                        {

                            Console.WriteLine("{0}個碎片不夠,只能合成更低一級的道具,即{1}", request.DebrisNum, NextProp.PropType);

                            NextProp.Behaviour(request);

                        }

                    }

                }

            ///中級道具

                public class Prop2:Prop

                {

                    public Prop2(string type) : base(type) { }

             

                    public override void Behaviour(SyntheticRequest request)

                    {

                        if (request.DebrisNum>= 500)

                        {

                            Console.WriteLine("獲得{0},消耗{1}碎片", this.PropType,request.DebrisNum);

                        }

                        else if (NextProp != null)

                        {

                            Console.WriteLine("{0}個碎片不夠,只能合成更低一級的道具,即{1}", request.DebrisNum, NextProp.PropType);

                            NextProp.Behaviour(request);

                        }

                    }

                }

            ///低級道具

                public class Prop3:Prop

                {

                    public Prop3(string type) : base(type) { }

             

                    public override void Behaviour(SyntheticRequest request)

                    {

                        if (request.DebrisNum>= 10)

                        {

                            Console.WriteLine("獲得{0},消耗{1}碎片", this.PropType,request.DebrisNum);

                        }

                        else if (NextProp != null)

                        {

                            Console.WriteLine("{0}個碎片不夠,只能合成更低一級的道具,即{1}", request.DebrisNum, NextProp.PropType);

                            NextProp.Behaviour(request);

                        }

                    }

                }

            //使用責任鏈模式

            class Program

                {

                    static void Main(string[] args)

                    {

                        //申請合成道具

                        SyntheticRequest request= new SyntheticRequest(66);

                        

                        //對該活動的審批可能涉及的角色

                        Prop prop1= new Prop1("高級道具");

                        Prop prop2= new Prop2("中級道具");

                        Prop prop3= new Prop3("低級道具");

             

                        //設置責任鏈

                        prop1.NextProp = prop2;

                        prop2.NextProp = prop3;

             

                        //合成處理

                        prop1.Behaviour(request);

                    }

                }

            整合代碼

            using System;

            using System.Collections.Generic;

            using System.Linq;

            using System.Text;

            using System.Threading.Tasks;

             

            namespace 責任鏈模式

            {

            //請求類,請求合成道具

                public class SyntheticRequest

                {

                    /// 當前擁有的碎片數(shù)量

                    public int DebrisNum{ get; set; }

                    

                    public SyntheticRequest(int num) 

                    {

                        this.DebrisNum= num;

                    }

                }

             

            //抽象角色類,可以通過合成得到的道具

                public abstract class Prop

                {

                 //下一級道具,更低一級的道具

                    public Prop NextProp{ get; set; }

                    //當前道具類型

                    public string PropType{ get; set; }

                    //構造函數(shù)

                    public Prop(string type)

                    { this.PropType= type; }

             

                    /// 該角色的執(zhí)行行為

                    public abstract void Behaviour(SyntheticRequest request);

                }

                 

                ///高級道具

                public class Prop1:Prop

                {

                    public Prop1(string type) : base(type) { }

             

                    public override void Behaviour(SyntheticRequest request)

                    {

                        if (request.DebrisNum>= 1000)

                        {

                            Console.WriteLine("獲得{0},消耗{1}碎片", this.PropType,request.DebrisNum);

                        }

                        else if (NextProp != null)

                        {

                            Console.WriteLine("{0}個碎片不夠,只能合成更低一級的道具,即{1}", request.DebrisNum, NextProp.PropType);

                            NextProp.Behaviour(request);

                        }

                    }

                }

            ///中級道具

                public class Prop2:Prop

                {

                    public Prop2(string type) : base(type) { }

             

                    public override void Behaviour(SyntheticRequest request)

                    {

                        if (request.DebrisNum>= 500)

                        {

                            Console.WriteLine("獲得{0},消耗{1}碎片", this.PropType,request.DebrisNum);

                        }

                        else if (NextProp != null)

                        {

                            Console.WriteLine("{0}個碎片不夠,只能合成更低一級的道具,即{1}", request.DebrisNum, NextProp.PropType);

                            NextProp.Behaviour(request);

                        }

                    }

                }

            ///低級道具

                public class Prop3:Prop

                {

                    public Prop3(string type) : base(type) { }

             

                    public override void Behaviour(SyntheticRequest request)

                    {

                        if (request.DebrisNum>= 10)

                        {

                            Console.WriteLine("獲得{0},消耗{1}碎片", this.PropType,request.DebrisNum);

                        }

                        else if (NextProp != null)

                        {

                            Console.WriteLine("{0}個碎片不夠,只能合成更低一級的道具,即{1}", request.DebrisNum, NextProp.PropType);

                            NextProp.Behaviour(request);

                        }

                    }

                }

             

            //使用責任鏈模式

            class Program

                {

                    static void Main(string[] args)

                    {

                        //申請合成道具

                        SyntheticRequest request= new SyntheticRequest(66);

                        

                        //對該活動的審批可能涉及的角色

                        Prop prop1= new Prop1("高級道具");

                        Prop prop2= new Prop2("中級道具");

                        Prop prop3= new Prop3("低級道具");

             

                        //設置責任鏈

                        prop1.NextProp = prop2;

                        prop2.NextProp = prop3;

             

                        //合成處理

                        prop1.Behaviour(request);

                    }

                }

            }

              優(yōu)缺點

              優(yōu)點:

              降低了請求的發(fā)送者和接收者之間的耦合;把多個條件判定分散到各個處理類中,使得代碼更加清晰,責任更加明確。

              缺點:

              在找到正確的處理對象之前,所有的條件判定都要執(zhí)行一遍,當責任鏈過長時,可能會引起性能的問題;可能導致某個請求不被處理。

              總結

              代碼中存在多個if-else語句的情況下,此時可以考慮使用責任鏈模式來對代碼進行重構。

              更多關于“unity培訓”的問題,歡迎咨詢千鋒教育在線名師。千鋒教育多年辦學,課程大綱緊跟企業(yè)需求,更科學更嚴謹,每年培養(yǎng)泛IT人才近2萬人。不論你是零基礎還是想提升,都可以找到適合的班型,千鋒教育隨時歡迎你來試聽。

            tags:
            聲明:本站稿件版權均屬千鋒教育所有,未經(jīng)許可不得擅自轉(zhuǎn)載。
            10年以上業(yè)內(nèi)強師集結,手把手帶你蛻變精英
            請您保持通訊暢通,專屬學習老師24小時內(nèi)將與您1V1溝通
            免費領取
            今日已有369人領取成功
            劉同學 138****2860 剛剛成功領取
            王同學 131****2015 剛剛成功領取
            張同學 133****4652 剛剛成功領取
            李同學 135****8607 剛剛成功領取
            楊同學 132****5667 剛剛成功領取
            岳同學 134****6652 剛剛成功領取
            梁同學 157****2950 剛剛成功領取
            劉同學 189****1015 剛剛成功領取
            張同學 155****4678 剛剛成功領取
            鄒同學 139****2907 剛剛成功領取
            董同學 138****2867 剛剛成功領取
            周同學 136****3602 剛剛成功領取
            相關推薦HOT