Open-Closed Principle OCP
Software Design Principles #2: OCP. "Software entities should be open for extension, but closed for modification." This one has the obvious benefit where the underlying code will not need changes when new methods are being created.
One key feature of Object Oriented Design are the Interfaces, that help us to have multiple implementations for a single Interface. The idea of OCP is to create abstract interfaces instead of classes, making the adding of new behaviour as easy as creating new classes - without touching and breaking the existing code.
Here the developer has to stop for a second and think what are the possible system changes that could be implemented in the future and implement abstraction against them. UML-based design tools are usually exporting ready-made interface code that can be utilized here.

For the practical demonstration I created a small application that would transfer different kinds of documents to internet for example.
Instead of creating several transfer functions for different filetypes (classes), I just use one generic Transferrable-interface that accepts both XML and Excel-documents as its input.
The application has three logical layers:
- com.markusvirtanen.design.ocp - main application logic
- com.markusvirtanen.design.ocp.bo - business objects (filetypes)
- com.markusvirtanen.design.ocp.boundaries - exported interface-code from UML-tool
Finally the most important, the code. The Interface describing the big picture of our Transferrable-service as a group of methods:
/*
* Copyright (c) 2009 Markus Virtanen. All rights reserved.
*/
package com.markusvirtanen.design.ocp.boundaries;
public interface Transferrable
{
public String getName();
public int getChecksum();
public void transfer();
}
And main code utilizing Transferrable-interface as follows:
/*
* Copyright (c) 2009 Markus Virtanen. All rights reserved.
*/
package com.markusvirtanen.design.ocp;
import com.markusvirtanen.design.ocp.bo.XlsDocument;
import com.markusvirtanen.design.ocp.bo.XmlDocument;
import com.markusvirtanen.design.ocp.boundaries.Transferrable;
/**
* Open-Closed Principle demo
*
* @author Markus
* @version 1.0, 20 Oct 2009
*
*/
public class OCP
{
/**
* Transfers document to target location.
*
* @param target Transferrable object.
*/
public void transferDocument( Transferrable target )
{
System.out.println("Transferring document " + target.getName());
// TODO: real transfer implementation
target.transfer();
}
/**
* Creates few documents and transfers them to internet.
*
* @param args
*/
public static void main( String[] args )
{
XmlDocument myXml = new XmlDocument("MyXML");
XlsDocument myXls = new XlsDocument("MyXLS");
OCP ocp = new OCP();
ocp.transferDocument(myXml);
ocp.transferDocument(myXls);
}
}
Without the abstraction in transferDocument( Transferrable target ) we would need several functions for different filetypes, such as transferXml( XmlDocument target ) and transferXls( XlsDocument target ). Later on, when we need to support more filetypes in our application, we could easily add them as classes in Business Objects -layer and with our Interface-rule we know in advance how to call the new classes in the future.
Still, the not so interesting part, code for XlsDocument class that implements our Transferrable-service:
/*
* Copyright (c) 2009 Markus Virtanen. All rights reserved.
*/
package com.markusvirtanen.design.ocp.bo;
import com.markusvirtanen.design.ocp.boundaries.Transferrable;
public class XlsDocument implements Transferrable
{
private String name;
public XlsDocument( String string )
{
this.name = string;
}
@Override
public int getChecksum()
{
return 0;
}
@Override
public void transfer()
{
}
@Override
public String getName()
{
return name;
}
}
For the next time I try to find some code highlight plugin for the site. :-)
Cheers,
/markus

