Design mode Bridge

objective

  • Separate the abstract part from its implementation part so that they can all change independently.
    The bridge pattern is intended to "decouple abstraction and implementation so that they can be changed independently".

The roles involved in the bridge model are:

  • Abstraction role: abstracts the given definition and holds a reference to the implemented object.
  • Modified abstraction role: extend the abstraction role, change and modify the parent class's definition of abstraction.
  • Implementor role: this role provides the interface of the implementer role, but does not provide the specific implementation. It must be noted that this interface is not necessarily the same as the interface definition of the abstract role, in fact, the two interfaces can be very different. The implementation role should only provide the underlying operation, while the abstract role should only provide a higher layer based on the underlying operation
    Operation.
  • Concrete implementor role: this role provides the concrete implementation of the implementation role interface.

demo:

In the shooting game, a player will have multiple roles and weapons
Roles: Swat, blade, fox, etc
Weapon: M4A1,AK47 AWM, etc
Different characters can use different weapons

Bridge mode is used to decouple specific roles and weapons, and abstract parent classes are used to realize mutual calls

UML:

Script:
Abstract role class

/// <summary>
///Role parent
/// </summary>
public class IRole 
{
    public string name;
    public IWeapon curretUseWeapon;
    public IRole(string name,IWeapon weapon)
    {
        this.name = name;
        curretUseWeapon = weapon;
    }
    public virtual void ChangeWeapon(IWeapon weapon) 
    {
        curretUseWeapon = weapon;
    }
    public virtual void Attrack() 
    {
        if (curretUseWeapon!=null)
        {
            curretUseWeapon.Attrack();
        }
    }

}

Role implementation class:

using UnityEngine;
/// <summary>
///Character SWAT
/// </summary>
public class Role_Swart : IRole
{
    public Role_Swart():base("SWAT",new Weapon_M4A1())
    {

    }
    public override void ChangeWeapon(IWeapon weapon)
    {
        base.ChangeWeapon(weapon);
    }
    public override void Attrack()
    {
        Debug.Log(name + "Used" + curretUseWeapon.name + "There was an attack");
        base.Attrack();
    }
}
using UnityEngine;
/// <summary>
//Ling Fox
/// </summary>
public class Role_LingFox : IRole
{
    public Role_LingFox() : base("Ling Fox", new Weapon_M4A1())
    {

    }
    public override void ChangeWeapon(IWeapon weapon)
    {
        base.ChangeWeapon(weapon);
    }
    public override void Attrack()
    {
        Debug.Log(name + "Used" + curretUseWeapon.name + "There was an attack");
        base.Attrack();
    }
}
using UnityEngine;
/// <summary>
///Character blade
/// </summary>
public class Role_Blade : IRole
{
    public Role_Blade() : base("Blade fighter",new Weapon_M4A1())
    {

    }
    public override void ChangeWeapon(IWeapon weapon)
    {
        base.ChangeWeapon(weapon);
    }
    public override void Attrack()
    {
        Debug.Log(name + "Used" + curretUseWeapon.name + "There was an attack");
        base.Attrack();
    }
}

Weapon abstract class

/// <summary>
///Weapon parent
/// </summary>
public class IWeapon 
{
    public string name;
    public IWeapon(string name)
    {
        this.name = name;
    }
    public virtual void Attrack() { }
}

Weapon realization class

using UnityEngine;
/// <summary>
/// M4A1
/// </summary>
public class Weapon_M4A1 : IWeapon
{
    public Weapon_M4A1():base("M4A1")
    {

    }
    public override void Attrack()
    {
        base.Attrack();
        Debug.Log(name + "There was an attack");
    }
}
using UnityEngine;
/// <summary>
/// AK47
/// </summary>
public class Weapon_AK47 : IWeapon
{
    public Weapon_AK47():base("AK47")
    {

    }
    public override void Attrack()
    {
        base.Attrack();
        Debug.Log(name + "There was an attack");
    }
}
using UnityEngine;
/// <summary>
/// AWM
/// </summary>
public class Weapon_AWM : IWeapon
{
    public Weapon_AWM():base("AWM")
    {

    }
    public override void Attrack()
    {
        base.Attrack();
        Debug.Log(name+"There was an attack");
    }
}

Calling class

using UnityEngine;

public class Bridge: MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
    //You can change roles and use weapons by modifying them
        IRole role = new Role_Blade();
        IWeapon weapon = new Weapon_AWM();
        role.ChangeWeapon(weapon);
        role.Attrack();
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

14 original articles published, praised 0, and 275 visitors
Private letter follow

Posted on Sun, 12 Jan 2020 01:22:04 -0800 by jamjam1