Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Kenneth A. Kousen - Making Java Groovy - 2014.pdf
Скачиваний:
50
Добавлен:
19.03.2016
Размер:
15.36 Mб
Скачать

Cool AST transformations

81

return new ImmutablePoint(x:xval,y:yval)

}

}

Now the Java client can instantiate ImmutablePointFactory and then invoke the newImmutablePoint factory method, supplying the desired x and y values.

Everything works, that is, until you succumb to the temptation to follow standard practices in the Java API and make the factory class a singleton. That’s the subject of the next subsection.

4.4.3Creating singletons

When a new Java developer first discovers the wide, wonderful world of design patterns, one of the first ones they tend to encounter is Singleton. It’s an easy pattern to learn, because it’s easy to implement and only involves a single class. If you only want one instance of a class, make the constructor private, add a static final instance variable of the class type, and add a static getter method to retrieve it. How cool is that?

Unfortunately, our poor new developer has wandered into a vast jungle, full of monsters to attack the unwary. First of all, implementing a true singleton isn’t nearly as easy as it sounds. If nothing else, there are thread safety issues to worry about, and because it seems no Java program is every truly thread-safe the results get ugly fast.

Then there’s the fact that a small but very vocal contingent of developers view the whole Singleton design pattern as an anti-pattern. They trash it for a variety of reasons, and they tend to be harsh in their contempt for both the pattern and anyone foolish or naïve enough to use it.

Fortunately I’m not here to resolve that issue. My job is to show you how Groovy can help you as a Java developer, and I can do that here. As you may have anticipated based on the title of this section, there’s an AST transformation called @Singleton.

To use it all I have to do is add the annotation to my class. Here I’ve added it to the

ImmutablePointFactory from earlier:

@Singleton

class ImmutablePointFactory {

ImmutablePoint newImmutablePoint(xval,yval) { return new ImmutablePoint(x:xval,y:yval)

}

}

Again, I can’t resist saying it: that was easy. The result is that the class now contains a static property called instance, which contains, naturally enough, the one and only instance of the class. Also, everything is implemented in as correct a manner as possible by the author10 of the transformation. In Groovy code I can now write the following:

ImmutablePoint p = ImmutablePointFactory.instance.newImmutablePoint(3,4)

10Paul King, one of the coauthors of Groovy in Action (Manning, 2007) and a fantastic developer. Let me be blunt about this: everything Paul King writes is good. He tends to add his presentations to SlideShare.net as well, so go read them as soon as humanly possible.

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]