Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

8
  • Thanks for your suggestion, but how would you unit test this class? Commented Nov 7, 2015 at 23:11
  • @Harry - It's not a class, it's a struct. See Choosing Between Class and Struct. I don't think this actually exists in Java, see Structs (C# vs Java). Basically a struct is a custom value type that is embedded on the stack or some containing class and is appropriate for lightweight, immutable data. (In that it resembles an enum.) Commented Nov 7, 2015 at 23:17
  • Not sure what you're doing for unit testing, but you can do things like Debug.Assert(Direction.N.Left == Direction.W && Direction.N.Right == Direction.E);. Commented Nov 7, 2015 at 23:21
  • @Harry - do you ever need to serialize your Direction? All of the solutions presented here (immutable structs and classes with private constructors and a fixed set of global singletons) present challenges for serialization. Commented Nov 7, 2015 at 23:24
  • No, I don't need do serialization. I'm translating the code provided in the link provided in the question. Is there a way to simplify this? I still think it's easier to to create 4 classes, one for each direction inheriting from IDirection and implementing all the methods such as TurnLeft, TurnRight and Move. What do you think? Commented Nov 7, 2015 at 23:43