战略设计模式

时间:2012-11-10 17:20:03

标签: java strategy-pattern

大家好我正在尝试实现策略模式,但我无法在具体类中设置数量,我的意思是数量与助手类中与接口有关系的数量相同。我尝试使用构造函数和setter和getter方法设置值,但如果您可以查看并提供一些反馈,它将无法正常工作。这就是代码。

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);
    }
}

2 个答案:

答案 0 :(得分:3)

您实际上并未在策略对象本身上调用任何方法

答案 1 :(得分: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;
    }
}