【问题标题】:Class expose different levels of abstraction at different places类在不同的地方暴露不同层次的抽象
【发布时间】:2014-08-13 15:05:58
【问题描述】:

我的问题非常具体,很难搜索,所以到目前为止我还没有成功。 我一直在思考我的这个特殊问题,但我似乎不够聪明。也许其他人是:

这应该是 Unity-Game 的状态机,其中 Game 类通过使用简单的 StateMachine 将其状态类型作为通用参数来管理关卡。 当我将游戏内脚本 SceneMaster 添加到 StateMachine 本身的结构中时,它会变得复杂。

Level(LevelMachine 的状态)应该有一个 SceneMaster 作为 Actor,但 LevelMachine 不应该关心 LevelStates(Level 的状态)是什么具体类型。

说得够多了,也许有人甚至有一个更合乎逻辑和更简单的解决方案。 非常感谢您的任何帮助

//LevelMachine takes States of type Level
public class LevelMachine : StateMachine<Level> {} //<---here is the problem, I don't want Level to be generic here

//Level is a State of LevelMachine, Level should be acting on a LevelMaster
public abstract class Level<S> : StateObject<LevelMaster<S>> where S : LevelMaster<S> {}



public interface ILevelMaster : IStateObject {}
//LevelMaster itself is a StateMachine as well, it takes states of type LevelState
public abstract class LevelMaster<S> : StateMachine<LevelState<S>>, ILevelMaster where S : ILevelMaster {}

//StateObject takes an Actor of type LevelMaster
public abstract class LevelState<A> : StateObject<A> where A : LevelMaster<A> {}



public class Level01_State : LevelState<Level01_Master> {
    public Level01_State (Level01_Master actor) : base (actor) {}
}

public class Level01_Master : SceneMaster<Level01_Master> {}



public class LEVEL01 : Level<Level01_Master> {
    public void Act () {
        Debug.Log (Actor); //Actor is supposed to be of type Level01_Master
    }
}

public class Game {
    //levelMachine should not care about what kinds of LevelStates the Levels in it take
    public static LevelMachine levelMachine = new LevelMachine();

    public static initialize() {
        levelMachine.AddState (new LEVEL01());
    }
}

【问题讨论】:

  • 我并没有完全按照你说的是你的问题,但如果我理解你试图去的方向,这似乎是一个我可以用工厂模式或抽象模式解决的问题工厂模式? stackoverflow.com/q/5739611/1718702
  • 嗯,谢谢大家的帮助。我真的不知道工厂对我有什么好处,因为我的问题不是创建实例(无论如何都包含唯一代码),但也许这只是我缺乏创造力。我想问题无法以我尝试的方式解决。我尝试引入的任何接口来替代 Level 以使其成为非泛型(我需要 LevelMachine)最终在一个或另一个位置不兼容。所以现在我选择在 Level 中添加一个 S 类型的 var,通过强制转换为 S,在接收 ILevelMaster 的方法中分配它。无论如何,非常感谢!

标签: c# generics unity3d game-engine state-machine


【解决方案1】:

适配器模式和工厂模式的结合可能会对您有所帮助。适配器模式访问http://dofactory.com/net/adapter-design-pattern链接,工厂模式访问http://dofactory.com/net/factory-method-design-pattern链接。

【讨论】:

  • 如果您可以使用这些模式快速给出一个大纲,那就太棒了。 - 现在我正在考虑它,我可能会找到一个接口 ILevel 能够在 LevelMachine 中替换 Level。也许到目前为止它没有起作用,因为我有一种错觉,我可以通过 levelMachine.Level.Actor 访问正确类型的 Level.Actor,如果 LevelMachine 将级别存储为 ILevel 类型,这当然是不可能的。因此,强制转换为 Level 是不可避免的。打扰您了,谢谢!
猜你喜欢
  • 2012-07-29
  • 2019-03-21
  • 2011-03-30
  • 1970-01-01
  • 2018-06-19
  • 2017-06-16
  • 2023-03-12
  • 2022-08-18
  • 1970-01-01
相关资源
最近更新 更多