Java Reference
In-Depth Information
Phones keep getting more and more powerful, so that the term phone is now something
of a misnomer. The current generation of “smart phones” includes a camera, a browser, a
contact manager, a calendar, and more. [ 8 ] If you've already developed classes for all the
components, you can then build a smart phone by delegation. The interesting part is that
the component classes can be in Java, and the container in Groovy.
8 Here's a good quote attributed to Bjarne Stroustrup, inventor of C++: “I've always wished for my computer to be
as easy to use as my telephone; my wish has come true because I can no longer figure out how to use my telephone.”
Consider a trivial Camera class in Java:
public class Camera {
public String takePicture() {
return "taking picture";
}
}
Here also is a Phone class, in Java.
public class Phone {
public String dial(String number) {
return "dialing " + number;
}
}
Now here's a SmartPhone class in Groovy that uses the @Delegate annotation to ex-
pose the component methods through the SmartPhone class (see figure 4.5 ) :
class SmartPhone {
@Delegate Camera camera = new Camera()
@Delegate Phone phone = new Phone()
}
 
Search WWH ::




Custom Search