Next we look at the WordLibraries
class. Note that this is written in the
plural form, which usually means that it is a class that will be used to
manage other classes that have something to do with the WordLibrary
class that
we saw earlier. The correct technical name for this type of class is a Facade,
a well-known design pattern that often occurs in Java. The
WordLibraries
class
has a private
constructor. So how can we make new objects if the constructor
is private
? We cannot. That's the point of making the constructor private
. The
class is also final
, although no one would be able to subclass it anyway since
the constructor is private
. The final
on the class is thus a bit redundant.
However, it is quite common for a Facade to also be final
.
The way that we use the WordLibraries
class is to call the static
method
createDefaultWordLibrary()
. Because it is static
, we call it on the class
itself, not on an object. Thus, when we create our default
WordLibrary
object we do it simply with:
WordLibraries.createDefaultWordLibrary()
.
In Java, the naming convention is that fields, local variables and parameters
to methods typically start with lower case letters and use CamelCase. Classes
start with a capital letter and use CamelCase. When I read
WordLibraries.createDefaultWordLibrary()
I know that this is a method called
on a class, thus a static method, without looking at the definition of the
method. Please please please please please please please please please please
please please please follow this convention in your code. Have classes start
with an upper-case letter and fields / local variables / method parameters
start with a lower-case letter. Even if you come from a language that does it
differently, when coding Java, please use our convention, otherwise you will
confuse us horribly when we try to figure out what your code is doing.
We hope you enjoyed this tutorial. If you did, you will also enjoy our courses. We suggest you start with Extreme Java - Advanced Java, followed by Extreme Java - Concurrency Performance for Java 8.
We deliver relevant courses, by top Java developers to produce more resourceful and efficient programmers within their organisations.
We can help make your Java application run faster and trouble-shoot concurrency and performance bugs...