Generic Erase compensation

Source: Internet
Author: User

Because the generic is used, the type parameter is erased and only the original type is preserved. Therefore, when using generics, we cannot use new T (), nor can we use instanceof because these two types of operations need to know the exact type. There are three solutions to this problem (use of design mode): Simple factory (simplest), factory method (most flexible), template method (most simple).
1. Simple Factory
In this method, the type is used as an argument to return an instance of the parameterized type to a universal factory class (which has a generic method that returns an instance of the concrete type Class) using the Newinstance () method of the class as follows:

Pros: Simple, straightforward use of generic methods
Disadvantages:
(1) a single newinstance () method using the class object can only use the default constructor, which limits the creation of objects.
(2) If a specific class does not have a default constructor, an exception is thrown.
2. Factory method
Compared to the simple factory method, the factory method adds the following elements:
(1) Factory interface/abstract interface-specific factory common interface/Type
(2) Specific factory--the specific factory is responsible for the creation of specific products, and it must ensure the successful creation of the product, in the specific factory can call a variety of construction methods, because the specific factory has been the parameterized type materialized.
(3) Plant management class--this class can be responsible for what type of specific plant is created and can be returned to a specific factory instance.
Pros: After adding the above three elements, the following issues are resolved:
1) All the specific factories have a common interface
2) specific plant to ensure the successful creation of specific products: can call arbitrary construction method to generate an instance of a specific class
3) Factory Management: Control over the production of specific plants, allowing the production of specific plants to be flexible.
The factory method is implemented as follows:
1) Abstract Factory

2) Authorfactory: Responsible for the creation of author instances

3) Integerfactory: Responsible for creating an integer instance

3) Factory management class (generic type): Factorymanage

4) Testing the client

Supplement: Specific author class

3. Template method
Generate instances of specific classes in a more streamlined way. The difference with the factory method is that the template class (abstract class) is used to control the process of the entire instantiation (a bit like the factory management class, the template method to control the overall process, and the concrete creation process is implemented by its subclasses), and the concrete class to complete the concrete product construction. But I used the concept of the factory when I was building an instance of the class, but it was only a concrete implementation. The biggest advantage: simple, because directly invoke the specific generated class, and the template class is hidden, transparent to the customer (and the factory method in the management class factory to be explicitly generated).
3.1 Template Classes

3.2 Concrete template class Authorcreator

3.3 Concrete template class: Integercreator

3.4 Client Testing

Generic Erase compensation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.