设计模式之Bridge——游戏篇(原创)

设计模式之Bridge——游戏篇


   今天从电子市场买来两张游戏碟,一张是三国游戏(
SanGuoGame),一张是CS游戏(CSGame)。玩过游戏的人可能都知道,游戏对计算机系统(ComputerSystem)是有要求的,可能一个游戏在Windows98系统下能玩,到了Windows2000系统下就不能玩了,因此为了不走冤枉路,先看看游戏要求的计算机系统(ComputerSystem)可是一个好习惯呦!

 

好了,闲话少叙开始我们的游戏旅程吧:

1、  在这里,先定义计算机系统(ComputerSystem)接口类:

public interface ComputerSystem {

  public abstract void  playGame(); //玩游戏

}

2、  再定义对计算机系统(ComputerSystem)接口的具体实现类:

AWindows98系统

public class Windows98 implements ComputerSystem{

  public void  playGame(){

    System.out.println("Windows98游戏!");

  }

}

BWindows2000系统

public class Windows2000 implements ComputerSystem{

  public void  playGame(){

    System.out.println("Windows2000游戏!");

  }

}

3、  定义游戏(Game)类:

public abstract class  Game  {

  public abstract void play(); //玩游戏

 

  protected ComputerSystem getSetupSystem(String type) { //获得要安装的系统

    if (type.equals("Windows98")) { //如果游戏要求必须安装在Windows98

      return new Windows98(); //使用Windows98系统

    }

    else if (type.equals("Windows2000")) { //如果游戏要求必须安装在Windows2000

      return new Windows2000(); //使用Windows2000系统

    }

    else {

      return new Windows98(); //默认启动的是Windows98系统

    }

  }

}

4、  定义游戏(Game)类的子类:

A:三国游戏(SanGuoGame

public class SanGuoGame extends Game {

  private ComputerSystem computerSystem;

  public SanGuoGame(String type) {//看游戏要求安装在那个系统上

    computerSystem = getSetupSystem(type);//那么使用这个系统

  }

 

  public void play() {//玩游戏

    computerSystem.playGame();

    System.out.println("我正在玩三国,不要烦我!");

  }

}

BCS游戏(CSGame

public class CSGame extends Game {

  private ComputerSystem computerSystem;

  public CSGame(String type) { //看游戏要求安装在那个系统上

    computerSystem = getSetupSystem(type); //那么使用这个系统

  }

 

  public void play() { //玩游戏

    computerSystem.playGame();

    System.out.println("我正在玩CS,不要烦我!");

  }

}

5、编写测试类:

public class Test  {

  public static void main(String[] args) {

    Game sanguo = new SanGuoGame("Windows98"); //游戏要求Windows98

    sanguo.play();

 

    sanguo = new SanGuoGame("Windows2000");//游戏要求Windows2000

    sanguo.play();

 

    Game cs = new CSGame("Windows98"); //游戏要求Windows98

    cs.play();

 

    cs = new CSGame("Windows2000");//游戏要求Windows2000

    cs.play();

  }

}

6、说明:

ABridge定义:将抽象和行为划分开来,各自独立,但能动态的结合。

B:从本例中我们可以看到,不同的游戏对系统的要求是不同的,三国和CS可能都需要Windows98系统,也可能需要Windows2000系统,甚至要求不相同的系统,因此处理这类问题,我们就可以用Bridge这种模式了。

      C:这里行为是指游戏,抽象是指系统!  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
桥梁模式(Bridge)是一种结构型设计模式,它将抽象部分与实现部分分离,使它们可以独立变化。这种模式通过委托的方式将两个层次结构连接起来,从而减少它们之间的耦合度。 在C#中实现桥梁模式,首先需要定义抽象部分和实现部分的接口或抽象类。然后在抽象部分中持有一个指向实现部分的引用,并通过该引用将实现部分的具体实现委托给实现部分。 下面是一个简单的示例代码: ```csharp // 实现部分的接口 interface IImplementor { void OperationImpl(); } // 具体实现部分A class ConcreteImplementorA : IImplementor { public void OperationImpl() { Console.WriteLine("Concrete Implementor A"); } } // 具体实现部分B class ConcreteImplementorB : IImplementor { public void OperationImpl() { Console.WriteLine("Concrete Implementor B"); } } // 抽象部分的接口 interface Abstraction { void Operation(); } // 具体抽象部分 class RefinedAbstraction : Abstraction { private IImplementor implementor; public RefinedAbstraction(IImplementor implementor) { this.implementor = implementor; } public void Operation() { implementor.OperationImpl(); } } // 使用示例 class Program { static void Main(string[] args) { IImplementor implementorA = new ConcreteImplementorA(); Abstraction abstractionA = new RefinedAbstraction(implementorA); abstractionA.Operation(); // Output: Concrete Implementor A IImplementor implementorB = new ConcreteImplementorB(); Abstraction abstractionB = new RefinedAbstraction(implementorB); abstractionB.Operation(); // Output: Concrete Implementor B } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值