Diagnosing Java code: Easy to master Java generics, part 4th

Source: Internet
Author: User

So, in this miniature series of generic types in this discussion of JSR-14 and Tiger, we've explored:

Generic types and the upcoming features that are designed to support them

Basic types, constrained generics, and constraints on polymorphic methods

Several restrictions imposed on these Java extensions

How the implementation strategies used by the compilers of these extended languages make these restrictions necessary

The impact of adding support for the new operation of the "naked" type parameter in a generic type

This month, we will explore the issues that need to be addressed before you can handle mixin, which might be expected to be the most powerful feature in a generic type, to end the discussion of generic types in the Java language.

Mixin vs Packaging

Mixin is a class that is parameterized by its parent class. For example, consider the following generic class, which inherits its own type parameters:

Class Scrollable<t> extends T {...}

The purpose of class scrollable is to embed the functionality necessary to add scrollable to the GUI widget. Each application of this generic class inherits a different parent class. For example, Scrollable<jtextpane> is a subclass of Jtextpane, and Scrollable<jeditorpane> is a subclass of JEditorPane. Comparing this method of embedding functionality with the existing functionality in the Java Swing Library, in this library, if we want to make jcomponent scrollable, we must "wrap" it in JScrollPane.

Packaging requires not only the forwarding method to access the functionality of the wrapped class, but it also prevents us from using the resulting scrollable object in the context of the instance that needs to be wrapped (for example, we cannot pass JScrollPane to a method that requires a Jtextpane instance). By scrollable the parent class, we can maintain a single point of control over the functionality involved in scrolling when we inherit multiple superclass classes. The ability to use mixin allows us to regain some of the powerful features of multiple inheritance without accompanying exceptions.

In the above example, we can even impose constraints on the type parameter to prevent it from being used in inappropriate contexts. For example, we might want to force the type parameter to be a subclass of JComponent:

Class Scrollable<t extends jcomponent> extends T {...}

Then our mixin can only inherit the GUI component.

Mixin and Generic classes: perfect combination

Typically, Mixin is added to a language as a standalone language feature, as in Jam. But merging Mixin as part of a generic type system is fascinating and almost irresistible. The reason: both mixin and generic classes can be considered as functions that map an existing class to a new class.

Generic classes can be considered to map their parameters to a newly instantiated function. Mixin can be considered as a function to map an existing class to a new subclass. By combining mixin with generic types, we can solve many of the key limitations of other mixin formulas.

In the Jam extension of the Java language, the superclass type of mixin has no name; we cannot reference it in the mixin body. This restriction will quickly lead to a series of other problems. For example, in Jam, the programmer is prevented from passing this as a parameter to the method, which cannot be checked for type. This limitation has a significant impact because many of the most common design patterns depend on being able to pass this as a parameter.

Consider the visitor pattern, where you define the audience class for each class in the composite hierarchy with the For method. The class that is typically accessed contains the Accept method, which takes a visitor and passes this to invoke the accessor's method. Therefore, in Jam, the visitor pattern cannot be used with mixin.

By explicitly expressing mixin as a generic class, we always have a handle to the parent class, which is the type parameter inherited by the class. For example, we can refer to the scrollable parent class as type T. As a result, there is no fundamental difficulty in allowing this to be passed as a type parameter.

However, there are some other obvious difficulties in explicitly articulating mixin as generic types. To give you an initial understanding of some of the difficulties that may arise, we will discuss a few of the outstanding difficulties and some possible solutions.

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.