2012-11-10 7 views
0

안녕하세요 모두 전략 패턴을 구현하려고하지만 구체적인 클래스에서 금액을 설정할 수 없습니다. 그 의미는 양이 관계가있는 도우미 클래스의 것과 동일한 것을 다시 시도한다는 것입니다. 인터페이스로 나는 생성자와 setter 및 getter 메소드를 사용하여 값을 설정하려고 시도했지만 모양을 가질 수 있고 graet 일 수있는 의견을 제시하면 작동하지 않습니다. 여기 코드가 있습니다.전략 디자인 패턴

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 (UK) 금액 : $ 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 송장 이름이 환율에 영향을 미치지 않으므로. 송장의 이름이 고객에게 부착 된 것 같습니다. 귀하의 모든 전략이 통화를 변환하려고하는 것처럼 보입니다. Invoke를 "인쇄"해야하는 경우 고객의 모든 인쇄 명령문을 미국 전략으로 이동하십시오. –

+0

여전히 동일한 결과를 반환하므로 새 금액을 전달할 수 없습니다. –