admin管理员组文章数量:1531661
桥接模式(Bridge Pattern)
桥接模式是将抽象部分与它的实现部分分离,使它们都可以独立地变化。
它是一种对象结构型模式,又称为柄体(Handle and Body)模式或接口(Interfce)模式。
Abstraction:定义抽象类的接口,它维护了一个指向Implementor类型对象的引用。
RefinedAbstraction: 扩充由Abstraction定义的接口;
Implementor:定义实现类的接口,该接口不一定要与Abstraction的接口完全一致,事实上两个接口可以完全不同。
一般情况,Implementor接口仅为提供基本操作,而Abstraction则定义了基于基本操作的较高层次操作。
ConcreteImplementorA:实现Implementor接口并定义它的具体实现。
在桥接模式中,两个类Abstraction和Implementor分别定义了抽象与行为类型的接口,
通过调用两接口的子类实现抽象与行为的动态组合。
个人总结:抽象与抽象的结合。
例如:游戏中的角色是可替换的,角色的装备也是可替换的
那么角色就会有个抽象基类,武器装备也是有个抽象基类
在角色的抽象基类里,引用了 武器的抽象基类。
使得 两个 或 多个 层次都可以任意替换,这个就是桥接模式。
算是通过抽象来定义一套依赖结构,达到每个部分都可模块化替换的功能。
简单例子:
Abstraction 和 RefinedAbstraction:
抽象角色基类:
/// <summary>
/// 角色抽象基类 Abstraction
/// </summary>
public abstract class Role_Base
{
public IWeapons weapons;//引用武器的抽象接口
public IEquipment equipment;//引用装备的抽象接口
public abstract void Show_Equipment();
public abstract void Attack();
}
实现:
/// <summary>
/// 战士 RefinedAbstraction A
/// </summary>
public class Warrior : Role_Base
{
//护甲
public override void Show_Equipment()
{
Debug.Log("战士 当前的装备:" + weapons.GetName() + " : " + equipment.GetName());
}
//攻击
public override void Attack()
{
Debug.Log("穿着 " + equipment.GetName() + " 的 战士 正在使用 " + weapons.GetName() + " 攻击");
}
}
/// <summary>
/// 法师 RefinedAbstraction B
/// </summary>
public class Mage : Role_Base
{
//护甲
public override void Show_Equipment()
{
Debug.Log("法师当前的装备:" + weapons.GetName() + ":" + equipment.GetName());
}
//攻击
public override void Attack()
{
Debug.Log("穿着 " + equipment.GetName() + " 的 法师 正在使用 " + weapons.GetName() + " 攻击");
}
}
Implementor 和 ConcreteImplementor:
抽象武器接口:
/// <summary>
/// 武器 Implementor A
/// </summary>
public interface IWeapons
{
string GetName();
}
实现:
/// <summary>
/// 铁剑 ConcreteImplementorA_A
/// </summary>
public class IronSword : IWeapons
{
public string name = "铁剑";
public string GetName()
{
return name;
}
}
/// <summary>
/// 魔法杖 ConcreteImplementorA_B
/// </summary>
public class MagicWand : IWeapons
{
public string name = "魔法杖";
public string GetName()
{
return name;
}
}
抽象装备接口:
/// <summary>
/// 装备 Implementor B
/// </summary>
public interface IEquipment
{
string GetName();
}
实现:
/// <summary>
/// 铁甲 ConcreteImplementorB_A
/// </summary>
public class IronArmor : IEquipment
{
public string name = "铁甲";
public string GetName()
{
return name;
}
}
/// <summary>
/// 魔法袍 ConcreteImplementorB_B
/// </summary>
public class MagicRobes : IEquipment
{
public string name = "魔法袍";
public string GetName()
{
return name;
}
}
运行测试:
void Start()
{
Role_Base role1; //声明一个角色
role1 = new Warrior();//新建战士
role1.weapons = new IronSword();//装备铁剑
role1.equipment = new IronArmor();//装备铁甲
role1.Show_Equipment();//展示装备
role1.Attack();//攻击
role1 = new Mage();//替换角色为法师
role1.weapons = new MagicWand();//装备法杖
role1.equipment = new MagicRobes();//装备法袍
role1.Show_Equipment();//展示装备
role1.Attack();//攻击
role1 = new Mage();//替换角色为近战法师
role1.weapons = new IronSword();//装备铁剑
role1.equipment = new MagicRobes();//装备法袍
role1.Show_Equipment();//展示装备
role1.Attack();//攻击
role1 = new Warrior();//替换角色为魔法战士
role1.weapons = new MagicWand();//装备法杖
role1.equipment = new IronArmor();//装备铁甲
role1.Show_Equipment();//展示装备
role1.Attack();//攻击
}
结果:
版权声明:本文标题:C#设计模式:5.桥接模式 u3d学习总结笔记本 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1726268595a1063541.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论