摘錄自:設(shè)計模式與游戲完美開發(fā)
十年磨一劍入热,作者將設(shè)計模式理論巧妙地融入到實(shí)踐中巴柿,以一個游戲的完整實(shí)現(xiàn)呈現(xiàn)設(shè)計模式的應(yīng)用及經(jīng)驗(yàn)的傳承 《軒轅劍》之父——蔡明宏届良、資深游戲制作人——李佳澤半开、Product Evangelist at Unity Technologies——Kelvin Lo舶吗、信仁軟件設(shè)計創(chuàng)辦人—— 賴信仁、資深3D游戲美術(shù)——劉明愷 聯(lián)合推薦全書采用了整合式的項目教學(xué)歹颓,即以一個游戲的范例來應(yīng)用23種設(shè)計模式的實(shí)現(xiàn)貫穿全書坯屿,讓讀者學(xué)習(xí)到整個游戲開發(fā)的全過程和作者想要傳承的經(jīng)驗(yàn),并以淺顯易懂的比喻來解析難以理解的設(shè)計模式巍扛,讓想深入了解此領(lǐng)域的讀者更加容易上手领跛。
工程GitHub
using UnityEngine;
using System.Collections;
namespace DesignPattern_ChainofResponsibility
{
// 處理訊息的介面
public abstract class Handler
{
protected Handler m_NextHandler = null;
public Handler( Handler theNextHandler )
{
m_NextHandler = theNextHandler;
}
public virtual void HandleRequest(int Cost)
{
if(m_NextHandler!=null)
m_NextHandler.HandleRequest(Cost);
}
}
// 處理所負(fù)責(zé)的訊息1
public class ConcreteHandler1 : Handler
{
private int m_CostCheck = 10;
public ConcreteHandler1( Handler theNextHandler ) : base( theNextHandler )
{}
public override void HandleRequest(int Cost)
{
if( Cost <= m_CostCheck)
Debug.Log("ConcreteHandler1.核準(zhǔn)");
else
base.HandleRequest(Cost);
}
}
// 處理所負(fù)責(zé)的訊息2
public class ConcreteHandler2 : Handler
{
private int m_CostCheck = 20;
public ConcreteHandler2( Handler theNextHandler ) : base( theNextHandler )
{}
public override void HandleRequest(int Cost)
{
if( Cost <= m_CostCheck)
Debug.Log("ConcreteHandler2.核準(zhǔn)");
else
base.HandleRequest(Cost);
}
}
// 處理所負(fù)責(zé)的訊息3
public class ConcreteHandler3 : Handler
{
public ConcreteHandler3( Handler theNextHandler ) : base( theNextHandler )
{}
public override void HandleRequest(int Cost)
{
Debug.Log("ConcreteHandler3.核準(zhǔn)");
}
}
}
using UnityEngine;
using System.Collections;
using DesignPattern_ChainofResponsibility;
public class ChainofResponsibilityTest : MonoBehaviour {
// Use this for initialization
void Start () {
UnitTest();
}
//
void UnitTest () {
// 建立Cost驗(yàn)証的連接方式
ConcreteHandler3 theHandle3 = new ConcreteHandler3(null);
ConcreteHandler2 theHandle2 = new ConcreteHandler2(theHandle3);
ConcreteHandler1 theHandle1 = new ConcreteHandler1(theHandle2);
// 確認(rèn)
theHandle1.HandleRequest(10);
theHandle1.HandleRequest(15);
theHandle1.HandleRequest(20);
theHandle1.HandleRequest(30);
theHandle1.HandleRequest(100);
}
}