91aaa在线国内观看,亚洲AV午夜福利精品一区二区,久久偷拍人视频,久久播这里有免费视播

<strong id="fvuar"></strong>

  • <sub id="fvuar"><dl id="fvuar"><em id="fvuar"></em></dl></sub>

    1. 千鋒教育-做有情懷、有良心、有品質(zhì)的職業(yè)教育機構(gòu)

      手機站
      千鋒教育

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

      千鋒教育

      掃一掃進入千鋒手機站

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

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

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

      unity責(zé)任模式

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

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

      unity責(zé)任模式

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

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

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

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

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

      //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}個碎片不夠,只能合成更低一級的道具,即{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

          {

              /// 當(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}個碎片不夠,只能合成更低一級的道具,即{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í)行一遍,當(dāng)責(zé)任鏈過長時,可能會引起性能的問題;可能導(dǎo)致某個請求不被處理。

        總結(jié)

        代碼中存在多個if-else語句的情況下,此時可以考慮使用責(zé)任鏈模式來對代碼進行重構(gòu)。更多關(guān)于“unity游戲開發(fā)培訓(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