Unity 设计模式之 桥接模式的实例介绍
1、桥接(Bridge)模式:
在Unity等开发中,由于某些类自身的逻辑,具有两个或多个维度的变化,为了使他多维度变化不影响,并且不引入复杂度,所以使用Bridge模式。Bridge模式,把抽象部分(Abstraction)与实现部分(Implementor)分离开来,使它们可以独立地变化,从而不影响其他对象。
2、桥接模式结构图:

3、模式角色:
1)抽象(Abstraction):定义抽象接口,该接口中包含实现具体行为、具体特征的Implementor接口。
2)提炼的抽象(RefinedAbstraction):继承自Abstraction的子类,依旧是一个抽象的事物名。
3)实现(Implementor):定义具体行为,具体特征的应用接口。
4)具体实现(ConcreteImplementor):实现Implementor。
4、模式使用总结:
1、优点
1)减少不同类对象在两个或多个维度扩展时的复杂程度,避免类的过度膨胀。
2)使得两个或多个维度之间的松耦合,使它们沿着各自方向变化而不互相影响
2、适用场景
1) 当一个对象有多个变化因素时,可以考虑使用桥接模式,通过抽象这些变化因素,将依赖具体实现修改为依赖抽象。
2) 当我们期望一个对象的多个变化因素可以动态变化,而且不影响客户端的程序使用时。
3) 如果使用继承的实现方案,会导致产生很多子类,任何一个变化因素都需要产生多个类来完成,就要考虑桥接模式。
5、桥接模式使用案例:

1、打开Unity,新建一个空工程,具体如下图

2、在工程中,新建几个脚本,然后双击打开,具体如下图

3、脚本的具体代码和代码说明如下图







4、HandsetSoftwareImplementor 脚本具体内容如下:
/// <summary>
/// Handset software implementor.
/// </summary>
public abstract class HandsetSoftwareImplementor {
public abstract void Run ();
}
5、HandsetGameConcreteImplementor 脚本具体内容如下:
using UnityEngine;
/// <summary>
/// Handset game concrete implementor.
/// </summary>
public class HandsetGameConcreteImplementor : HandsetSoftwareImplementor {
public override void Run ()
{
Debug.Log ("运行手机游戏!");
}
}
6、HandsetAddressListConcreteImplementor 脚本具体内容如下:
using UnityEngine;
public class HandsetAddressListConcreteImplementor : HandsetSoftwareImplementor {
public override void Run ()
{
Debug.Log ("运行手机通讯录!");
}
}
7、HandsetBrandAbstraction 脚本具体内容如下:
public abstract class HandsetBrandAbstraction {
protected HandsetSoftwareImplementor handsetSoftware;
protected string BrandName;
public void SetHandsetSoftware(HandsetSoftwareImplementor handsetSoftware){
this.handsetSoftware = handsetSoftware;
}
public abstract void Run();
}
8、HandsetBrandAbstraction 脚本具体内容如下:
using UnityEngine;
public class HandsetBrandARefinedImplementor : HandsetBrandAbstraction {
public override void Run ()
{
Debug.Log ("在 HandsetBrandA 上-------");
handsetSoftware.Run ();
}
}
9、HandsetBrandAbstraction 脚本具体内容如下:
using UnityEngine;
public class HandsetBrandBRefinedImplementor : HandsetBrandAbstraction {
public override void Run ()
{
Debug.Log ("在 HandsetBrandB 上-------");
handsetSoftware.Run ();
}
}
10、HandsetBrandAbstraction 脚本具体内容如下:
using UnityEngine;
public class Test : MonoBehaviour {
// Use this for initialization
void Start () {
//创建手机品牌
HandsetBrandAbstraction handsetBrand;
//在 HandsetBrandA 运行游戏和通讯录
handsetBrand = new HandsetBrandARefinedImplementor ();
handsetBrand.SetHandsetSoftware (new HandsetGameConcreteImplementor());
handsetBrand.Run ();
handsetBrand.SetHandsetSoftware (new HandsetAddressListConcreteImplementor());
handsetBrand.Run ();
//在 HandsetBrandB 运行游戏和通讯录
handsetBrand = new HandsetBrandBRefinedImplementor ();
handsetBrand.SetHandsetSoftware (new HandsetAddressListConcreteImplementor());
handsetBrand.Run ();
handsetBrand.SetHandsetSoftware (new HandsetGameConcreteImplementor());
handsetBrand.Run ();
}
}
11、脚本编译正确,回到Unity界面,在场景中新建一个 GameObject,并把 Test 脚本赋给 GameObject,具体如下图

12、运行场景,控制台 Console 打印如下图

13、到此,《Unity 设计模式之 桥接模式的实例介绍》讲解结束,谢谢