Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic Method Pattern In Java

I am very new to Java generics and have spent an exorbitant amount of time looking for an appropriate solution (if any).

I'm trying to design adapters that handle objects of a specific type of class. As described below, the CAdapter class handles "CClass" objects only. I am trying to provide an abstract generic adapter that handles the bulk of the work (much like Java collections such as LinkedList). I then provide a concrete adapter implementation for each type that needs to be supported.

// Classes

public interface AInterface {
  public String toString();
}

public class BClass extends AInterface  {
  public String toString() { return "BClass "; }
}

public class CClass extends AInterface  {
  public String toString() { return "CClass"; }
}

// Adapters

public interface AdapterInterface<T extends AInterface> {
  public T getInterface();
}

public class BAdapter implements AdapterInterface<BClass> {
  private BClass aInterface = null;
  public BClass getInterface() { return aInterface; }
}

public class CAdapter implements AdapterInterface<CClass> {
  private CClass aInterface = null;
  public CClass getInterface() { return aInterface; }
}

Firstly, I have read that providing a CONCRETE implementation for such a generic adapter is frowned upon (something about God killing a kitten)! Maybe somebody could expand on this?

Secondly, I have run into an issue with dynamically instantiating an adapter and not having the Java compiler complain. For example, I have the method:

public <T extends AInterface> AdapterInterface<T> getAdapter(String type) {
  AdapterInterface<T> result = null;
  if (type.equals("C") {
    result = new CAdapter();
  }
  return result;
}

Of course, the compiler will complain about CAdapter not matching . Given any type of AInterface object I would like to be able to load the right adapter and process it appropriately. I am failing to understand the factory pattern in order to accomplish this.

Any thoughts would be greatly appreciated.

like image 560
Michael Cronk Avatar asked Feb 02 '12 00:02

Michael Cronk


People also ask

What are generic methods in Java?

Generic methods are methods that introduce their own type parameters. This is similar to declaring a generic type, but the type parameter's scope is limited to the method where it is declared. Static and non-static generic methods are allowed, as well as generic class constructors.

What is generic in Java with example?

Generics add that type of safety feature. We will discuss that type of safety feature in later examples. Generics in Java are similar to templates in C++. For example, classes like HashSet, ArrayList, HashMap, etc., use generics very well.

Is generics a design pattern?

In software engineering world, Factory Design Pattern is used to encapsulate object creation. Generics which is another useful concept, converts runtime typecast errors to compile-time errors. Besides these benefits, Factory Design Pattern and Generics provide a simple approach to manage software problems.


2 Answers

My answer is a bit superfluous, but:

Anything with a <T> means "my caller knows what this type is, but I don't". So

AdapterInterface<T> result = null;

Means "I don't actually know what type result is, it's whatever my caller thinks it is". The compiler complains about this:

result = new CAdapter();

Because the this code can't assume that T is CClass.

In fact, there is no way to do this without a cast (declaring the method wild carded just means that you need to cast the result where you call it). The cast is your way of telling the compiler "I know that you have no way of knowing what this is, that's ok: I do. Trust me. Chill.". Yes, you'll get a warning. And that's ok.

Generics don't eliminate all casting, but they allow you to do it just once. Instead of needing casts all over the place, you need them at just that one spot where you - the coder - know for sure that you are playing games with types. All the rest of the code, the suff that uses the adapter you have just created, can safely work with the generic type.

like image 67
PaulMurrayCbr Avatar answered Oct 07 '22 00:10

PaulMurrayCbr


Don't use generics here, covariant (or contravariant, can never remember which is which) return types seem to do what you want:

interface AdapterInterface {
    public AInterface getInterface();
}

class BAdapter implements AdapterInterface {
    private BClass aInterface = null;

    public BClass getInterface() {
        return aInterface;
    }
}

class CAdapter implements AdapterInterface {
    private CClass aInterface = null;

    public CClass getInterface() {
        return aInterface;
    }
}

public AdapterInterface getAdapter(String type) {
    AdapterInterface result = null;
    if (type.equals("C")) {
        result = new CAdapter();
    }
    return result;
}

Unless there's some other methods in the interfaces you didn't mention. The following also compiles with the classes being generic:

public
AdapterInterface<? extends AInterface> getAdapter(String type) {
    if (type.equals("C")) {
        return new CAdapter();
    } else {
        // …
    }
}

The reason your original method won't compile is that T is some specific unknown type that extends AInterface; it doesn't mean "any type that extends from AInterface". It isn't possible to statically prove that the adapter you return is an adapter for the type the caller wanted.

like image 28
millimoose Avatar answered Oct 07 '22 01:10

millimoose