Smarter Java development
Use interfaces to effectively enforce development contracts while maintaining loose coupling of code
By Michael Cymerman, JavaWorld.com, 08/01/99
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
A quick and simple scheme to speed the development of large-scale Java applications involves the use of interfaces. Java interfaces
are a blueprint for the functionality contained in an associated object.
By incorporating interfaces into your next project, you will noticebenefits throughout the lifecycle of your development effort.
The technique of coding to interfaces rather than objects will improve the efficiency of the development team by:
- Allowing the development team to quickly establish the interactions among the necessary objects, without forcing the early
definition of the supporting objects
- Enabling developers to concentrate on their development tasks with the knowledge that integration has already been taken into
account
- Providing flexibility so that new implementations of the interfaces can be added into the existing system without major code
modification
- Enforcing the contracts agreed upon by members of the development team to ensure that all objects are interacting as designed
An overview
Because object-oriented development efforts involve the interactions of objects, it is essential to develop and enforce strong
contracts between those objects. The technique of coding to interfaces involves using interfaces, rather than objects, as
the primary method of communication.
This article will introduce the user to the concept of coding to interfaces through a simple example. A detailed example will
follow, helping demonstrate the value of this scheme in a larger system requiring multiple developers. Before we get to the
sample code, however, let's look at the benefits of coding to interfaces.
Why code to interfaces?
The Java interface is a development contract. It ensures that a particular object satisfies a given set of methods. Interfaces
are used throughout the Java API to specify the necessary functionality for object interaction. Examples of interface use
are callback mechanisms (Event Listeners), patterns (Observer), and specifications (Runnable, Serializable).
Coding to interfaces is a technique by which developers can expose certain methods of an object to other objects in the system.
The developers who receive implementations of these interfaces have the ability to code to the interface in place of coding
to the object itself. In other words, the developers would write code that did not interact directly with an object as such,
but rather with the implementation of that object's interface.
Another reason to code to interfaces rather than to objects is that it provides higher efficiency in the various phases of
a system's lifecycle:
- Design: the methods of an object can be quickly specified and published to all affected developers
- Development: the Java compiler guarantees that all methods of the interface are implemented with the correct signature and that all changes
to the interface are immediately visible to other developers
- Integration: there is the ability to quickly connect classes or subsystems together, due to their well-established interfaces
- Testing: interfaces help isolate bugs because they limit the scope of a possible logic error to a given subset of methods
There is some overhead associated with this development technique, due to the required code infrastructure. This infrastructure
includes both interfaces for the interactions between objects and invocation code to create implementations of interfaces.
This overhead is insignificant when compared to the ease and benefit of using interfaces as described.
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Resources
- Java DesignBuilding Better Apps and Applets, Peter Coad (Yourdon Press, 1998) -- brimming with information on proper design techniques, including inheritance, composition,
and the use of interfaces
http://www1.fatbrain.com/asp/bookinfo/bookinfo.asp?theisbn=0139111816
- "Designing with interfaces," Bill Venners (JavaWorld, December 1998) -- provides useful insight into the use of interfaces in a design to simulate polymorphism in Java and demonstrate
dynamic binding
http://www.javaworld.com/javaworld/jw-12-1998/jw-12-techniques.html
- "Java Tip 10Implement callback routines in Java", John Mitchell (JavaWorld) -- demonstrates how callbacks are used in Java. These callbacks, in addition to those used in event handling, are your first
experiences of coding to interfaces
http://www.javaworld.com/javaworld/javatips/jw-javatip10.html
- "Defining an Interface," from the Sun Java tutorial, provides some basics on interfaces
http://java.sun.com/docs/books/tutorial/java/interpack/createinterface.html
Re: Good post... butBy Anonymous on June 17, 2009, 11:21 amI think that was the interface which contained the 2 other interfaces.. could be wrong
Reply | Read entire comment
Great post! very usefulBy Anonymous on February 12, 2009, 9:40 pmGreat post! very useful
Reply | Read entire comment
Good post... but:By Anonymous on January 18, 2009, 3:03 pm"public class Scheduler implements Schedule{" ¿Where is Schedule Interface, i can´t see it?
Reply | Read entire comment
View all comments