Schweitzer Fachinformationen
Wenn es um professionelles Wissen geht, ist Schweitzer Fachinformationen wegweisend. Kunden aus Recht und Beratung sowie Unternehmen, öffentliche Verwaltungen und Bibliotheken erhalten komplette Lösungen zum Beschaffen, Verwalten und Nutzen von digitalen und gedruckten Medien.
FOREWORD xxiii
INTRODUCTION xxv
PART I: INTRODUCTION TO JAVA EE DESIGN PATTERNS
CHAPTER 1: A BRIEF OVERVIEW OF DESIGN PATTERNS 3
What Is a Design Pattern? 4
How Patterns Were Discovered and Why We Need Them 5
Patterns in the Real World 5
Design Pattern Basics 6
Enterprise Patterns 7
Java to Enterprise Java 7
The Emergence of Enterprise Java Patterns 8
Design Patterns Versus Enterprise Patterns 8
Plain Old Design Patterns Meet Java EE 9
When Patterns Become Anti-Patterns 10
Summary 10
Notes 10
CHAPTER 2: THE BASICS OF JAVA EE 13
Multitier Architecture 14
The Client Tier 15
The Middle Tier 16
Web Layer 16
Business Layer 16
The EIS Tier 18
Java EE Servers 18
The Java EE Web Profile 18
Core Principles of Java EE 19
Convention over Configuration 19
Context and Dependency Injection 20
Interceptors 21
Summary 22
Exercises 22
PART II: IMPLEMENTING DESIGN PATTERNS IN JAVA EE
CHAPTER 3: FAÇADE PATTERN 25
What Is a Façade? 26
Façade Class Diagram 27
Implementing the Façade Pattern in Plain Code 27
Implementing the Façade Pattern in Java EE 29
Façade with Stateless Beans 29
Façade with Stateful Bean 31
Where and When to Use the Façade Pattern 31
Summary 31
Exercises 32
Notes 32
CHAPTER 4: SINGLETON PATTERN 33
What Is a Singleton? 34
Singleton Class Diagram 34
Implementing the Singleton Pattern in Plain Code 35
Implementing the Singleton Pattern in Java EE 38
Singleton Beans 39
Using Singletons at Startup 39
Determining Startup Order 40
Managing Concurrency 42
Where and When to Use the Singleton Pattern 45
Summary 46
Exercises 46
Notes 47
CHAPTER 5: DEPENDENCY INJECTION AND CDI 49
What Is Dependency Injection? 50
Implementing DI in Plain Code 50
Implementing DI in Java EE 53
The @Named Annotation 54
Context and Dependency Injection (CDI) 55
CDI Versus EJB 56
CDI Beans 56
The @Inject Annotation 57
Contexts and Scope 57
Naming and EL 58
CDI Beans for Backing JSF 58
Qualifi ers 59
Alternatives 59
Stereotypes 60
Other Patterns via CDI 60
Summary 61
Exercises 61
Notes 62
CHAPTER 6: FACTORY PATTERN 63
What Is a Factory? 64
Factory Method 64
Implementing the Factory Method in Plain Code 66
Abstract Factory 68
Implementing the Abstract Factory in Plain Code 69
Implementing the Factory Pattern in Java EE 70
Harness the Power of CDI 76
Where and When to Use the Factory Patterns 80
Summary 80
Exercises 81
Notes 81
CHAPTER 7: DECORATOR PATTERN 83
What Is a Decorator? 84
Decorator Class Diagram 85
Implementing the Decorator Pattern in Plain Code 86
Implementing the Decorator Pattern in Java EE 89
Decorators Without XML Confi guration 94
Where and When to Use the Decorator Pattern 94
Summary 95
Exercises 96
Notes 96
CHAPTER 8: ASPECT-ORIENTED PROGRAMMING (INTERCEPTORS) 97
What Is Aspect-Oriented Programming? 98
Implementing AOP in Plain Code 100
Aspects in Java EE, Interceptors 102
Interceptor Life Cycle 105
Default-Level Interceptors 106
Interceptor Order 107
CDI Interceptors 109
Where and When to Use Interceptors 111
Summary 112
Notes 112
CHAPTER 9: ASYNCHRONOUS 113
What Is Asynchronous Programming? 114
Asynchronous Pattern 114
Implementing Asynchronous Pattern in Plain Code 116
Asynchronous Programming in Java EE 118
Asynchronous Beans 118
Asynchronous Servlets 120
Where and When to Use Asynchronous Programming 124
Summary 125
Exercises 125
Notes 126
CHAPTER 10: TIMER SERVICE 127
What Is the Timer Service? 127
Implementing a Timer in Java EE 130
Automatic Timers 130
Programmatic Timers 131
Timer Expression 134
Transactions 136
Summary 137
Exercises 137
Notes 138
CHAPTER 11: OBSERVER PATTERN 139
What Is an Observer? 139
Description 140
Observer Class Diagram 141
Implementing the Observer Pattern in Plain Code 142
Implementing the Observer Pattern in Java EE 144
Where and When to Use the Observer Pattern 149
Summary 150
Exercises 151
Notes 151
CHAPTER 12: DATA ACCESS PATTERN 153
What Is a Data Access Pattern? 154
Data Access Class Diagram 154
Overview of the Data Access Pattern 155
Data Transfer Object Pattern 155
Java Persistence Architecture API and Object Relational Mapping 156
Implementing the Data Access Pattern in Java EE 157
Type-Safe DAO Implementation 162
Where and When to Use the Data Access Pattern 163
Summary 163
Exercises 163
Notes 163
CHAPTER 13: RESTFUL WEB SERVICES 165
What Is REST? 166
The Six Constraints of REST 167
Client-Server 167
Uniform Interface 167
Stateless 168
Cacheable 168
Layered System 168
Code on Demand 168
Richardson Maturity Model of REST API 168
Level 0: The Swamp of POX (Plain Old XML) 169
Level 1: Resources 169
Level 2: HTTP Verbs 169
Level 3: Hypermedia Controls 169
Designing a RESTful API 169
Resource Naming 170
Nouns Not Verbs 170
Self-Descriptive 170
Plural Not Singular 171
HTTP Methods 171
Get 171
Post 171
Put 172
Delete 172
Rest in Action 172
The users noun 172
The topics noun and the posts noun 173
Implementing REST in Java EE 175
HATEOAS 178
Where and When to Use REST 180
Summary 181
Exercises 181
Notes 182
CHAPTER 14: MODEL VIEW CONTROLLER PATTERN 183
What Is the MVC Design Pattern? 184
MVC Types 185
Implementing the MVC Pattern in Plain Code 186
Implementing the MVC Pattern in Java EE 190
The FacesServlet 190
MVC Using the FacesServlet 190
Where and When to Use the MVC Pattern 193
Summary 193
Exercises 193
Note 193
CHAPTER 15: OTHER PATTERNS IN JAVA EE 195
What Are WebSockets? 195
What Is Message-Orientated Middleware 198
What Is the Microservice Architecture? 199
Monolithic Architecture 199
Scalability 200
Decomposing into Services 201
Microservice Benefi ts 202
Nothing in Life Is Free 203
Conclusions 204
Finally, Some Anti-Patterns 204
Uber Class 204
Lasagna Architecture 204
Mr. Colombus 205
Friends with Benefi ts 205
Bleeding Edge 205
Utilityman 206
Notes 206
PART III: SUMMARY
CHAPTER 16: DESIGN PATTERNS: THE GOOD,THE BAD, AND THE UGLY 209
The Good: Patterns for Success 209
The Bad: Over and Misuse of Patterns 211
...and The Ugly 212
Summary 214
Notes 214
INDEX 215
THIS BOOK DISCUSSES THE CLASSIC DESIGN PATTERNS that were first mentioned in the famous book by the GoF1 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.
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.
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.
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.
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 EE7-compliant 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:
The source code for the samples is available for download from the Wrox website at:
www.wrox.com/go/projavaeedesignpatterns
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,"2 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 bear-like 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 it's 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.
This shift was followed by Java EE 6 and 7, which used the same principles and ideas as Java EE 5. Java EE is a great choice for development, but the debate was not over, thanks to the Ewok theory.
It was a hot August day when I first got a call from Wrox/Wiley about whether I would be interested in writing a Spring book. I was experienced and confident with implementing and developing in Spring, but there were already tons of books written about it, which made it hard to see the value in writing a new one.
Besides, I was using Java EE more than ever since version 6 had been released. Considering the Spring versus Java EE debates, my blog posts, and the Ewoks, I felt like writing about Java EE. However, just like Spring, there were many great Java EE books that I admired. I always had the feeling that some properties of Java EE were underrated. Java EE has great built-in implementations of design patterns with simple use of annotations.
The classic patterns listed in the GoF book were used extensively in almost all languages, frameworks, and platforms. J2EE was no exception and neither was Java EE. Actually Java EE took a bold step in providing default implementations for many of those patterns, but still even most of the experienced developers underestimated the value of those out of the box implementations.
I had been blogging about those patterns for almost a year, so I decided to present a counteroffer to write a book on the "classic" design patterns in Java EE. As you are reading this book now, you may guess the feedback was positive.
This book fills the gap between the Java EE platform with the classic design patterns from the GoF book as well as talking about new patterns. This way we did not write just another Java EE book but a catalogue for design patterns in Java EE.
I started blogging, writing and giving talks on design patterns in Java EE to extend my knowledge and experience on a platform I really believed in, so the best thing about writing this book for me was that I had the chance to write about something I was really passionate about. Although my blog had simpler examples, I was already using it as a reference when I needed, so writing a book, which is more formally and properly formatted while still following the same idea was a great opportunity.
Every chapter that my coauthor Alex and I wrote had the same goal: Write content that we would like to read ourselves. The result is a book that we both want to keep as a reference.
We hope that you enjoy reading this book as much as we enjoyed writing it.
To help you get the most from the text and keep track of what's happening, we've used a number of conventions throughout the book.
NOTE Notes indicates notes, tips, hints, tricks, or asides to the current discussion.
As for styles in the text:
persistence.properties
We use a monofont type with no highlighting for most code examples.
We use bold to emphasize code that is particularly important in the present context or to show changes from a previous code snippet.
As you work through the examples in this book, you may choose either to type in all the code manually or to use the source code files that accompany the book. All the source code...
Dateiformat: ePUBKopierschutz: Adobe-DRM (Digital Rights Management)
Systemvoraussetzungen:
Das Dateiformat ePUB ist sehr gut für Romane und Sachbücher geeignet – also für „fließenden” Text ohne komplexes Layout. Bei E-Readern oder Smartphones passt sich der Zeilen- und Seitenumbruch automatisch den kleinen Displays an. Mit Adobe-DRM wird hier ein „harter” Kopierschutz verwendet. Wenn die notwendigen Voraussetzungen nicht vorliegen, können Sie das E-Book leider nicht öffnen. Daher müssen Sie bereits vor dem Download Ihre Lese-Hardware vorbereiten.Bitte beachten Sie: Wir empfehlen Ihnen unbedingt nach Installation der Lese-Software diese mit Ihrer persönlichen Adobe-ID zu autorisieren!
Weitere Informationen finden Sie in unserer E-Book Hilfe.