Null Object Pattern
The null object pattern represents an object with neutral or empty behavior.
This is useful to prevent excessive
null
checks in an application.
The
Collections#emptyList()
method is an example of the null
object pattern.
Questions
What is the command pattern? When is it useful?
What is the observer pattern? When is it useful?
What is the strategy pattern? When is it useful?
What is the visitor pattern? When is it useful?
What is the null object pattern? When is it useful?
Reflection
Reflection allows a program to manipulate an object at runtime without knowing
about the object at compile time. For example, if you wanted to debug an object
that didn’t override the
toString()
method, you could use reflection
to inspect every field. Reflection is possible due to a collection of classes that
represent programming constructs such as
Class
,
Field
, and
Method
.
Class
A
Class
is a special object constructed by the JVM that represents classes and
interfaces in an application. If you consider a class file as the blueprint for
an object, you can consider the
Class
class as a blueprint for blueprints.
A
Class
instance can be retrieved from an object by calling the
getClass()
method, or it can be looked up by its fully qualified name with the
Class#forName()
method.
A
Class
provides type introspection for an object. Type introspection allows
a program to examine the properties of an object at runtime. For example,
a
Class
object contains introspective methods such as
getName()
,
getPackage()
,
getDeclaredFields()
,
and
getDeclaredMethods()
. A
Class
can also create an object with the
newInstance()
method. This is widely used in frameworks that need
to instantiate objects defined in XML files.
Field
A
Field
provides access to a single field in a class or an interface. A
Field
contains introspective methods that allow you to get a value from a field, as well
as reflective methods that allow you to a set a value into a field. However, a flag
must be set on the
Field
object via the
setAccessible()
method to allow
access to fields with limited visibility.
Method
A
Method
object provides access to a single method in a class or interface.
A
Method
contains introspective methods that provide information about the
method declaration, as well as reflective methods that allow you to invoke the
method. However, a flag must be set on the
Method
object via the
setAccessible()
method to allow access to methods with limited visibility.
Do'stlaringiz bilan baham: |