类设计1

public class ReportService {
	
	private ReportGenerator generator = new PdfReportGenerator();
	private int[][] data = new int[50][50];
	
	public void generateAnnulReport(){
		generator.generate(data);
	}
	
	public void generateMonthlyReport(){
		generator.generate(data);
	}
	
	public void generateDailyReport(){
		generator.generate(data);
	}
	
}
public interface ReportGenerator {
	public void generate(int[][] data);
}
public class HtmlReportGenerator implements ReportGenerator{

	public void generate(int[][] data) {
		System.out.println("HtmlReportGenerator");
	}

}
public class PdfReportGenerator implements ReportGenerator {

	public void generate(int[][] data) {
		System.out.println("PdfReportGenerator");
	}

}

   这种设计,高层的ReportService依赖了底层的PdfReportGenerator和ReportGenerator类,还不是很好,一旦希望使用HtmlReportGenerator,那么就必须修改ReportService类。

你可能感兴趣的:(设计)