FOREWORD
Ignorant men raise questions that wise men answered a thousand years ago
JOHANN WOLFGANG VON GOETHE
Design patterns are our link to the past and the future. They make up a foundational language that represents well understood solutions to common problems that talented engineers before us have added to our collective knowledge base. Design patterns or blueprints exist in every engineering field in one way or another. Software development is no different. Indeed, design patterns are probably our most tangible link to engineering rather than the more organic and less regimented world of the artisan or craftsman. The art and science of design patterns was brought to the world of software engineeringand more specifically to enterprise Javaby the seminal Gang of Four (GoF) book. They have been with us ever since through our adventures in J2EE, Spring, and now modern lightweight Java EE. This is for very good reasons. Server-side Java developers tend to write the type of mission critical applications that need to stand the test of time and hence benefit the most from the discipline that design patterns represent.
It really takes a special kind of person to write a book on design patterns, let alone a book on how to utilize design patterns in Java EE applications. You require not only basic knowledge of APIs and the patterns themselves, but deep insight that can only come with hard-earned experience, as well as an innate ability to explain complex concepts elegantly. I am glad Java EE now has Murat and Alex to accomplish the mighty feat.
This book fulfills a much needed gap and fills it well. It is also very good that the book is on the cutting edge and covers Java EE 7 and not just Java EE 6 or Java EE 5. In fact many of the design patterns covered, like Singleton, Factory, Model-View-Controller (MVC), Decorator, and Observer, are now incorporated right into the Java EE platform. Others like Facade, Data Access Object (DAO), and Data Transfer Object (DTO) fit elegantly on top. Murat and Alex tackle each pattern, explain its pragmatic motivation, and discuss how it fits into Java EE.
It is an honor and a privilege to write a small opening part of this very important book that I hope will become a very useful part of every good Java EE developer's bookshelf. I hope you enjoy the book, and that it helps you write better, more satisfying enterprise Java applications.
M. REZA RAHMAN
Java EE/GlassFish Evangelist
Oracle Corporation
INTRODUCTION
THIS BOOK DISCUSSES THE CLASSIC DESIGN PATTERNS that were first mentioned in the famous book by the GoF and updates them specifically for Java EE 6 and 7.
In every chapter we describe the traditional implementation of each pattern and then show how to implement it using Java EE-specific semantics.
We use full code examples to demonstrate both the traditional and Java EE implementations and color each chapter with real-life stories that show the use (or misuse) of the pattern.
We investigate the pros and cons of each pattern and examine their usages. Each chapter finishes with some exercises that challenge your understanding of the pattern in Java EE.
WHO THIS BOOK IS FOR
This book is for everyone with any level of experience. It covers almost everything about a pattern, from how it is referred to in other books, to code on basic Java implementation, to Java EE implementation, and finally real life examples of how and when to use a specific pattern. It also has real life war stories that talk about good and bad practices.
Having some basic knowledge of design patterns and Java EE will aid you as you read this book.
If you are already experienced with patterns and basic Java implementations, you may prefer to jump into Java EE implementations. Refreshing your memory and knowledge of design patterns could prove helpful.
WHAT THIS BOOK COVERS
This book covers all classical design patterns that Java EE offers as part of standard implementation, besides some new patterns. The coverage goes back to Java EE5 and is up to date for the latest version available, which is Java EE 7.
We hope this book will be a reference you will keep on your shelf for a long time.
HOW THIS BOOK IS STRUCTURED
Each chapter focuses on a design pattern. If the pattern is classical, a simple Java implementation is given after the explanation of the pattern. Each chapter offers war stories telling a good or bad real life example about the pattern focused on/in the chapter. The war story is followed by a Java EE implementation, example, and explanation. Each code sample given can be run by itself. Finally, each chapter ends with when and how to use the pattern effectively.
WHAT YOU NEED TO USE THIS BOOK
Any modern computer with an operating system that has a Java Virtual Machine (JVM) implementation is sufficient to run the samples given in this book. For ease of coding, you need an integrated development environment (IDE) of your own choice. The sample can run on any popular modern IDEs including Eclipse, NetBeans, and IntelliJ.
You need the Java Development Kit (JDK) for Java EE7 to be able to compile and run the code samples, but some of the code samples would also work on previous Java EE JDKs.
You can use any Java EE7compliant application server to run the samples. We ran all the code samples on Glassfish, which is the reference implementation server, and TomEE, which is the Java EE version of the popular Java web server Tomcat. You can use any server, but because Glassfish is the reference implementation, you might want to try it for the samples.
To run the samples in this book, you need the following:
- An operating system that has a JDK for Java EE7, such as Linux, Mac OS X, or Windows
- Java EE 7 JDK
- An IDE of your choice, such as Eclipse for Java EE Developers, NetBeans, or IntelliJ
- Java EE 7compliant application server such as GlassFish or TomEE
The source code for the samples is available for download from the Wrox website at:
www.wrox.com/go/projavaeedesignpatterns
MOTIVATION FOR WRITING
In November 2011, after having a debate on Java EE versus Spring for a project, I went back to my desk and wrote a blog post titled Java EE 6 and the Ewoks, which became popular pretty quickly. The story was based on the TV show How I Met Your Mother. In this show, Barney, who is the playboy character, introduced a theory that was focused on Ewoks, the teddy bearlike creatures introduced in Episode VI of Star Wars. Fans have mixed feelings on Ewoks.
According to Barney, those born before May 25, 1973, when Return of the Jedi was released, think Ewoks are childish and simply hate them. However, those born after that date find Ewoks cute because they remind them of teddy bears.
Now back to my story. Engaging in a debate with a customer about Java EE versus Spring made me realize that its similar to the Ewok theory. Those who are old enough to have used J2EE 1.4 (EJB 1.0/2.0/2.1) in corporate projects had a slow, unproductive development environment with RAM-eating and buggy IDEs and servers taking several minutes to boot. The architecture was over engineered and probably failed, resulting in a migration to Spring. Those users tended to hate Java EE with a passion, no matter what version they used. The release of Java EE 5 was underrated and did not really impress anyone.
Java EE will never be J2EE again. It is now open, has a large community and reshapes itself by assimilating good ideas from frameworks such as Spring and Hibernate. The first great change was the architecture and style of coding. Enterprise JavaBeans (EJB) followed the lightweight Plain Old Java Object (POJO) model, almost unusable entity beans were replaced with Java Persistence API (JPA), REST and Web Services became standard and integral parts of the run time, and annotations replaced XML configuration. Still, some might argue that Java EE 5 was not ready for the huge shift because it was not as mature as Spring, and the development environment was still not responsive enough. Using Spring on Tomcat instead of EJBs and Java EE 5 on an application server greatly increased the development productivity, but Java EE 5 was still a big step forward towards designing, leveraging, and architecting the Enterprise Java platform from scratch.
Next page