设计模式 Bridge 桥接模式

本文介绍了一种使用设计模式解决坦克游戏中不同型号与平台显示差异的问题。通过将抽象部分与实现部分分离,使得两者可以独立变化,降低了代码耦合度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

“ 将抽象部分和实现部分分离,使他们都可以独立的变化。”

                                                                                        -《设计模式》GoF

个人理解:将一个事务进行多方面的抽象分离,利用组合实现绑定关系。

需求:游戏 坦克,多种型号、在不同平台上运行。

前提,有不同型号的坦克,具有相同的动作(射击,移动),但根据不同平台对于相同动作的实现是不同的,调用的API不同(PC和Mobile)。

这里有两个变化的方向:

1.多种型号的坦克(移动速度等不同)。

2.多种平台,实现不同(屏幕显示等不同)。

将第二个变化隔离出来,用组合的方式解决两者的耦合。

For different kinds of tank, we can use abstract class to solve it, some code like this

 

public abstract class tank
{
    .......
    
public abstract void move();
    
public abstract void run();
    .......
}

public abstract t01 : tank
{
    ........
    
public override void move()
    {
        ........
        
//tank display : invoke some api to draw a tank on screen when it moves.
        ........
    }

    
public override void shoot()
    {
        ........
        
//tank display : invoke some api to draw a tank on screen when it shoots.
        ........
    }
}

For different platform, we know the display of tank is different.Then should we do like this?

public class PcT01: tank
{
    
public override void move()
    {
        ........
        
// invoke pc api to draw a  tank on the pc screen.
        ........
    }

    .................................    
}



public class MobileT01: tank
{
    
public override void move()
    {
        ........
        
// invoke mobile api to draw a tank on the mobile screen.
        ........
    }

    .................................    
}

Yeah, If we will get more requirements for different platform this will bring a hug work to our developer.

So we can design like this:

//we put the difference of display between pc and mobile to the abstract class tank.
public abstract class tank
{
    
protected platform pf;
    
public tank(pf)
    {
        ........
        
this.pf = pf;
        ........
    }

    
public abstract void move();
    
public abstract void shoot();
    ........
}

public class T01
{
    
public T01(platform pf):base(pf)
    {
        ........
    }

    
public override move()
    {
        ........
        pf.display();
        ........
    }

    ............

}

public abstract class platform
{
    .......
    
public abstract void display();
    .......
}

public class pcplatform : platform 
{
    
public override void display()
    {        
        
//invoke pc api to draw tank.
    }
}

public class mobileplatform : platform 
{
    
public override void display()
    {        
        
//invoke mobile api to draw tank.
    }
}

Then we can according the plat we use to dicide which platform we should create or new.

Any more requirement for running on different platform, we just need add an abstract class to the system.and create the suitable platform instance.

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值