Am I using statics in the right direction?

I am writing an XNA engine and I save all models in List. To be able to use this throughout the engine, I did it public static List<Model>, so I can access it from any new classes that I am developing. It certainly makes getting a list of models very easy to get too, but is it the right use? Or would I rather pass a variable through a method declaration?

+5
source share
5 answers

In OOP, it is generally recommended that you avoid using static methods and properties unless you have a good reason for doing so. One reason for this is that in the future you may want to have two or more instances of this list for some reason, and then you will get stuck with static calls.

Static methods and properties are too harsh. As Stevey claims this:

Static methods are as flexible as granite. Each time you use it, you execute part of your program in concrete. Just make sure you don’t hurt your foot; you watch it harden. Someday you will be surprised that the hell you really need a different implementation of this PrintSpooler class and it should have been an interface, factory, and a set of class implementations. D'o!

+5

"Doing the Simplest Thing That, , ". (public static #), . . " " Visual Studio .

, "" -. , , . , "" " ".

- , , .

, . , ContentManager , . , , , Game ContentManager public static Game.

, , , , : . private static , , ConentManager public static LoadContent. , , , ( ). .

+5

, .

, , . , .

+1

Singleon, .
MSDN.

0

.

, , , , -, " " , , , , .

, , , .

In other cases, encapsulating globally accessible data in a “global” object (or a static object, the same) greatly simplifies the encoding of OOP.

You can get the middle level by writing the GetModels () global function, which returns a list of models. Or use DI to automatically enter a list of models.

0
source

All Articles