Java 战略设计模式
大家好,我正在尝试实现策略模式,但是我不能在具体的类中设置数量,我的意思是数量与helper类中与接口有关系的数量相同。我试着用构造函数、setter和getter方法设置值,但是如果你能看一看并给出一些反馈,它就不起作用了。下面是代码Java 战略设计模式,java,strategy-pattern,Java,Strategy Pattern,大家好,我正在尝试实现策略模式,但是我不能在具体的类中设置数量,我的意思是数量与helper类中与接口有关系的数量相同。我试着用构造函数、setter和getter方法设置值,但是如果你能看一看并给出一些反馈,它就不起作用了。下面是代码 public interface InvoicingAlgorithm { public void getInvoice(String name, double amount); } public class AmericanInvoice imp
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;
}
}
因此,当我测试它时,它将返回值--------------------到:OracleFROM:Easyflap(英国)AMOUNT:$500.00--------------------这是在我尝试修改AmericanInvoice中的AMOUNT时,Customer类中的getInvoice方法的值,它不起作用
美国之音的测试课程
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);
}
}
实际上,您没有对strategy对象本身调用任何方法 您实际上没有对策略对象本身调用任何方法 我不允许以这种方式使用策略模式,因为当前汇率不需要使用策略模式。但是下面的代码很可能是您根据示例打算执行的操作
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;
}
}
我不允许以这种方式使用策略模式,因为当前汇率不需要使用策略模式。但是下面的代码很可能是您根据示例打算执行的操作
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;
}
}
为什么在InvoicingAlgorithm接口中更改方法的signature@Doesn“t之所以如此,是因为发票名称与汇率无关。发票的名称似乎附在客户身上。看起来你的策略所要做的就是转换货币。如果您需要它“打印”您的调用,那么将所有打印语句从客户移动到美国战略。它仍然返回相同的结果,因此无法传递新的amount@Doesn“tMatter你能用你正在使用的所有实际代码更新你的问题吗?这些只是碎片。其中是创建Customer对象并调用getInvoice的代码。为什么在InvoicingAlgorithm接口中更改方法的signature@Doesn“t之所以如此,是因为发票名称与汇率无关。发票的名称似乎附在客户身上。看起来你的策略所要做的就是转换货币。如果您需要它“打印”您的调用,那么将所有打印语句从客户移动到美国战略。它仍然返回相同的结果,因此无法传递新的amount@Doesn“tMatter你能用你正在使用的所有实际代码更新你的问题吗?这些只是碎片。其中是创建Customer对象并调用getInvoice的代码。因此,在本例中,strategy对象的类型是Customer,不是吗?不,是invoice。您可以在Customer Dynamicly下用一种发票类型替换另一种发票类型,因此本例中的策略对象的类型为Customer,不是吗?不,是发票。您可以在“客户”下动态替换一种发票类型