This is an old revision of the document!
Table of Contents
Adventure Game (Grails)
In this case study, we re-develop part of the adventure game in the Grails web framework. This will demonstrate the productivity gains possible from a good, well-design application development framework, and further emphasizes the power of the Groovy programming language and the so-called lightweight enterprise frameworks. The presentation has been designed as an aide-memoir of the development process that will be demonstrated live in the final lecture of the first week of the module.
The presentation was inspired by case-study presented in Chapter 16 “Seeing the Grails Light” from Groovy in Action and was also influenced by Chapter 11 “Working with Grails” from Groovy Recipes1).
A Short Introduction to Grails
(Reproduced from the Introduction to the Grails user Guide.)
Java web development as it stands today is dramatically more complicated than it needs to be. Most modern web frameworks in the Java space are over complicated and don't embrace the Don't Repeat Yourself (DRY) principals.
Dynamic frameworks like Rails, Django and TurboGears helped pave the way to a more modern way of thinking about web applications. Grails builds on these concepts and dramatically reduces the complexity of building web applications on the Java platform. What makes it different, however, is that it does so by building on already established Java technology like Spring and Hibernate.
Grails is a full stack framework and attempts to solve as many pieces of the web development puzzle through the core technology and it's associated plug-ins. Included out the box are things like:
- An easy to use Object Relational Mapping (ORM) layer built on Hibernate
- An expressive view technology called Groovy Server Pages (GSP)
- A controller layer built on Spring MVC
- A command line scripting environment built on the Groovy-powered Gant
- An embedded Jetty web container which is configured for on the fly reloading
- Dependency injection with the inbuilt Spring container
- Support for internationalization (i18n) built on Spring's core
MessageSource
concept - A transactional service layer built on Spring's transaction abstraction
All of these are made easy to use through the power of the Groovy language and the extensive use of Domain Specific Languages (DSLs).
The Architecture of Grails
Grails is made up of components from the Java Platform, Java Enterprise Edition and “best-of-breed” open source enterprise Java components. It wraps all these up in a Groovy API that makes common activities simple and more complex activities easier than they would be in Java. Furthermore a Grails application can be deployed on any Java Web Container (it comes with Jetty, but a Grails application can be deployed in a web Container such as Tomcat or an Enterprise Application Container such as Glassfish) and against any database that is supported by JDBC and Hibernate (it comes with HSQL).
Figure 1: The Grails Stack
This convenience comes at a cost: Grails has fairly fixed ideas about how a web application should be developed (an idea called Convention over Configuration) and you, as a developer, are forced to do things “the Grails way”. The good news is that these conventions are largely based on best practice and it frees you to concentrate on your business logic. After all, concentrating on business logic and having the enterprise integration “just happen” is the holy grail2) of enterprise application development!
The Development Example
We shall take the example that we have developed over the previous four case studies, and re-implement Player and Item administration for an adventure game in Grails.
Step 2: Create domain class Item
An index to the source code for all the examples in this case study is available.
Step 1: Install and Configure Grails
Iteration I: Specification and Map Implementation
- Add and remove items to/from the location
- Record the picking up and dropping of an item by a player
- Display details of the current location
- Display the items being carried by a given player
- Display the number of players who are carrying a particular item
def banquetingRoom = [ 'bread' : ['Chris', 'John'], 'fork' : ['Chris'], 'sword' : ['John', 'Sally'], 'magic amulet' : ['Sally'], 'apple' : [] ]
def addItem(location, item) { location[item] = [] }
readNumberOfPlayersHoldingItem(location, item) { return location[item].size() }
- 1| A simple adventure game (at-m42/Examples/case-study-02/location1.groovy)
extern> http://www.cpjobling.org.uk/~eechris/at-m42/Examples/case-study-02/location1.groovy
Iteration II: Implementation of a Text-based User Interface
- 1| Text-based user interface (at-m42/Examples/case-study-02/location2.groovy)
extern> http://www.cpjobling.org.uk/~eechris/at-m42/Examples/case-study-02/location2.groovy
Iteration III: Implementation With Closures
- 1| Implementation with closures (at-m42/Examples/case-study-02/location3.groovy)
extern> http://www.cpjobling.org.uk/~eechris/at-m42/Examples/case-study-02/location3.groovy