2012-11-10 167 views
0

嗨,大家我试图实现战略模式,但我不能在具体的类中设置金额,我的意思是金额与助手类中具有关系的金额相同与界面。我尝试着用构造函数和setter以及getter方法设置这个值,但是如果你可以看一看并给出一些反馈,它就会变成graet.Here就是代码。策略设计模式

public interface InvoicingAlgorithm 
{ 
    public void getInvoice(String name, double amount); 
} 


public class AmericanInvoice implements InvoicingAlgorithm 
{ 



    AmericanInvoice() 
    { 

    } 
    //Uk: america 1 : 1.57 
    @Override 
    public void getInvoice(String name, double amount) 
    { 
     Customer customer = new Customer(name , amount * 1.57); 
     customer.setAmount(amount * 1.57); 
     customer.getInvoice(); 
    } 

} 

public class Customer 
{ 

    /** 
    * @param name represent the name of the Customer 
    */ 
    private String name; 

    /** 
    * @param amount represent the amount of money 
    */ 
    private double amount; 

    /** 
    * @param i represents object of InvoicingAlgorithm 
    */ 
    private InvoicingAlgorithm i; 

    Customer(String name, double amount) 
    { 
     this.name = name; 
     this.amount = amount; 

    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public double getAmount() { 
     return amount; 
    } 

    public void setAmount(double amount) { 
     this.amount = amount; 
    } 

    public InvoicingAlgorithm getI() { 
     return i; 
    } 


    public void setInvoicingAlgorithm(InvoicingAlgorithm i) 
    { 
     this.i = i; 
    } 

    public String getInvoice() 
    { 
     DecimalFormat df = new DecimalFormat("#.00"); 
     String total = "--------------------------------------TO: " 
       + name + "FROM: Easyflap (UK) AMOUNT" + ":$" + 
       df.format(amount) 
       + "--------------------------------------"; 

     return total; 
    } 
} 

所以,当我测试它返回的值--------------------------------- ----- TO:OracleFROM:Easyflap(英国)金额:$ 500.00 ---------------------------------- ----当我尝试修改AmericanInvoice中的金额时,它来自Customer类中的getInvoice方法,它不起作用。

测试类的AmericanInvoice

public class AmericanInvoiceTest { 

    /** 
    * Test of getInvoice method, of class AmericanInvoice. 
    */ 
    @Test 
    public void testGetInvoice() { 
     System.out.println("Producing American invoice"); 
     final int invoiceAmount = 500; 
     final Customer c = new Customer("Oracle", invoiceAmount); 
     c.setInvoicingAlgorithm(new AmericanInvoice()); 
     String actualOutput = c.getInvoice(); 
     final File f = new File("actual-american.txt"); 
     FileUtility.resetFile(f); 
     FileUtility.writeFile(f, actualOutput); 
     String expectedOutput = FileUtility.readFile(new File("expected-american.txt")); 
     //System.out.println(actualOutput); 
     //System.out.println(expectedOutput); 
     actualOutput = actualOutput.replaceAll("\\s", ""); 
     expectedOutput = expectedOutput.replaceAll("\\s", ""); 
     //System.out.println(actualOutput); 
     //System.out.println(expectedOutput); 
     assertEquals(actualOutput, expectedOutput); 
    } 
} 

回答

3

其实你不调用策略对象本身上的任何方法!

+0

所以在这个例子中策略对象是Customer类型的,不是吗? –

+0

不是,这是发票。您可以将一个发票类型替换为客户下的另一个发票类型 –

1

我不会宽恕这种策略模式,因为目前的汇率不需要使用Strategy Pattern。但是下面的代码很可能是你打算根据你的例子做的。

public interface InvoicingAlgorithm { 
    public double adjustInvoice(double amount); 
} 


public class AmericanInvoice implements InvoicingAlgorithm {  
    //Uk: america 1 : 1.57 
    @Override 
    public double adjustInvoice(double amount) { 
     return amount * 1.57; 
    } 
} 

public class Customer { 

    /** 
    * @param name represent the name of the Customer 
    */ 
    private String name; 

    /** 
    * @param amount represent the amount of money 
    */ 
    private double amount; 

    /** 
    * @param i represents object of InvoicingAlgorithm 
    */ 
    private InvoicingAlgorithm i; 

    Customer(String name, double amount) { 
     this.name = name; 
     this.amount = amount; 

    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public double getAmount() { 
     return amount; 
    } 

    public void setAmount(double amount) { 
     this.amount = amount; 
    } 

    public InvoicingAlgorithm getI() { 
     return i; 
    } 

    public void setInvoicingAlgorithm(InvoicingAlgorithm i) { 
     this.i = i; 
    } 

    public String getInvoice() { 
     DecimalFormat df = new DecimalFormat("#.00"); 
     String total = "--------------------------------------TO: " 
       + name + "FROM: Easyflap (UK) AMOUNT" + ":$" + 
       df.format(i.adjustInvoice(amount)) 
       + "--------------------------------------"; 

     return total; 
    } 
} 
+0

为什么您在InvoicingAlgorithm界面中更改了方法的签名 –

+0

@ Doesn'tMatter因为发票的名称与汇率没有关系。发票的名称似乎附加在客户身上。它看起来像你所有的策略是试图做的是转换货币。如果您需要“打印”您的调用,则将客户的所有打印语句移至美式策略。 –

+0

它仍然返回相同的结果,所以不能通过新的金额 –