如何使实例一次并多次使用它?

时间:2015-08-30 06:08:56

标签: java

我应该编写一个处理数学运算的应用程序。我有一个界面Operator和四个类PlusMinusMultiplyDivide)。这些接口和类位于不同的文件夹中。我应该在我的主程序中加载和使用它们。我是这样做的:

File operatorFile = new File(operatorPath);
URL operatorFilePath = operatorFile.toURL();          
URL[] operatorFilePaths = new URL[]{operatorFilePath};
ClassLoader operatorsLoader = new URLClassLoader(operatorFilePaths);




Class operatorInterface = operatorsLoader.loadClass("operators.Operator"); 

            FilenameFilter dotClassFilter = new FilenameFilter() 
            { 
                public boolean accept(File dir, String name) 
                { 
                    return name.endsWith(".class"); 
                } 
            };
while(expression != null)
            {
                String[] elementInExpression = expression.split(",");

                if(elementInExpression.length != 3)
                {
                    System.out.println("Expression should have two operand and 1 operator ");
                    throw new ValidationException("Validation on number of element failed1");
                }

                Validation.validateOperand(elementInExpression[0],elementInExpression[1]);
                Validation.validateOperator(elementInExpression[2]);

                double firstNumber = Double.parseDouble(elementInExpression[0]);
                double secondNumber = Double.parseDouble(elementInExpression[1]);

                double output = 0;

                Method methodsInOperator;
                Object instance;
                String operatorSign;

                for(Class operatorCls : operatorClass)
                {
                    instance = operatorCls.newInstance();
                    methodsInOperator = operatorCls.getMethod("getSign", null); 
                    operatorSign = (String)methodsInOperator.invoke(instance, null);
                    if(operatorSign.equals(elementInExpression[2]))
                    {
                        methodsInOperator = operatorCls.getMethod("calculate", new Class[] { double.class, double.class } ); 
                        output =(double)methodsInOperator.invoke(instance, firstNumber, secondNumber);  
                    }
                }

                processingResult.add(output);
                expression = mathExpReader.readLine();
            }

但是在下面的语句中,我为从文件中读取的每一行创建了一个实例,这样就浪费了内存。有没有办法让实例一次并多次使用它?

for(Class operatorCls : operatorClass)
                {
                    instance = operatorCls.newInstance(); //here we waste memory
                    methodsInOperator = operatorCls.getMethod("getSign", null); 
                    operatorSign = (String)methodsInOperator.invoke(instance, null);
                    if(operatorSign.equals(elementInExpression[2]))
                    {
                        methodsInOperator = operatorCls.getMethod("calculate", new Class[] { double.class, double.class } ); 
                        output =(double)methodsInOperator.invoke(instance, firstNumber, secondNumber);  
                    }
                }

1 个答案:

答案 0 :(得分:1)

使用单例模式创建类,然后你必须初始化一次对象,你可以在项目的任何地方使用它。

here是单例模式的教程。