Saturday 21 July 2012

The real way to get information about Java Generics

An other bit of plagarism I am afraid but again on a task that I had searche for long and hard.
This is related to the problem of using Java reflection to examine generics. There are may articles  that will tell you is just can't be done at runtime and that the information has disappeared.

Not so!

The following code will show that you can do it:


    class TargetClass
    {
    public List<String> getData() { return null; }
    }
   
    Method method = TargetClass.class.getMethod("getData", null);

    Type returnType = method.getGenericReturnType();

    if(returnType instanceof ParameterizedType){
        ParameterizedType type = (ParameterizedType) returnType;
       
        Type rawType = type.getRawType() ;
        Class rawTypeClass = (Class) rawType ;
       
        Type[] typeArguments = type.getActualTypeArguments();
       
        System.out.println("returnType = " + rawTypeClass );

        if ( List.class.isAssignableFrom(rawTypeClass)  ) System.out.println("Is a list");
       
        for(Type typeArgument : typeArguments){
            Class typeArgClass = (Class) typeArgument;
            System.out.println("typeArgClass = " + typeArgClass);
        }

this prints out:

returnType = interface java.util.List
Is a list
typeArgClass = class java.lang.String

Which is correct!

The article what gets the credit for this is here: Java Reflection: Generics by Jakob Jenkov.

Friday 20 July 2012

"Gang of Four" design paterns

There are times when you find something on the web and you go are that's useful! There are several reasons for this revelation, in this case it is a reminder that there are good ways and bad ways to write your code.

This article is one of those salutary reminders that we can all do better. So I have posted a link to the here with a short synopsis so I can find it when I need to be reminded.


Design patterns provide solutions to common software design problems. In the case of object-oriented programming, design patterns are generally aimed at solving the problems of object generation and interaction, rather than the larger scale problems of overall software architecture. They give generalised solutions in the form of templates that may be applied to real-world problems.
Design patterns are a powerful tool for software developers. However, they should not be seen as prescriptive specifications for software. It is more important to understand the concepts that design patterns describe, rather than memorising their exact classes, methods and properties. It is also important to apply patterns appropriately. Using the incorrect pattern for a situation or applying a design pattern to a trivial solution can overcomplicate your code and lead to maintainability issues.

The Gang of Four are the authors of the book, "Design Patterns: Elements of Reusable Object-Oriented Software". This important book describes various development techniques and pitfalls in addition to providing twenty-three object-oriented programming design patterns. The four authors were Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides.

The full article by , is published at http://www.blackwasp.co.uk/GofPatterns.aspx but the types of patterns he describes are listed here:

Creational Patterns

The first type of design pattern is the creational pattern. Creational patterns provide ways to instantiate single objects or groups of related objects. There are five such patterns:
  • Abstract Factory. The abstract factory pattern is used to provide a client with a set of related or dependant objects. The "family" of objects created by the factory are determined at run-time.
  • Builder. The builder pattern is used to create complex objects with constituent parts that must be created in the same order or using a specific algorithm. An external class controls the construction algorithm.
  • Factory Method. The factory pattern is used to replace class constructors, abstracting the process of object generation so that the type of the object instantiated can be determined at run-time.
  • Prototype. The prototype pattern is used to instantiate a new object by copying all of the properties of an existing object, creating an independent clone. This practise is particularly useful when the construction of a new object is inefficient.
  • Singleton. The singleton pattern ensures that only one object of a particular class is ever created. All further references to objects of the singleton class refer to the same underlying instance.

Structural Patterns

The second type of design pattern is the structural pattern. Structural patterns provide a manner to define relationships between classes or objects.
  • Adapter. The adapter pattern is used to provide a link between two otherwise incompatible types by wrapping the "adaptee" with a class that supports the interface required by the client.
  • Bridge. The bridge pattern is used to separate the abstract elements of a class from the implementation details, providing the means to replace the implementation details without modifying the abstraction.
  • Composite. The composite pattern is used to create hierarchical, recursive tree structures of related objects where any element of the structure may be accessed and utilised in a standard manner.
  • Decorator. The decorator pattern is used to extend or alter the functionality of objects at run-time by wrapping them in an object of a decorator class. This provides a flexible alternative to using inheritance to modify behaviour.
  • Facade. The facade pattern is used to define a simplified interface to a more complex subsystem.
  • Flyweight. The flyweight pattern is used to reduce the memory and resource usage for complex models containing many hundreds, thousands or hundreds of thousands of similar objects.
  • Proxy. The proxy pattern is used to provide a surrogate or placeholder object, which references an underlying object. The proxy provides the same public interface as the underlying subject class, adding a level of indirection by accepting requests from a client object and passing these to the real subject object as necessary.