在 Java 中实现命令模式的问题

发布于 2024-12-15 05:28:27 字数 6153 浏览 5 评论 0原文

我试图在java中简单地实现命令模式。但是,我收到以下错误:

java.lang.ClassNotFoundException: AddCommand
Exception in thread "main" java.lang.NullPointerException
at com.programming.sample.TransactionCommand.execute(TestTransactionCommand.java:64)
at com.programming.sample.CommandManager.runCommands(TestTransactionCommand.java:33)
at com.programming.sample.TestTransactionCommand.main(TestTransactionCommand.java:124)

代码:

            package com.programming.sample;

            //TestTransactionCommand.java
            import java.util.*;
            final class CommandReceiver {
              private int[] c;
              private CommandArgument a;
                 private CommandReceiver(){
                   c = new int[2];
                 }
                 private static CommandReceiver cr = new CommandReceiver();
                 public static CommandReceiver getHandle() {
                 return cr;
                 }
                 public void setCommandArgument(CommandArgument a) {
                 this.a = a;
                 }
                 public void methAdd() {
                 c = a.getArguments();
                     System.out.println("The result is " + (c[0]+c[1]));
                 }
                 public void methSubtract() {
                 c = a.getArguments();
                     System.out.println("The result is " + (c[0]-c[1]));
                 }
            }
             class CommandManager {
               private Command myCommand;
               public CommandManager(Command  myCommand) {
                 this.myCommand  =  myCommand ;    
               }
               public void runCommands( ) {
                        myCommand.execute();     
               }
             }
            class TransactionCommand implements Command {
              private CommandReceiver commandreceiver;
              private Vector commandnamelist,commandargumentlist; 
              private String commandname;
              private CommandArgument commandargument;
              private Command command;
              public TransactionCommand () {
                this(null,null);
              }
              public TransactionCommand ( Vector  commandnamelist, Vector
            commandargumentlist){
                this.commandnamelist = commandnamelist;
                this.commandargumentlist = commandargumentlist;
                commandreceiver =  CommandReceiver.getHandle();  
              }
              public void execute( ) {
                for (int i = 0; i < commandnamelist.size(); i++) {
                  commandname = (String)(commandnamelist.get(i));
                  commandargument = (CommandArgument)((commandargumentlist.get(i)));
                  commandreceiver.setCommandArgument(commandargument);
                  String classname = commandname + "Command";
                     try {
                       Class cls = Class.forName(classname);
                       command = (Command) cls.newInstance();
                     }
                     catch (Throwable e) {   
                              System.err.println(e);
                     }
                  command.execute();
                } 
              }
            }
             class AddCommand extends TransactionCommand {
               private CommandReceiver cr;
               public AddCommand () {
                  cr = CommandReceiver.getHandle();  
               }  
               public void execute( ) {  
                 cr.methAdd();  
               }   
             }
             class SubtractCommand extends TransactionCommand {
               private CommandReceiver cr;
               public SubtractCommand () {
                  cr = CommandReceiver.getHandle();  
               }
               public void execute( ) {
                 cr.methSubtract();
               }   
             }
             class CommandArgument {
               private int[] args;
               CommandArgument() {
                 args = new int[2];
               }
               public int[] getArguments() {
                return args;
               }
               public void setArgument(int i1, int i2) {
                     args[0] = i1; args[1] = i2;
               }
             }
             public class TestTransactionCommand {
               private  Vector clist,alist; 
               public TestTransactionCommand() {
                 clist = new Vector(); 
                   alist = new Vector();
               }
               public void clearBuffer(Vector c, Vector a) {
                 clist.removeAll(c);
                   alist.removeAll(a); 
               }
               public Vector getClist() {
                 return clist;
               }
               public Vector getAlist() {
                 return alist;
               }
                public static void main(String[] args) {

                 CommandArgument ca,ca2;
                 TestTransactionCommand t = new TestTransactionCommand();
                 ca = new CommandArgument();
                 ca.setArgument(2,8);
                 Vector myclist = t.getClist();
                 Vector myalist = t.getAlist();
                 myclist.addElement("Add"); 
                 myalist.addElement(ca);
                 TransactionCommand tc = new TransactionCommand(myclist,myalist);
                 CommandManager cm = new CommandManager(tc);       
                 cm.runCommands();

                 t.clearBuffer(myclist,myalist);
                 ca2 = new CommandArgument();
                 ca2.setArgument(5,7);
                 myclist = t.getClist();
                 myalist = t.getAlist();
                 myclist.addElement("Subtract"); 
                 myalist.addElement(ca2);
                 myclist.addElement("Add"); 
                 myalist.addElement(ca2);
                 TransactionCommand tc2 = new TransactionCommand(myclist,myalist);        
                 CommandManager cm2 = new CommandManager(tc2);       
                 cm2.runCommands();
               }
             } 

I was trying to do a simple implementation of the command patter in java. However, I am getting the following error:

java.lang.ClassNotFoundException: AddCommand
Exception in thread "main" java.lang.NullPointerException
at com.programming.sample.TransactionCommand.execute(TestTransactionCommand.java:64)
at com.programming.sample.CommandManager.runCommands(TestTransactionCommand.java:33)
at com.programming.sample.TestTransactionCommand.main(TestTransactionCommand.java:124)

