简体   繁体   English

拥有一个公共只读对象,每个类相同的本地只读对象或直接引用库对象更好吗?

[英]Is it better to have a public readonly object, identical local readonly objects per class, or to directly reference a library object?

I'm building a game using C# in XNA and I'm not sure which of these four ways is best to refer to the "Viewport" for reading access only: 我在XNA中使用C#构建一个游戏,我不确定这四种方法中哪一个最好只能引用“视口”来读取访问权限:

  1. Have a public static variable that every class can access by calling Game.Viewport (Game is the main class: 有一个公共静态变量,每个类都可以通过调用Game.Viewport来访问(Game是主类:

    public static readonly Viewport Viewport = GraphicsDevice.Viewport; public static readonly Viewport Viewport = GraphicsDevice.Viewport;

in use: 正在使用:

rectangle = new Rectangle (Game.Viewport/2, ...
  1. Have a local variable that refers to the Viewport for each class that needs the viewport: 有一个局部变量引用需要视口的每个类的视口:

    private readonly Viewport viewport = GraphicsDevice.Viewport; private readonly Viewport viewport = GraphicsDevice.Viewport;

in use: 正在使用:

rectangle = new Rectangle (viewport/2, ...
  1. Use a local variable passed down into the constructor of each class that needs the viewport (this would be the only one that didn't require imported Microsoft.Xna.Framework.Graphics): 使用传递给需要视口的每个类的构造函数的局部变量(这将是唯一一个不需要导入Microsoft.Xna.Framework.Graphics的类):

    private readonly Viewport viewport; 私人只读视口视口;

then in the constructor: 然后在构造函数中:

viewport = pViewport;

in use: 正在使用:

rectangle = new Rectangle (viewport/2, ...
  1. Directly reference the viewport from the class library everywhere where it's needed (in use): 在需要(使用中)的任何地方直接从类库中引用视口:

    rectangle = new Rectangle (GraphicsDevice.Viewport.Width/2, ... rectangle = new Rectangle(GraphicsDevice.Viewport.Width / 2,...

I wondering which one is best in terms of speed and which one is best in terms of readability. 我想知道哪一个在速度方面最好,哪个在可读性方面最好。 Personally, I feel like the fourth method would be easiest and fastest so I don't need to create any references to the Viewport and it's clear that I'm referring to something that is global. 就个人而言,我觉得第四种方法最简单,最快,所以我不需要创建任何对视口的引用,很明显我指的是全局的东西。 It seems like it's taking out the "middleman", but then you have to import the XNA graphics library to every class that uses the Viewport. 看起来它正在取出“中间人”,但是你必须将XNA图形库导入到使用Viewport的每个类。

What do you guys think? 你们有什么感想?

I suppose it depends on your design. 我想这取决于你的设计。 If this is just a simple, one-off application, I'd go with your first example. 如果这只是一个简单的一次性应用程序,我会选择你的第一个例子。 You have a single viewport, none of your classes are dependent on the GraphicsDevice or however you decide to manage that viewport. 您有一个视口,您的所有类都不依赖于GraphicsDevice或者您决定管理该视口。

Storing the single object there and referencing it in your code will be fast; 将单个对象存储在那里并在代码中引用它将很快; it's just a field reference so they don't come much faster than that. 它只是一个场参考,所以它们的速度并不快。 Calling GraphicsDevice.Viewport every time, especially since it's a property means you'll be running a property getter method every single time you want to access it. 每次都调用GraphicsDevice.Viewport特别是因为它是一个属性意味着你每次想要访问它都会运行一个属性getter方法。

EDIT: If you want to quickly throw in a property wrapper on Game.Viewport (instead of a field) so you can track access (say for logging) temporarily, then you're free to do it this way. 编辑:如果你想快速在Game.Viewport (而不是一个字段)上Game.Viewport一个属性包装器,这样你就可以暂时跟踪访问(比如记录),那么你就可以这样做了。 If you choose the latter with GraphicsDevice.Viewport , you have to do it every place in your code where you call it (code duplication). 如果您使用GraphicsDevice.Viewport选择后者,则必须在代码中的每个位置执行此操作(代码重复)。

If this is for a more reusable library, if it makes sense to restrict access to your Viewport to only the relevant classes, or want to separate your dependency to Game , passing it down via constructors as dependency injection can be worthwhile, especially when it comes to unit testing. 如果这是一个更可重用的库, 如果将Viewport的访问权限限制在相关的类中,或者想要将依赖项与Game分开是有意义的,那么通过构造函数将它作为依赖注入传递下来是值得的,特别是当它来到时进行单元测试。

I wouldn't even consider option 2 (evaluating GraphicsDevice.Viewport for each class as a private field). 我甚至不会考虑选项2(将每个类的GraphicsDevice.Viewport评估为私有字段)。 At the very least you could wrap it with your own reusable static class (which is what you're doing with Game ) 至少你可以用你自己的可重复使用的静态类包装它(这是你在Game做的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM