Java Reference
In-Depth Information
class. This lets you hide the implementation class from the client method, provid-
ing an additional level of abstraction.
To convert a constructor into a factory method, select the constructor's name,
and choose
Refactor | Replace Constructor with Factory Method
from the
menu. You're asked to specify a name and the class in which the new method
should be created.
IDEA
even suggests names with prefixes like
create
,
new
, and
getInstance
. Another popular name for factory methods that perform an
implicit conversion is
valueOf
. Once the refactoring is completed, the original
constructor is removed, and references to it are replaced with references to the
factory method.
Improving performance by inlining variables and methods
Inlining
is a process of removing a method call or variable reference and replac-
ing it with the body of the method being called or the value of the replaced vari-
able. This change preserves the functionality of the code while avoiding the
method call or variable reference. It's exactly the opposite of the
Extract Method
refactoring. This is usually done as a performance optimization or to remove a
dependency between classes. You should use this refactoring sparingly: The
speed benefits are very minor in most cases, and you'll break encapsulation by
pulling code out of a centralized method and essentially copying it out to its
usages.
To execute the command, select
Refactor | Inline
(
Ctrl+Alt+N
) after select-
ing either a local variable or a method to inline. For methods, you also have a
couple of options:
Inline all invocations and remove the method
■
Inline a single invocation only and keep the method
■
If you try to delete a method that's in use,
Safe Delete
will complain. If
you use the
Inline
refactoring first, however, you can remove the meth-
od with no loss of functionality.
TIP
9.4.6
Restructuring class hierarchies
IDEA
provides a number of refactoring operations that are designed to help you
change the structure of your code by introducing new superclasses and interfaces,
as well as adjusting the relationship between classes, their ancestors, and their
descendents.
IDEA
has a wealth of refactoring options and there isn't enough