Code:

            package com.programming.sample;

            //TestTransactionCommand.java
            import java.util.*;
            final class CommandReceiver {
              private int[] c;
              private CommandArgument a;
                 private CommandReceiver(){
                   c = new int[2];
                 }
                 private static CommandReceiver cr = new CommandReceiver();
                 public static CommandReceiver getHandle() {
                 return cr;
                 }
                 public void setCommandArgument(CommandArgument a) {
                 this.a = a;
                 }
                 public void methAdd() {
                 c = a.getArguments();
                     System.out.println("The result is " + (c[0]+c[1]));
                 }
                 public void methSubtract() {
                 c = a.getArguments();
                     System.out.println("The result is " + (c[0]-c[1]));
                 }
            }
             class CommandManager {
               private Command myCommand;
               public CommandManager(Command  myCommand) {
                 this.myCommand  =  myCommand ;    
               }
               public void runCommands( ) {
                        myCommand.execute();     
               }
             }
            class TransactionCommand implements Command {
              private CommandReceiver commandreceiver;
              private Vector commandnamelist,commandargumentlist; 
              private String commandname;
              private CommandArgument commandargument;
              private Command command;
              public TransactionCommand () {
                this(null,null);
              }
              public TransactionCommand ( Vector  commandnamelist, Vector
            commandargumentlist){
                this.commandnamelist = commandnamelist;
                this.commandargumentlist = commandargumentlist;
                commandreceiver =  CommandReceiver.getHandle();  
              }
              public void execute( ) {
                for (int i = 0; i < commandnamelist.size(); i++) {
                  commandname = (String)(commandnamelist.get(i));
                  commandargument = (CommandArgument)((commandargumentlist.get(i)));
                  commandreceiver.setCommandArgument(commandargument);
                  String classname = commandname + "Command";
                     try {
                       Class cls = Class.forName(classname);
                       command = (Command) cls.newInstance();
                     }
                     catch (Throwable e) {   
                              System.err.println(e);
                     }
                  command.execute();
                } 
              }
            }
             class AddCommand extends TransactionCommand {
               private CommandReceiver cr;
               public AddCommand () {
                  cr = CommandReceiver.getHandle();  
               }  
               public void execute( ) {  
                 cr.methAdd();  
               }   
             }
             class SubtractCommand extends TransactionCommand {
               private CommandReceiver cr;
               public SubtractCommand () {
                  cr = CommandReceiver.getHandle();  
               }
               public void execute( ) {
                 cr.methSubtract();
               }   
             }
             class CommandArgument {
               private int[] args;
               CommandArgument() {
                 args = new int[2];
               }
               public int[] getArguments() {
                return args;
               }
               public void setArgument(int i1, int i2) {
                     args[0] = i1; args[1] = i2;
               }
             }
             public class TestTransactionCommand {
               private  Vector clist,alist; 
               public TestTransactionCommand() {
                 clist = new Vector(); 
                   alist = new Vector();
               }
               public void clearBuffer(Vector c, Vector a) {
                 clist.removeAll(c);
                   alist.removeAll(a); 
               }
               public Vector getClist() {
                 return clist;
               }
               public Vector getAlist() {
                 return alist;
               }
                public static void main(String[] args) {

                 CommandArgument ca,ca2;
                 TestTransactionCommand t = new TestTransactionCommand();
                 ca = new CommandArgument();
                 ca.setArgument(2,8);
                 Vector myclist = t.getClist();
                 Vector myalist = t.getAlist();
                 myclist.addElement("Add"); 
                 myalist.addElement(ca);
                 TransactionCommand tc = new TransactionCommand(myclist,myalist);
                 CommandManager cm = new CommandManager(tc);       
                 cm.runCommands();

                 t.clearBuffer(myclist,myalist);
                 ca2 = new CommandArgument();
                 ca2.setArgument(5,7);
                 myclist = t.getClist();
                 myalist = t.getAlist();
                 myclist.addElement("Subtract"); 
                 myalist.addElement(ca2);
                 myclist.addElement("Add"); 
                 myalist.addElement(ca2);
                 TransactionCommand tc2 = new TransactionCommand(myclist,myalist);        
                 CommandManager cm2 = new CommandManager(tc2);       
                 cm2.runCommands();
               }
             } 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

老子叫无熙 2024-12-22 05:28:27

我想,这就是它失败的地方。

try {
    Class cls = Class.forName(classname);
    command = (Command) cls.newInstance();
}
catch (Throwable e) {   
    System.err.println(e);
}
command.execute();

显然,在类路径中找不到类 AddCommand,因此它导致了 ClassNotFoundException,您只需使用 System.err.println(e) 忽略它;,然后尝试调用 command.execute();。对象command未初始化为实例。

也许,提供完整的类名(例如 com.programming.sample.AddCommand)可以解决此问题。

I guess, this is where it's failing.

try {
    Class cls = Class.forName(classname);
    command = (Command) cls.newInstance();
}
catch (Throwable e) {   
    System.err.println(e);
}
command.execute();

Obviously, the class AddCommand wasn't found in the classpath, so it resulted in the ClassNotFoundException, which you ignored with just System.err.println(e); and then trying to invoke command.execute();. The object command isn't initialized to an instance.

Perhaps, providing the full class name, like, com.programming.sample.AddCommand, would fix it.

谜兔 2024-12-22 05:28:27

创建类名时,字符串仅包含 AddCommand。该类的实际名称是com.programming.sample.AddCommand

When you create the classname, the string contains only AddCommand. The class' actual name is com.programming.sample.AddCommand.

淡写薰衣草的香 2024-12-22 05:28:27

我无法编译你的代码 - 没有命令界面。
简单来说,

interface Command {
    public void execute();
}

您的代码可以很好地编译,只是带有警告:

Note: D:\dev\puzzles\TestTransactionCommand.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

I can't compile your code - there is no Command interface.
With simple

interface Command {
    public void execute();
}

your code compiles fine just with warning:

Note: D:\dev\puzzles\TestTransactionCommand.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文