1. 程式人生 > >設計模式(C#)——12責任鏈模式

設計模式(C#)——12責任鏈模式

推薦閱讀:

前言

       在開發遊戲過程中,當玩家合成一種道具的時候,對於不痛的道具,需要的碎片個數,型別是不同的。用傳統的寫法,就是使用if…else…語句來判斷。如果後面,策劃修改了道具合成機制,我們就需要更改if結構判斷了,這就違背了設計模式原則中的對擴充套件的開發,對修改的關閉,為此,我們引入責任鏈模式。

介紹

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

要素

       1.抽象處理者(Handler):定義出一個處理請求的介面。如果需要,介面可以定義 出一個方法以設定和返回對下家的引用。
       2.具體處理者(ConcreteHandler):具體處理者接到請求後,可以選擇將請求處理掉,或者將請求傳給下家。由於具體處理者持有對下家的引用,因此,如果需要,具體處理者可以訪問下家。
       3.請求類(Request):處理者需要處理的請求資訊;

實現

       這裡我們還是用上面的例子,使用責任鏈模式來實現獎品的分發機制。
1.建立請求類

	//請求類,請求合成道具
    public class SyntheticRequest
    {
        /// 當前擁有的碎片數量
        public int DebrisNum{ get; set; }
        
        public SyntheticRequest(int num) 
        {
            this.DebrisNum= num;
        }
}

2.建立抽象角色類

	//抽象角色類,可以通過合成得到的道具
    public abstract class Prop
    {
    	//下一級道具,更低一級的道具
        public Prop NextProp{ get; set; }
        //當前道具型別
        public string PropType{ get; set; }
        //建構函式
        public Prop(string type)
        { this.PropType= type; }
 
        /// 該角色的執行行為
        public abstract void Behaviour(SyntheticRequest request);
    }

3.建立具體角色類

    ///高階道具
    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);
            }
        }
    }

4.使用責任鏈模式

	//使用責任鏈模式
	 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
    {
        /// 當前擁有的碎片數量
        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; }
        //建構函式
        public Prop(string type)
        { this.PropType= type; }
 
        /// 該角色的執行行為
        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);
        }
    }
}

優缺點

優點:
       降低了請求的傳送者和接收者之間的耦合;把多個條件判定分散到各個處理類中,使得程式碼更加清晰,責任更加明確。
缺點:
       在找到正確的處理物件之前,所有的條件判定都要執行一遍,當責任鏈過長時,可能會引起效能的問題;可能導致某個請求不被處理。

總結

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

注意

       這裡舉的例子,在實際開發過程中可能不會用到設計模式,希望各位讀者切勿固步自封。