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