Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is letting a class pass itself as a parameter to a generic base class evil?

I first saw a colleague do this when he implemented object pools. He passed the class that was going to be pooled as a parameter to a generic base class. This base class layed out the pooling code.

The odd thing is that the base class will know of its children. This is considered bad practice in every normal case. But in this case the parent is just a technical solution to avoid writing repetetive code. The base class is never referenced by any other code.

One drawback with this construction is that it "burns the base class". You cannot introduce the generic base class in the middle of a hierarchy. This problem might be outside the topic.

Below is a thinkable example:

public abstract class Singleton<T> where T : class
{
    public static T Instance { get; private set; }

    public Singleton()
    {
        if (Instance != null)
            throw new Exception("Singleton instance already created.");
        Instance = (T) (object) this;
    }
}

public class MyClass : Singleton<MyClass>
{
}

Improved code:

public abstract class Singleton<T> where T : Singleton<T>
{
    public static T Instance { get; private set; }

    public Singleton()
    {
        if (Instance != null)
            throw new Exception("Singleton instance already created.");
        Instance = (T) this;
    }
}

public class MyClass : Singleton<MyClass>
{
}
like image 208
Johan Nilsson Avatar asked Oct 08 '13 11:10

Johan Nilsson


People also ask

How many types of parameters can a generic class have?

The type parameter section of a generic class can have one or more type parameters separated by commas. We can write a single generic method declaration that can be called with arguments of different types. Based on the types of arguments passed to the generic method, the compiler handles each method call appropriately.

How can we pass a class to a generic type?

That is, how can we pass a Class-- a type token -- and get a generic collection of that type? The answer of course is that since Classcan't capture generic type (since it's nonreifiable), we must use something like super type token.

What is the declaration of a generic class?

The declaration of a generic class is almost the same as that of a non-generic class except the class name is followed by a type parameter section. The type parameter section of a generic class can have one or more type parameters separated by commas.

What is generic class in Java?

Generic Class in Java Last Updated : 24 Oct, 2020 Java Generics was introduced to deal with type-safe objects. It makes the code stable.Java Generics methods and classes, enables programmer with a single method declaration, a set of related methods, a set of related types.


1 Answers

No; this is a well-known pattern called the CRTP.
It is especially useful in C++ as an alternative to virtual methods.

You can see it inside the .Net framework in IComparable<T> and IEquatable<T>.

For added robustness, you should add where T : Singleton<T>

like image 103
SLaks Avatar answered Sep 29 '22 07:09

SLaks