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

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

            手機(jī)站
            千鋒教育

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

            千鋒教育

            掃一掃進(jìn)入千鋒手機(jī)站

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

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

            當(dāng)前位置:首頁  >  技術(shù)干貨  > unity責(zé)任模式

            unity責(zé)任模式

            來源:千鋒教育
            發(fā)布人:wjy
            時(shí)間: 2022-07-29 16:16:00 1659082560

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

            unity責(zé)任模式

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

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

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

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

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

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

                public class SyntheticRequest

                {

                    /// 當(dāng)前擁有的碎片數(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; }

                    //當(dāng)前道具類型

                    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}個(gè)碎片不夠,只能合成更低一級的道具,即{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}個(gè)碎片不夠,只能合成更低一級的道具,即{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}個(gè)碎片不夠,只能合成更低一級的道具,即{1}", request.DebrisNum, NextProp.PropType);

                            NextProp.Behaviour(request);

                        }

                    }

                }

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

            class Program

                {

                    static void Main(string[] args)

                    {

                        //申請合成道具

                        SyntheticRequest request= new SyntheticRequest(66);

                        

                        //對該活動(dòng)的審批可能涉及的角色

                        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

                {

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

                    public int DebrisNum{ get; set; }

                    

                    public SyntheticRequest(int num) 

                    {

                        this.DebrisNum= num;

                    }

                }

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

                public abstract class Prop

                {

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

                    public Prop NextProp{ get; set; }

                    //當(dāng)前道具類型

                    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}個(gè)碎片不夠,只能合成更低一級的道具,即{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}個(gè)碎片不夠,只能合成更低一級的道具,即{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}個(gè)碎片不夠,只能合成更低一級的道具,即{1}", request.DebrisNum, NextProp.PropType);

                            NextProp.Behaviour(request);

                        }

                    }

                }

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

            class Program

                {

                    static void Main(string[] args)

                    {

                        //申請合成道具

                        SyntheticRequest request= new SyntheticRequest(66);

                        

                        //對該活動(dòng)的審批可能涉及的角色

                        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)缺點(diǎn)

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

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

              缺點(diǎn):

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

              總結(jié)

              代碼中存在多個(gè)if-else語句的情況下,此時(shí)可以考慮使用責(zé)任鏈模式來對代碼進(jìn)行重構(gòu)。更多關(guān)于“unity游戲開發(fā)培訓(xùn)”的問題,歡迎咨詢千鋒教育在線名師。千鋒教育多年辦學(xué),課程大綱緊跟企業(yè)需求,更科學(xué)更嚴(yán)謹(jǐn),每年培養(yǎng)泛IT人才近2萬人。不論你是零基礎(chǔ)還是想提升,都可以找到適合的班型,千鋒教育隨時(shí)歡迎你來試聽。

            tags:
            聲明:本站稿件版權(quán)均屬千鋒教育所有,未經(jīng)許可不得擅自轉(zhuǎn)載。
            10年以上業(yè)內(nèi)強(qiáng)師集結(jié),手把手帶你蛻變精英
            請您保持通訊暢通,專屬學(xué)習(xí)老師24小時(shí)內(nèi)將與您1V1溝通
            免費(fèi)領(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
            服務(wù)器為什么要用Linux?

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

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

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

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

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

            2023-10-14 12:12:36
            IT通常說的平臺(tái)是什么意思?

            在信息技術(shù)(IT)領(lǐng)域,”平臺(tái)”這個(gè)詞有著廣泛的含義,它常常指代支持軟件應(yīng)用開發(fā)和運(yùn)行的基礎(chǔ)框架和環(huán)境。以下是對”平臺(tái)”這個(gè)概念的更深入...詳情>>

            2023-10-14 11:55:36
            什么是PowerPivot?

            什么是PowerPivotPowerPivot,全稱”PowerPivot for Excel”,是Microsoft提供的一種數(shù)據(jù)分析工具,可以作為Excel的插件使用。通過PowerPivot,...詳情>>

            2023-10-14 11:25:48