作为开发人员,大多数时候我们必须处理复杂的系统,包括许多依赖项,甚至根本不存在任何文档。
在这些情况下,找到应对这些挑战的方法至关重要。 处理它的最常见方法是隐藏所有这些依赖项并指定一个前置接口。
为了实现使用的模式是立面模式。
最终您会以非常复杂的系统结束它,该系统会以各种方式生成报告。 系统将报告生成为xml文件,csv文件,甚至从数据库生成报告。
xml文件中生成的报告基于位置提取统计信息,csv文件中生成的报告与时间序列数据以及有关使用情况数据的数据库报告有关。
上述功能需要合并到Web应用程序中。
第一步是根据每个报告创建一些界面。
第一个界面将是“地理位置报告”。 通过给出位置和距离,应生成报告,其中位置在距该点指定的距离之内。
package com.gkatzioura.design.structural.facade;
public interface GeolocationReport {
String[][] generate(Double lat,Double lng,Double distance);
}
Geolation报告将基于从我们的系统生成的xml报告。 因此,该实现将基于xml报告。
package com.gkatzioura.design.structural.facade;
public class XMLGeolocationReport implements GeolocationReport {
@Override
public String[][] generate(Double lat, Double lng, Double distance) {
/**
* http requests to retrieve the xml
* iterate the xml using stax
*/
return new String[][]{};
}
}
我们的下一份报告将与时间序列数据有关。 将给出两个日期作为参数,因此将检索所需的数据。
package com.gkatzioura.design.structural.facade;
import java.util.Date;
public interface TimeSeriesReport {
String[][] generate(Date start,Date end);
}
由于csv报告是使用时间序列数据生成的报告,因此我们的实现将基于系统的csv报告。
package com.gkatzioura.design.structural.facade;
import java.util.Date;
public class CSVTimeSeriesReport implements TimeSeriesReport {
@Override
public String[][] generate(Date start, Date end) {
/**
* retrieve the csv and iterate line by line within the time limits
*/
return new String[][]{};
}
}
最后一步是用户使用情况报告。 应该给出一个代表用户的uuid。
package com.gkatzioura.design.structural.facade;
import java.util.UUID;
public interface UsageReport {
String[][] report(UUID uuid);
}
使用情况报告将基于数据库报告。
package com.gkatzioura.design.structural.facade;
import java.util.UUID;
public class SQLUsageReport implements UsageReport {
@Override
public String[][] report(UUID uuid) {
return new String[0][];
}
}
到目前为止,我们已经抽象了系统的主要功能。 我们的下一步是创建使用所有这些功能的外观,并弥合我们的Web应用程序需求与提供信息的系统之间的鸿沟。
例如,根据用户的使用情况,我们需要具有基于用户的时间和位置的数据。
package com.gkatzioura.design.structural.facade;
import java.util.Date;
import java.util.UUID;
public interface UserUsageFacade {
String[][] usageOn(UUID user, Date from, Double lat,Double lng);
}
该实现将使用报表实现,以创建适合我们的Web应用程序的报表。
package com.gkatzioura.design.structural.facade;
import java.util.Date;
import java.util.UUID;
public class UserUsageFacadeImpl implements UserUsageFacade {
private final GeolocationReport geolocationReport;
private final TimeSeriesReport timeSeriesReport;
private final UsageReport usageReport;
private final double DEFAULT_DISTANCE = 20d;
private final int DEFAULT_TIME_RANGE = 20;
public UserUsageFacadeImpl(GeolocationReport geolocationReport, TimeSeriesReport timeSeriesReport, UsageReport usageReport) {
this.geolocationReport = geolocationReport;
this.timeSeriesReport = timeSeriesReport;
this.usageReport = usageReport;
}
@Override
public String[][] usageOn(UUID user, Date from, Double lat, Double lng) {
String[][] locationData = geolocationReport.generate(lat,lng,DEFAULT_DISTANCE);
Date to = Date.from(from.toInstant().plusSeconds(DEFAULT_TIME_RANGE));
String[][] timeSetiesData = timeSeriesReport.generate(from,to);
String[][] usageData = usageReport.report(user);
/**
* Generate the report based on the data retrieved
*/
return new String[][] {};
}
}
您可以在github上找到源代码。
您还可以在适配器模式, 装饰器模式, 复合模式和桥接模式中找到一些有用的文章。
翻译自: https://www.javacodegeeks.com/2018/08/structural-design-facade-pattern.html