久久精品国产亚洲高清|精品日韩中文乱码在线|亚洲va中文字幕无码久|伊人久久综合狼伊人久久|亚洲不卡av不卡一区二区|精品久久久久久久蜜臀AV|国产精品19久久久久久不卡|国产男女猛烈视频在线观看麻豆

    1. <style id="76ofp"></style>

      <style id="76ofp"></style>
      <rt id="76ofp"></rt>
      <form id="76ofp"><optgroup id="76ofp"></optgroup></form>
      1. 千鋒教育-做有情懷、有良心、有品質(zhì)的職業(yè)教育機構(gòu)

        手機站
        千鋒教育

        千鋒學(xué)習(xí)站 | 隨時隨地免費學(xué)

        千鋒教育

        掃一掃進入千鋒手機站

        領(lǐng)取全套視頻
        千鋒教育

        關(guān)注千鋒學(xué)習(xí)站小程序
        隨時隨地免費學(xué)習(xí)課程

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

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

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

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

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

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

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

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

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

          這里我們還是用上面的例子,使用責(zé)任鏈模式來實現(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; }

                //構(gòu)造函數(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);

                    }

                }

            }

        //使用責(zé)任鏈模式

        class Program

            {

                static void Main(string[] args)

                {

                    //申請合成道具

                    SyntheticRequest request= new SyntheticRequest(66);

                    

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

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

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

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

         

                    //設(shè)置責(zé)任鏈

                    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 責(zé)任鏈模式

        {

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

            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; }

                //構(gòu)造函數(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);

                    }

                }

            }

         

        //使用責(zé)任鏈模式

        class Program

            {

                static void Main(string[] args)

                {

                    //申請合成道具

                    SyntheticRequest request= new SyntheticRequest(66);

                    

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

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

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

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

         

                    //設(shè)置責(zé)任鏈

                    prop1.NextProp = prop2;

                    prop2.NextProp = prop3;

         

                    //合成處理

                    prop1.Behaviour(request);

                }

            }

        }

          優(yōu)缺點

          優(yōu)點:

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

          缺點:

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

          總結(jié)

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

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

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

        PlatformIO是什么PlatformIO是一個全面的物聯(lián)網(wǎng)開發(fā)平臺,它為眾多硬件平臺和開發(fā)環(huán)境提供了統(tǒng)一的工作流程,有效簡化了開發(fā)過程,并能兼容各種...詳情>>

        2023-10-14 12:55:06
        云快照與自動備份有什么區(qū)別?

        1、定義和目標不同云快照的主要目標是提供一種快速恢復(fù)數(shù)據(jù)的方法,它只記錄在快照時間點后的數(shù)據(jù)變化,而不是所有的數(shù)據(jù)。自動備份的主要目標...詳情>>

        2023-10-14 12:48:59
        服務(wù)器為什么要用Linux?

        服務(wù)器為什么要用Linux作為服務(wù)器操作系統(tǒng)的優(yōu)選,Linux在眾多選擇中脫穎而出。Linux作為服務(wù)器操作系統(tǒng)的優(yōu)選,有其獨特的優(yōu)勢和特點。包括其...詳情>>

        2023-10-14 12:34:11
        ORM解決的主要問題是什么?

        ORM(對象關(guān)系映射)解決的主要問題是將關(guān)系數(shù)據(jù)庫與面向?qū)ο缶幊讨g的映射困境。在傳統(tǒng)的關(guān)系數(shù)據(jù)庫中,數(shù)據(jù)以表格的形式存儲,而在面向?qū)ο?..詳情>>

        2023-10-14 12:26:19
        Go為什么不支持三元運算符?

        Go為什么不支持三元運算符Go語言是一種以簡潔和有效性為目標的編程語言,因此在設(shè)計過程中,Go的設(shè)計者刻意排除了一些他們認為可能導(dǎo)致復(fù)雜性或...詳情>>

        2023-10-14 12:12:36
        崇州市| 许昌县| 遂昌县| 昌乐县| 香港 | 扬中市| 秦皇岛市| 山阴县| 武安市| 镇江市| 辽宁省| 保靖县| 濮阳市| 富源县| 阳新县| 鄄城县| 建水县| 大港区| 新闻| 盐源县| 汶上县| 房产| 芜湖市| 涞源县| 荆州市| 理塘县| 雅江县| 南澳县| 清流县| 垣曲县| 崇明县| 麦盖提县| 洪雅县| 中卫市| 岫岩| 鄂州市| 南阳市| 宣城市| 铜川市| 平原县| 盐津县|