Java generic warning

Source: Internet
Author: User
N has not been here for a long time. I did not expect to change the background interface...
Don't talk nonsense. Go to the topic!

System Framework: A "command" tree, a "data" tree, and the root of the command treeThat is, the top element is an abstract class or interface that provides an external execute method, but the return type of this method is different because of different commands.Here are two examples for selecting a course.
Command 1: The execute () method of the GetMyCourseCommand command in the List of courses selected by the student should return a List.
Command 2: whether the execute () method of the CourseIsSelectedCommand command of a course is selected to return a Boolean.
In this case, my interface will be defined as: public interface Comamnd <T> ...{
Public T execute ();
}

Because I don't want the client to know which Command I actually called, I wrote a factory to produce the Command. Because it is a factory, the getCommand () method will be set to static, which is very simple, I cannot create a Factory object every time I want to get a Command.
In this way, the client needs to create different Data objects as needed, and pass the data object to the factory method. The getCommand () the complete definition of is public static Command getCommand (Data d );

So far, what the client should do when processing the above example:
Example 1: Data d = new GetMyCourseData (params ...);
Command <List> cmd = CommandFactory. getCommand (d );
List <Course> list = cmd.exe cute ();

Example 2: Data d = new CourseIsSelectedData (params ...);
Command <Boolean> cmd = CommandFactory. getCommand (d );
Boolean isSelected = cmd.exe cute ();

The client seems quite clear, but the generic Java prompts a warning, the code that produces the Command object in the middle line.

I tried N multiple methods. Except for a new factory object and changed getCommand to a member method, this warning cannot be cleared. Of course, I'm not sure if this approach is overly-designed.

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.