Java tips, observations, bugs and problems from the world of Spring, Weblogic, Oracle, MySQL and many other technologies...
Showing posts with label Generics. Show all posts
Showing posts with label Generics. Show all posts
Thursday, 2 June 2011
Implementing the Observer Pattern using Composition
When I previously discussed the Observer pattern I recommended following Joshua Bloch’s advice and “favor composition over inheritance” and suggested that the JDK’s implementation of the Observer, Observable, though convenient, was somewhat flawed. This blog demonstrates how to implement the Observer pattern using composition. The benefit of using this technique is that it clearly upholds the single responsibility principle broken in several places by my earlier news server examples.
Labels:
Aggregation,
Generics,
Java,
Observer,
Pattern
Tuesday, 31 May 2011
UML, the Template Class and Java
One problem you often see when dealing with UML is that some developers don’t get the idea that what you see in class diagram should translate directly into code they draw a class diagram and write some code and neither bare any resemblance to each other. This, and other forthcoming blogs in the series, demonstrate the link between diagrams and code.
This blog demonstrates how to draw a template class in UML followed by its equivalent Java code and looking at it you'll notice that the Java code is all about generics. I guess that's because UML's template class idea stems from C++'s template classes, which are similar to, but not a direct equivalent of Java's generics.
This blog demonstrates how to draw a template class in UML followed by its equivalent Java code and looking at it you'll notice that the Java code is all about generics. I guess that's because UML's template class idea stems from C++'s template classes, which are similar to, but not a direct equivalent of Java's generics.
Friday, 6 May 2011
Generic Factory Class - Sample
Previously I’ve talked about generic classes and generic methods. This blog combines the two and demonstrates how to write a generic factory class. Now, as this is a blog, we’re keeping it simple and assuming that the classes we wish to use our factory to create have a default no-arg constructor and are public - you can add extra detail in to deal with constructor args.
Thursday, 5 May 2011
Using Generic Methods
This blog adds to my previous couple of blogs about using generics, by introducing Generic Methods. My other blogs dealt with sample code that parametrise entire classes, but it makes more sense to parametrise a single method rather than a whole class if possible.
Wednesday, 4 May 2011
Using Generic bounded wild-cards in an API
Yesterday’s blog touched on the strange aspect of bounded wild-card generics showing that you can create typed collections into which you can’t add anything except null. The example, covering Fruit classes, begs the question of why you can write ? extends Fruit.
Labels:
Collections,
Generics,
Java
Tuesday, 3 May 2011
Are Generics Overly Complicated?
Generics, introduced in Java 5, are widely used in Java programs today especially in the arena of collection classes. What are not so widely used and little understood by average programmers are generics that use bounded wildcards (and don’t forget that by definition nearly everyone is average).
Labels:
Collections,
Generics,
Java
Subscribe to:
Posts (Atom)