开发者

How to properly name classes inside a domain namespace?

Let's say I'm trying to implement a Chess Game.

I'd create my Chess classes in a Chess namespace. Now, 开发者_JS百科should I prefix all my Chess related classes with the Chess word, or as they are already inside that namespace, should I just call them by what they are?

Example:

ChessGame vs Game
ChessPiece vs Piece


Adding a prefix that matches the containing namespace is a waste of time and provides no real benefit. Just go with Game, Piece, etc.


In java the domain namespace is the paquetage.

"name of a class" is : name of a paquage + an identifier.

In short, the identifier of the class is the name of the class, but it's a human convention. The name of the class is always paquage+identifier.

For anonyme class, java provide an identifier.

doing :

a.b.c.Foo foo = new a.b.c.Foo();

is equivalent to

import a.b.c.Foo;

...

Foo foo = new Foo();

How to name paquage / domain space ? With your name of your domain :-), somtimes with "ing" as suffix (java.util.logging).

You can also simulate some domain namespaces with the nested class (inner static class).


Suppose you had a language without any namespaces or packages (in some languages like Java the package is program module and namespace at the same time...). To avoid name clashes, you would use the prefixes. But you have to take care to name properly all program elements and write some characters every time you introduce something new. So the solution would be to introduce namespace into the language, construct which will automatically prefix each element with some name, which you can specify at one place (avoiding typos). Now as long as the namespace names do not class, you are safe. Namespaces allowed to specify the prefix on one place, so you can easily use longer names and therefore avoid the clash without having to type more each time. Look for example how the packages of some libraries are named - they use namespace uri (e.g. country_abbreviation.compay_name.product_name.program_module). However writing the whole identifier names, with package name, tend to be tedious, so many languages invented constructs which allow to use names from namespace as if you used them right in theri namespace (e.g. using, import etc.). But this gets us back to the original situation. The solution is to avoid use of implicit namespaces for program elements, which might change, as in your situation. So the solution you want is to use namespaces for you different games, so you can specify in the GUI component which exact Game element you mean by providing the full name with the package name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