Facilitate Reuse Through Interfaces

Predicting and designing for systematic reuse is hard – in trying to design for future requirements, team often bake in accidental complexity. One effective way to avoid that trap is to separate your concerns via well designed interfaces. It is important to point out that although separating concerns is essential, which ones to prioritize and plan for is heavily dependent on your problem domain. Additionally, it is not pragmatic to provide for variability across all points in your design.

Here is an example of facilitating reuse via interfaces – the example class takes a ReportResult object and needs to format them into a variety of formats (XML, PDF, etc.). Although this is a simple example it is powerful – as new requirements for supporting additional formats arise it can gracefully be extended. Plus, more importantly, formats that aren’t required don’t have to be implemented until there is a real requirement.

public interface Formatter {
    public T format(ReportResult reportResult) ;

The above can be implemented using specific formatters as shown below:

public class XmlFormatter {
    public String format(ReportResult reportResult)  {
       //implement logic for XML formatting

With the report generation and formatting separation in place, additional formatters can be implemented with ease – e.g. PdfFormatter, HtmlFormatter, or PlainTextFormatter, etc. This decouples formatting logic from report generation classes thus enabling reuse for the report generation functionality.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: