java设计模式之Facade门面模式

java设计模式之Facade门面模式java设计模式之Facade门面模式门面设计模式为子系统中的一组接口提供了一个同意的接口,Facade定义了一个更高级别的接口,子子系统更易于使用。1.什么时候使用Facade模式?Facade设计模式是java中常用的一种结构式的设计模式,当我们想要以简化的方式向客户端公开一个复杂的系统时

大家好,欢迎来到IT知识分享网。java设计模式之Facade门面模式"

java设计模式之Facade门面模式


门面设计模式为子系统中的一组接口提供了一个同意的接口,Facade定义了一个更高级别的接口,子子系统更易于使用。

1.什么时候使用Facade模式?

Facade设计模式是java中常用的一种结构式的设计模式,当我们想要以简化的方式向客户端公开一个复杂的系统时,Facade是最合适的,它的目的是将内部复杂性隐藏起来,对外部提供一个简单的使用方式。Facade还可以将系统的代码从子系统的细节中解耦出来,便于以后维护。

2.Facade模式示例

要了解Facade,我们举两个非常简单的例子:当我们启动计算机时,我们所要做的就是按一下启动按钮,我们不关心计算机内部是如何运行的。在java编程中,我们必须连接到数据库,才能获取数据,我们只是调用dataSource.getConnection()来获取连接,但内部会发生很多事情,例如加载驱动等。下面我们来用代码来演示一下:

是这样的:我们想要一个报告生成器,它有多个步骤来创建任何报告,例如,它应首先创建报告页眉、页脚、添加数据行、格式化报告,然后报告的格式是pdf、html等。

代码:

public class ReportHeader {
}
public class ReportFooter {
}
public class ReportData {
}
public enum ReportType {
PDF, HTML
}
public class Report { private ReportHeader header; private ReportData data; private ReportFooter footer; public ReportHeader getHeader() { return header; } public void setHeader(ReportHeader header) { System.out.println("Setting report header"); this.header = header; } public ReportData getData() { return data; } public void setData(ReportData data) { System.out.println("Setting report data"); this.data = data; } public ReportFooter getFooter() { return footer; } public void setFooter(ReportFooter footer) { System.out.println("Setting report footer"); this.footer = footer; } }
public class ReportWriter {

    public void writeHtmlReport(Report report, String location) {
        System.out.println("HTML Report written");

        //implementation
    }

    public void writePdfReport(Report report, String location) {
        System.out.println("Pdf Report written");

        //implementation
    }
}
public class ReportGeneratorFacade
{
    public static void generateReport(ReportType type, DataSource dataSource, String location)
    {
        if(type == null || dataSource == null)
        {
            //throw some exception
        }
        //Create report
        Report report = new Report();

        report.setHeader(new ReportHeader());
        report.setFooter(new ReportFooter());

        //Get data from dataSource and set to ReportData object

        report.setData(new ReportData());

        //Write report
        ReportWriter writer = new ReportWriter();
        switch(type)
        {
            case HTML:
                writer.writeHtmlReport(report, location);
                break;

            case PDF:
                writer.writePdfReport(report, location);
                break;
        }
    }
}

运行测试:

public static void main(String[] args) throws Exception
    {
        ReportGeneratorFacade reportGeneratorFacade = new ReportGeneratorFacade();
         
        reportGeneratorFacade.generateReport(ReportType.HTML, null, null);
         
        reportGeneratorFacade.generateReport(ReportType.PDF, null, null);
    }

程序输出:

Setting report header
Setting report footer
Setting report data
HTML Report written
 
 
Setting report header
Setting report footer
Setting report data
Pdf Report written

3.常见问题

3.1Facade模式的优点:
门面设计模式并没有降低程序的复杂性,只是对外部隐藏内部的复杂,因此门面模式的受益者是使用方客户端的应用程序和其他系统。
3.2Facade依然要面对的挑战:
  • 子系统与facade门面相连,需要额外的代码层。
  • 当子系统的内部结构发生变化时,需要将这些变化合并到门面设计中。

  

 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/27858.html

(0)
上一篇 2023-12-03 21:00
下一篇 2023-12-04 09:33

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信