Bridge (structural mode - bridge mode)

Review Dependency Inversion
Abstraction B: Stable
Implementation Details B: Unstable
insert image description here
If at this time, abstraction B itself becomes unstable for some reason.

Example:
Now there is a game to be launched on PC and mobile. PC and mobile have many differences, such as sound, movement, rendering, etc. One of the game objects, Tank, has different models, such as T50/T75/T90. Dimensions of change in demand : platform changes and Tank
models .
insert image description here


insert image description here

insert image description here

insert image description here

public abstract class TankPlatform
{
    
    
    public abstract void DoShot();
    public abstract void ToMove(object move);
    public abstract void DoTurn(object turn);
}

2. Specifically realize the Tank platform

public  class PCTankPlatform : TankPlatform
{
    
    
    public override void DoShot()
    {
    
    
    }
    public override void ToMove(object move)
    {
    
    
    }
    public override void DoTurn(object turn)
    {
    
    
    }
}
public  class MobileTankPlatform : TankPlatform
{
    
    
    public override void DoShot()
    {
    
    
    }
    public override void ToMove(object move)
    {
    
    
    }
    public override void DoTurn(object turn)
    {
    
    
    }
}

3. Abstract Tank

public abstract class Tank
{
    
    
    protected TankPlatform tankPlat;
    //根据需求写
    //构造器
    public Tank(TankPlatform tankPlat)
    {
    
    
        this.tankPlat = tankPlat;
    }
    //属性,可以动态配置
    public TankPlatform TankPlat
    {
    
    
        get
        {
    
    
            return tankPlat;
        }
        set
        {
    
    
            this.tankPlat = tankPlat;
        }
    }
    public abstract void Shot();
    public abstract void Run();
    public abstract void Turn();
}

4. Specifically realize the Tank

public class T50:Tank
{
    
    
    public T50(TankPlatform tankPlat) : base(tankPlat)
    {
    
    
    }
    public override void Shot()
    {
    
    
        //...
        tankPlat.DoShot();
        //...
    }
    public override void Run()
    {
    
    
        int move = 0;
        //....
        tankPlat.ToMove(move);
        //....
    }
    public override void Turn()
    {
    
    
        string turn = "Left";
        //....
        tankPlat.DoTurn(turn);
        //....
    }
}
public class T75:Tank
{
    
    
    public T75(TankPlatform tankPlat) : base(tankPlat)
    {
    
    
    }
    public override void Shot()
    {
    
    
        //...
        tankPlat.DoShot();
        //...
    }
    public override void Run()
    {
    
    
        int move = 0;
        //....
        tankPlat.ToMove(move);
        //....
    }
    public override void Turn()
    {
    
    
        string turn = "Left";
        //....
        tankPlat.DoTurn(turn);
        //....
    }
}
public class T90:Tank
{
    
    
    public T90(TankPlatform tankPlat) : base(tankPlat)
    {
    
    
    }
    public override void Shot()
    {
    
    
        //...
        tankPlat.DoShot();
        //...
    }
    public override void Run()
    {
    
    
        int move = 0;
        //....
        tankPlat.ToMove(move);
        //....
    }
    public override void Turn()
    {
    
    
        string turn = "Left";
        //....
        tankPlat.DoTurn(turn);
        //....
    }
}

5. Call

public static void Main(string[] args)
{
    
    
    TankPlatform tankPlat = new PCTankPlatform();
    T50 tank = new T50(tankPlat);
}

Key Points
insert image description here
C# does not support multiple inheritance for classes, but supports multiple inheritance for interfaces.

Guess you like

Origin blog.csdn.net/weixin_51565051/article/details/131551157