I'm creating a mock class for a Lexer
object, and I think I may need to do some refactoring. I have two options:
Lexer
, and rename the current Lexer
to something like RealLexer
. Have MockLexer
implement Lexer
, and method calls take anything of type Lexer
. I dislike that my precious Lexer
class is now renamed to something that has no meaning if you don't know that there's a mock class.LexerInterface
(which I already dislike, since it has Interface
in its name), but allowing myself to keep the current Lexer
the way it is. MockLexer
then implements LexerInterface
. Another downside is that method calls take LexerInterface
as params.Both options smell bad to me, so I figured I'd let standards decide for me. Has anyone had experience with this?
Interface names should be capitalized like class names. Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized. Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter.
Naming InterfacesInterfaces should be in the title case with the first letter of each separate word capitalized. In some cases, interfaces can be nouns as well when they present a family of classes e.g. List and Map .
Java uses CamelCase as a practice for writing names of methods, variables, classes, packages, and constants.
There are two main naming conventions for interfaces in Java: no prefix or suffix for interfaces, ~Impl suffix for implementations, ~I prefix for interfaces.
I'd definitely vote for using Lexer
as your interface name. How about adding some information about how or why your implementation does its thing as part of the name?
E.g.:
StringParsingLexer
TokenizingLexer
SingleThreadedLexer
{ThirdPartyLibraryName}DelegatingLexer
Also, do you really need to be explicitly constructing a MockLexer
? Using a framework like Mockito can make your testing considerably easier and faster; You can get started as easily as:
Lexer mockLexer = Mockito.mock(Lexer.class);
Mockito.when(mockLexer.doFoo()).thenReturn("bar");
My recommendation, as I stated in the comments, is to use Lexer
for your interface and DefaultLexer
for the default implementation. This pattern is used quite frequently and as such is very understandable to anyone who will be maintaining your code. As for the mock object, it would also be understandable to name this something like MockLexer
.
As an example of a naming convention that Java uses:
javax.swing.table.TableModel
is an interfacejavax.swing.table.AbstractTableModel
is an abstract class implementing TableModel
javax.swing.table.DefaultTableModel
is an implementation of AbstractTableModel
.
There is however, no recommendation in the Java Codding Conventions outside of using capital letters, nouns, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With