DesignPattern_Bridge
摘錄自:設計模式與游戲完美開發(fā)
十年磨一劍,作者將設計模式理論巧妙地融入到實踐中,以一個游戲的完整實現(xiàn)呈現(xiàn)設計模式的應用及經(jīng)驗的傳承 《軒轅劍》之父——蔡明宏、資深游戲制作人——李佳澤、Product Evangelist at Unity Technologies——Kelvin Lo、信仁軟件設計創(chuàng)辦人—— 賴信仁、資深3D游戲美術——劉明愷 聯(lián)合推薦全書采用了整合式的項目教學,即以一個游戲的范例來應用23種設計模式的實現(xiàn)貫穿全書,讓讀者學習到整個游戲開發(fā)的全過程和作者想要傳承的經(jīng)驗,并以淺顯易懂的比喻來解析難以理解的設計模式,讓想深入了解此領域的讀者更加容易上手。
工程GitHub
源碼注釋及命名有所更改,個人感覺會比原版更好理解
- 橋梁模式:將抽象化與實現(xiàn)化脫耦,使得二者可以獨立的變化,也就是說將他們之間的強關聯(lián)變成弱關聯(lián),也就是指在一個軟件系統(tǒng)的抽象化和實現(xiàn)化之間使用組合/聚合關系而不是繼承關系,從而使兩者可以獨立的變化。
using UnityEngine;
using System.Collections;
namespace DesignPattern_Bridge
{
// 抽象操作者基類
public abstract class Implementor
{
public abstract void OperationImp();
}
//操作者1以及先關操作
public class ConcreteImplementor1 : Implementor
{
public ConcreteImplementor1(){}
public override void OperationImp()
{
Debug.Log("執(zhí)行Concrete1Implementor.OperationImp");
}
}
//操作者2以及先關操作
public class ConcreteImplementor2 : Implementor
{
public ConcreteImplementor2(){}
public override void OperationImp()
{
Debug.Log("執(zhí)行Concrete2Implementor.OperationImp");
}
}
// 擴展操作基類
public abstract class Abstraction
{
private Implementor m_Imp = null;
public void SetImplementor( Implementor Imp )
{
m_Imp = Imp;
}
public virtual void Operation()
{
if( m_Imp!=null)
m_Imp.OperationImp();
}
}
// 擴展操作1
public class RefinedAbstraction1 : Abstraction
{
public RefinedAbstraction1(){}
public override void Operation()
{
Debug.Log("物件RefinedAbstraction1");
base.Operation();
}
}
// 擴展操作2
public class RefinedAbstraction2 : Abstraction
{
public RefinedAbstraction2(){}
public override void Operation()
{
Debug.Log("物件RefinedAbstraction2");
base.Operation();
}
}
}
using UnityEngine;
using System.Collections;
using DesignPattern_Bridge;
public class BridgeTest : MonoBehaviour
{
void Start()
{
UnitTest();
}
//
void UnitTest()
{
// 生成擴展操作1
Abstraction theAbstraction = new RefinedAbstraction1();
// 設定相關操作1
theAbstraction.SetImplementor(new ConcreteImplementor1());
theAbstraction.Operation();
// 設定相關操作2
theAbstraction.SetImplementor(new ConcreteImplementor2());
theAbstraction.Operation();
// 生成擴展操作2
theAbstraction = new RefinedAbstraction2();
// 設定相關操作1
theAbstraction.SetImplementor(new ConcreteImplementor1());
theAbstraction.Operation();
// 設定相關操作2
theAbstraction.SetImplementor(new ConcreteImplementor2());
theAbstraction.Operation();
}
}