Java Reference
In-Depth Information
A new class enters this life!
Wow! Who has created me?
Compiled a new class: class Test
Created its instance: Test@79dd63b4
Truly a Frankenstein‐inspired class. If you think this is cheating by requiring
the JDK, note that the Eclipse developers have managed to implement a
Java compilation library on top of the JRE, which you could use here instead.
There's also the BCEL (Byte Code Engineering Library). Finally, if you know
the methods a class should have beforehand (as was the case in the previ-
ous example), you can start with an interface and then rely only on reflection
to do the rest for you, without a class loader or compilation. Again, this is
too advanced to discuss in full here (in fact, this is already very deep in Java's
internals at this point). Finally, it's interesting to know that some developers
have applied this concept toward creating software solutions that allow you to
change a Java program while it is running, without having to stop it. Look up
“hot code replace” if you wish to learn more about this.
behavioral patterns
Behavioral patterns are concerned with organizing communication between objects in an efficient
and clean manner.
Here, the chain‐of‐responsibility, observer (and model‐view‐controller), iterator, visitor, template,
and strategy patterns are covered. The command, interpreter, mediator, and state patterns are not
discussed as they are less commonly used. They are interesting and very useful in particular cases,
but deal less with architecture‐related object‐oriented programs and are therefore less suitable for a
beginner's tour.
Chain‐of‐responsibility Pattern
The chain‐of‐responsibility pattern describes a method that organizes objects in a chain. This is use-
ful when certain commands are handled by different objects, each of them passing the command to
the next object in the chain.
A simple example to illustrate this pattern is through a logging system. Imagine that an abstract
Logger class is defined like so:
public abstract class Logger {
private Logger nextLoggerInChain;
public void setNext(Logger logger) {
nextLoggerInChain = logger;
}
public void logMessage(String message) {
performLogging(message);
 
Search WWH ::




Custom Search