Unity 设计模式之 桥接模式的实例介绍
1、打开Unity,新建一个空工程,具体如下图
2、在工程中,新建几个脚本,然后双击打开,具体如下图
3、脚本的具体代码和代码说明如下图
4、HandsetSoftwareImplementor 脚本具体内容如下:///<summary>///Handsetsoftwareimplementor.///</summary>publicabstractclassHandsetSoftwareImplementor{publicabstractvoidRun();}
5、HandsetGameConcreteImplementor脚本具体内容如下:usingUnityEngin髫潋啜缅e;///<summary>///Handsetgameconcreteimplementor.///</summary>publicclassHandsetGameConcreteImplementor:HandsetSoftwareImplementor{publicoverridevoidRun() { Debug.Log("运行手机游戏!"); }}
6、HandsetAddressListConcreteImplementor脚本具体内容如下:usingUnityEngine;publicclassHandsetAddressListConcreteImplementor:HandsetSoftwareImplementor{publicoverridevoidRun() { Debug.Log("运行手机通讯录!"); }}
7、HandsetBrandAbstraction脚本短铘辔嗟具体内容如下:publicabstractclassHandsetBrandAbstracti泠贾高框on{protectedHandsetSoftwareImplementorhandsetSoftware; protectedstringBrandName;publicvoidSetHandsetSoftware(HandsetSoftwareImplementorhandsetSoftware){this.handsetSoftware=handsetSoftware; } publicabstractvoidRun();}
8、HandsetBrandAbstraction脚本具体内容如下:usingUnityEngine;publicclassHandsetBrandARefinedImplementor:HandsetBrandAbstraction{publicoverridevoidRun() { Debug.Log("在HandsetBrandA上-------"); handsetSoftware.Run(); }}
9、HandsetBrandAbstraction脚本短铘辔嗟具体内容如下:usingUnityEngine;publicclassHandsetBrand蚱澄堆别BRefinedImplementor:HandsetBrandAbstraction{publicoverridevoidRun() { Debug.Log("在HandsetBrandB上-------"); handsetSoftware.Run(); }}
10、HandsetBrandAbstraction脚本短铘辔嗟具体内容如下:usingUnityEngine;publicclassTest:MonoBeh锾攒揉敫aviour{//Usethisforinitialization voidStart(){//创建手机品牌 HandsetBrandAbstractionhandsetBrand;//在HandsetBrandA运行游戏和通讯录 handsetBrand=newHandsetBrandARefinedImplementor(); handsetBrand.SetHandsetSoftware(newHandsetGameConcreteImplementor()); handsetBrand.Run(); handsetBrand.SetHandsetSoftware(newHandsetAddressListConcreteImplementor()); handsetBrand.Run();//在HandsetBrandB运行游戏和通讯录 handsetBrand=newHandsetBrandBRefinedImplementor(); handsetBrand.SetHandsetSoftware(newHandsetAddressListConcreteImplementor()); handsetBrand.Run(); handsetBrand.SetHandsetSoftware(newHandsetGameConcreteImplementor()); handsetBrand.Run();} }
11、脚本编译正确,回到Unity界面,在场景中新建一个 GameObject,并把 Test 脚本赋给 GameObject,具体如下图
12、运行场景,控制台 Console 打印如下图
13、到此,《Unity 设计模式之 桥接模式的实例介绍》讲解结束,谢谢