Java Reference
In-Depth Information
Lambda-Enabled Design Patterns
One of the other bastions of design we're all familiar with is the idea of design patterns . Pat-
terns document reusable templates that solve common problems in software architecture. If
you spot a problem and you're familiar with an appropriate pattern, then you can take the
pattern and apply it to your situation. In a sense, patterns codify what people consider to be a
best-practice approach to a given problem.
Of course, no practice is ever the best practice forever. A common example is the once-popu-
lar singleton pattern, which enforces the creation of only one instance of an object. Over the
last decade, this has been roundly criticized for making applications more brittle and harder
to test. As the Agile software movement has made testing of applications more important, the
issues with the singleton pattern have made it an antipattern : a pattern you should never use.
In this section, I'm not going to talk about how patterns have become obsolete. We're instead
going to look at how existing design patterns have become better, simpler, or in some cases
implementable in a different way. In all cases, the language changes in Java 8 are the driving
factor behind the pattern changing.
Command Pattern
A command object is an object that encapsulates all the information required to call another
method later. The command pattern is a way of using this object in order to write generic
code that sequences and executes methods based on runtime decisions. There are four classes
that take part in the command pattern, as shown in Figure 8-1 :
Receiver
Performs the actual work
Command
Encapsulates all the information required to call the receiver
Invoker
Controls the sequencing and execution of one or more commands
Client
Creates concrete command instances
Search WWH ::




Custom Search