lundi 15 avril 2019

In Java, How to implement the functions similar to using the template parameter to avoid creating Creator subclasses in C++?

Recently, when I was reading the book named "Design Pattern-Elements of Reusable Object-Oriented Software", there was such a paragraph in the book:

Using templates to avoid subclassing. As we've mentioned, another potential problem with factory methods is that they might force you to subclass just to create the appropriate Product objects. Another way to get around this in C++ is to provide a template subclass of Creator that's parameterized by the Product class.

what I want to know is: In Java, How to implement the functions similar to using the template parameter to avoid creating Creator subclasses in C++?

I have tried to use generics, but I don't know how to use it.

public class ConcreteCreator<T extends Product> extends Creator{

    @Override
    public Product createProduct() {
        // ... how can I return new T();

    }
}


Aucun commentaire:

Enregistrer un commentaire