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.