游戏中的设计模式四(桥接模式)
来源:互联网 发布:c语言库函数大全pdf 编辑:程序博客网 时间:2024/04/29 16:08
写在前面
在游戏中的角色和武器,往往一个角色可以装换多种武器,武器也拥有诸多类型
当角色需要配备不同武器时候,需要修改角色类,使其配备另外的武器对象
这样造成对象与对象之间耦合性高,并且不容易扩展
使用桥接模式,将抽象与实现分离,使它们都可以独立地变化
案例分析
在各个帮派角色中,可以使用倚天剑和屠龙刀,当角色需要切换不同武器时候,就要大量的修改角色类来调用另外的武器
使用桥接模式,将抽象与实现分离
代码编写
角色基类ICharacter
using System.Collections;using System.Collections.Generic;using UnityEngine;public class ICharacter{public IWeapon weapon;public string name;public ICharacter(IWeapon weapon){this.weapon = weapon;}public virtual void Use(){weapon.Start(this.name);}}
丐帮
using System.Collections;using System.Collections.Generic;using UnityEngine;public class GBCharacter : ICharacter{public GBCharacter(IWeapon weapon) : base(weapon){this.name = "GB";}}
武当
using System.Collections;using System.Collections.Generic;using UnityEngine;public class WDCharacter : ICharacter{public WDCharacter(IWeapon weapon) : base(weapon){this.name = "WD";}}
峨眉
using System.Collections;using System.Collections.Generic;using UnityEngine;public class EMCharacter : ICharacter{public EMCharacter(IWeapon weapon) : base(weapon){this.name = "EM";}}
武器基类
using System.Collections;using System.Collections.Generic;using UnityEngine;public abstract class IWeapon{public string name;//开始使用武器public abstract void Start(string name);}
屠龙刀
using System;using System.Collections;using System.Collections.Generic;using UnityEngine;public class TLDWeapon : IWeapon{public TLDWeapon(){this.name = "屠龙刀";}public override void Start(string name){Debug.Log(name + "将武器切换为" + this.name);}}
倚天剑
using System;using System.Collections;using System.Collections.Generic;using UnityEngine;public class YTJWeapon : IWeapon{public YTJWeapon(){this.name = "倚天剑";}public override void Start(string name){Debug.Log(name + "将武器切换为:" + this.name);}}
创建一个脚本继承自Monobehavior
using UnityEngine;class GameContext : MonoBehaviour{private void Start(){//SceneStateManager.GetInstacne.SetSceneState(new StartSceneState());IWeapon ytjWeapon = new YTJWeapon();IWeapon tldWeapon = new TLDWeapon();ICharacter gb = new GBCharacter(ytjWeapon);gb.Use();ICharacter wd = new WDCharacter(tldWeapon);wd.Use();ICharacter em = new EMCharacter(ytjWeapon);em.Use();}}
输出结果如下图所示
这样当我们主角需要配备不同武器时候,只需要修改不同武器类即可
同时代码扩展性也强,比如当我们游戏中有另外一种武器时候不需要修改代码,只是在原来的基础上添加代码
总结
桥接模式用于把抽象化和实现化解耦,使得两者可以独立变化,即实体类的功能独立于接口实现类。这两种类型的类可被结构化而互不影响。
原文地址:blog.liujunliang.com.cn
阅读全文
0 0
- 游戏中的设计模式四(桥接模式)
- 设计模式在游戏中的应用--代理模式(四)
- Python设计模式(四)【桥接模式】
- spring中的设计模式(四)
- Spring框架中的设计模式(四)
- 设计模式在游戏中的应用--策略模式(二)
- 设计模式在游戏中的应用--装饰模式(三)
- 游戏中的设计模式(1)--观察者模式
- 设计模式在游戏中的应用--原型模式(六)
- 设计模式在游戏中的应用--外观模式(八)
- 设计模式在游戏中的应用--观察者模式(十)
- 游戏中的设计模式二(外观模式)
- 游戏中的设计模式三(中介者模式)
- 游戏中的设计模式五(策略模式)
- 游戏中的设计模式六(模板模式)
- 游戏中的设计模式七(建造者模式)
- 游戏中的设计模式八(享元模式)
- 游戏中的设计模式九(命令模式)
- 圆形图片 || 头像
- JAVA获取时间戳,哪个更快
- OSG的traverse()方法解析
- dll动态库调用约定
- 基于xml配置的Spring MVC HelloWorld
- 游戏中的设计模式四(桥接模式)
- GreenDao3.0 使用(包括导入,具体方法,基本使用,加密,数据库升级等)
- SQL 优化实例
- 关卡设计快速入门_6. 执行构建过程
- Hibernate之一对多单向外键关联(OneToMany)
- Unity3D 中的定制特性
- Codeforces-814E An unavoidable detour for home(DP)
- caffe 中 BatchNorm layer设定
- HttpServlet详解