Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm a CS alumni, but we didn't learn much by the way of OOP, or Design Patterns. I've heard the phrase C with iostreams and thought it fitting. Anyway that's besides the point. I am just curious about what I perceive would be a very simple design pattern.

Have any of you played the game Starcraft? You know how you can select/deselect units? Back to that in a second. Suppose I have a Unit class, and I wanted it to keep track of all the units that were selected. I had the idea that I could create a static list/arrray inside the Unit class, and the units themselves could add or remove themselves from the array as they were selected/deselected.

That's just one example, there could be other applications of this "pattern". Say you wanted to maintain a list of visible "tabs" in a web browser or w/e, you could have them add a reference to themselves in a a static array in the class.

The idea is that you could just call Unit.GetSelected() and get a pointer/reference to all of the "selected" or "opened" units/tabs. I am aware that there are other maybe better ways of solving these problems, but that is also besides the point. What would this pattern be called?

share|improve this question
What does "static" mean in this context? Some programming languages have a static keyword which means "associated with the class, not with object instances." Is that what you mean, or do you mean something else? – Robert Harvey Jul 13 '14 at 3:20
The Java or C++ use of the word, meaning the variable belongs to the class and not a particular instance of the class. In other words, there is one variable common to all objects of that type. If it helps identify the pattern, for the purposes listed above I would probably also make it private. – user142504 Jul 13 '14 at 3:24
Oh, I should add, while accessible through a getter it would only be mutable by Unit objects. – user142504 Jul 13 '14 at 3:32

This anti-pattern would be named whatever you like. I'm not sure I've seen it named before, though I would heartily recommend against getting cozy with it. I've seen this in games occasionally when someone thinks it's a good idea to have a global list of "all of the units in the game". Even if that's a good idea, that shouldn't be the responsibility of the unit - it's the responsibility of the game.

For your example, only the UI should know or care what's selected. Your game objects certainly should not.

In a more general sense, having a static that a type knows about tends to be tight coupling - making your code inflexible, hard to test, and hard to use concurrently.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.