简体   繁体   English

静态 - 好还是坏?

[英]Static - Good or Bad?

Alright, so I've been trying to figure out this for far too long now. 好吧,所以我一直试图弄清楚这个问题已经太久了。

I've read countless articles/questions on static , and also singletons. 我读过无数关于static和单身的文章/问题。

This is how I have been using static for quite some time: 这就是我一直使用静态的一段时间:

I have a GameManager.cs, which has TileMap.cs & Player.cs. 我有一个GameManager.cs,它有TileMap.cs和Player.cs。 TileMap.cs needs to access the player's camera, for updating the position of the map: TileMap.cs需要访问玩家的相机,以更新地图的位置:

GameManager.cs:

public static TileMap map;
public static Player player;

Update code, draw, etc. below...

There will only be one of each of these. 每个都只有一个。

TileMap.cs:

Vector2 mapPosition = new Vector2(GameManager.Player.position.X, GameManager.Player.position.Y);

Is this acceptable? 这可以接受吗? What downsides are there to this? 这有什么缺点?

Edit: Seeing as though this question is too broad, let me see if I can be more specific. 编辑:看到这个问题太宽泛,让我看看我是否可以更具体。

Is there a better way to do this rather than through static methods? 有没有比通过静态方法更好的方法呢? I have multiple lists of classes inside TileMap.cs, some of which have Lists inside of them (thinking mostly of my particle engine), so would Update(Player player) be more efficient, or would it not really matter? 我在TileMap.cs中有多个类列表,其中一些列表中有列表(主要考虑我的粒子引擎),因此Update(Player player)会更高效,还是不重要?

PS, I have noticed when the player moves the game map sort of "jitters" (lags for a small fraction of a second). PS,我注意到当玩家移动游戏地图时会出现“紧张”(滞后一小段时间)。 Could this be causing it? 这会导致它吗?

Thanks, 谢谢,
Shyy Shyy

As you are asking about downsides: 正如您在询问缺点:

1) does the code that uses static variables involved in muti threading ? 1)使用多线程涉及的static变量的代码吗? If yes, you may consider locking management. 如果是,您可以考虑锁定管理。 Which easily makes apparently easy code complicated. 这很容易使代码变得容易复杂。

2) Are Player , position and others structures ? 2) Playerposition和其他结构? If so, every time accessing them via property, you create a copy of instances and not access to the references directly. 如果是这样,每次通过属性访问它们时,都会创建实例的副本,而不是直接访问引用。 Considering that code provided some 2D engine, and you are creating a Vector , so probably some rendering pipeline code, this may introduce some serious performance implications. 考虑到代码提供了一些2D引擎,并且您正在创建一个Vector ,所以可能是一些渲染管道代码,这可能会带来一些严重的性能影响。

I've noticed similar questions quite often professionally. 我经常在专业上注意到类似的问题。 So let me give you a straight answer, of which I am well aware that it doens't apply to the general case. 所以,让我给你一个直接的答案,我很清楚,它不适用于一般情况。 See it as an opinion of what I consider 'best practice for beginners'. 将其视为我认为“初学者最佳实践”的观点。

static is often used for making variables available across class boundaries, as-if they are singletons. static通常用于使变量在类边界上可用,如果它们是单例的话。 The singleton pattern here is just a design pattern wrapper (which doesn't solve most of the problems). 这里的单例模式只是一个设计模式包装器(它不能解决大多数问题)。 While this might make programs easier to write, using static can also make programs much more complex if you want to make your application multi-threaded. 虽然这可能使程序更容易编写,但如果要使应用程序具有多线程,使用static也可以使程序更加复杂。

In general I therefore think it's a good idea to avoid using static alltogether and simply pass objects around. 因此,一般来说,我认为避免全部使用static并简单地传递对象是个好主意。

There is one exception, that is: if you need to have data that is accessible across thread boundaries. 有一个例外,即:如果需要跨线程边界可访问的数据。 If this is the case, you'll quickly find yourself in a world of hurt, and it is best to learn as much as you can about locking and use that for all static variables (including their members if they are structures/classes!). 如果是这种情况,你很快就会发现自己处于一个受伤的世界,最好尽可能多地学习锁定并将其用于所有静态变量(包括它们的成员,如果它们是结构/类!) 。

If that isn't good enough as well, you can continue on that path and learn about things like interlocked and memory barriers (but I wouldn't recommend that if you don't need it). 如果这还不够好,你可以继续沿着这条路走下去,了解互锁和记忆障碍等事情(但如果你不需要,我不建议这样做)。

Fortunately, most applications are fast enough to work in a single thread. 幸运的是,大多数应用程序都足够快,可以在单个线程中工作。 I imagine your application to be no exception (and you can probably use a standard game framework to do the multi-threaded part of the application -- PS: if so, be careful with class variables as well, since they might be passed across thread boundaries as well). 我想你的应用程序也不例外(并且你可以使用标准的游戏框架来完成应用程序的多线程部分 - PS:如果是这样,也要小心类变量,因为它们可能会通过线程传递边界也是如此)。

As for your lag: I think you should use a good profiler to find performance hotspots; 关于你的滞后:我认为你应该使用一个好的剖析器来找到性能热点; it's probably unrelated to the use of static . 它可能与static的使用无关。

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

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