如何实施策略设计模式

时间:2020-08-06 21:51:51

标签: java design-patterns

任何人都可以在下面的代码中告诉我们如何实现策略设计模式。我已经遍历了各种链接,但是关于实现它的方法还不十分清楚。 问题是:使用策略设计模式计算点并以PDF或HTML格式显示报告。 下面是已经可用的代码。客户,电影和租赁是已经创建的类:

  1. Customer.java

     public class Customer {
     private String _name;
     private Vector _rentals = new Vector();
     public Customer(String name) {
     _name = name;
     };
    
     public void addRental(Rental arg) {
     _rentals.addElement(arg);
     }
    
     public String getName() {
     return _name;
     }
    
    public static void main(String[] args) {
    
     Customer c = new Customer("ABC");
     Movie m = new Movie("Title", 1);
     Rental r = new Rental(m, 10);
     c.addRental(r);
     // Rental calculation
     String s = c.statement();
     System.out.println("s: " + s);
    }
         public String statement() {
         double totalAmount = 0;
     int frequentRenterPoints = 0;
     Enumeration rentals = _rentals.elements();
     String result = "Rental Record for " + getName() + "\n";
    
     while (rentals.hasMoreElements()) {
         double thisAmount = 0;
         Rental each = (Rental) rentals.nextElement();
    
         // determine amounts for each line
         switch (each.getMovie().getPriceCode()) {
    
         case Movie.REGULAR:
             thisAmount += 2;
             if (each.getDaysRented() > 2)
                 thisAmount += (each.getDaysRented() - 2) * 1.5;
             break;
    
         case Movie.NEW_RELEASE:
             thisAmount += each.getDaysRented() * 3;
             break;
    
         case Movie.CHILDRENS:
             thisAmount += 1.5;
             if (each.getDaysRented() > 3)
                 thisAmount += (each.getDaysRented() - 3) * 1.5;
             break;
         } // add frequent
             // renter points
         frequentRenterPoints++;
    
         // add bonus for a two day new release rental
         if ((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) && each.getDaysRented() > 1)
             frequentRenterPoints++;
    
         // show figures for this rental
         result += "\t" + each.getMovie().getTitle() + "\t" + String.valueOf(thisAmount) + "\n";
         totalAmount += thisAmount;
     }
    
     // add footer lines
     result += "Amount owed is " + String.valueOf(totalAmount) + "\n";
     result += "You earned " + String.valueOf(frequentRenterPoints) + " frequent renter points";
     return result;
     }
     }
    
  2. Movie.java

    public class Movie {
    public static final int CHILDRENS = 2;
    public static final int REGULAR = 0;
    public static final int NEW_RELEASE = 1;
    private String _title;
    private int _priceCode;
    
     public Movie(String title, int priceCode) {
    _title = title;
    _priceCode = priceCode;
    }
    public int getPriceCode() {
        return _priceCode;
    }
    
    public void setPriceCode(int arg) {
    _priceCode = arg;
    }
    public String getTitle() {
    return _title;
    };
    }
    
  3. Rental.java

    public class Rental {
        private Movie _movie;
    private int _daysRented;
    public Rental(Movie movie, int daysRented) {
        _movie = movie;
    _daysRented = daysRented;
    }
    public int getDaysRented() {
        return _daysRented;
    }
        public Movie getMovie() {
        return _movie;
    }
    }
    

2 个答案:

答案 0 :(得分:0)

您的任务是“使用策略设计模式以PDF或HTML格式显示报告”,因此将在此处应用策略。 在显示“报告”之前,您应该拥有所有计算出的数据(保持工作不变),在传递数据之后并选择适当的报告类型。

public interface IReport 
{
    public void printReport(String data);
}

public class ReportHTML implements IReport
{
    @Override
    public void printReport(String data) {
        System.out.println("Html Report with data="+data);  
    }
}

public class ReportPDF implements IReport{
    @Override
    public void printReport(String data) {
        System.out.println("PDF Report with data="+data);   
    }
}

public class Test {
    public static void main(String[] args)
    {
        //do whatever computation concerning business logic
        //after for print pass computed data and chose report type as needed
        IReport ir = new ReportHTML();
        ir.printReport("[Customers, Movie, Rental]");
        ir = new ReportPDF();
        ir.printReport("[Customers, Movie, Rental]");
    }
}

输出:

Html Report with data=[Customers, Movie, Rental]
PDF Report with data=[Customers, Movie, Rental]

答案 1 :(得分:0)

HTML和PDF是显示格式。策略只是通常由接口定义的可插拔算法。由于任务是显示报告,而可插拔部分是如何显示报告(算法),因此需要一种显示策略。至于名称,很多人会调用ReportDisplayStrategy界面,这很好,但是我经常认为设计模式名称只是污染,因此我会考虑使用类似于ReportRenderer的名称。如果输出始终是文件(您可以在此处执行更多操作),则界面可能如下所示:

interface ReportRenderer
{ 
    File render(Report report)
}

剩下的事情就是制作报告对象并编写PDF和HTML的渲染器实现。