Objects and Interfaces
This series, The Object-Oriented Thought Process, is intended for someone just learning an object-oriented language and who wants to understand the basic concepts before jumping into the code, or someone who wants to understand the infrastructure behind an object-oriented language he or she is already using. These concepts are part of the foundation that any programmer will need to make the paradigm shift from procedural programming to object-oriented programming.
Click here to start at the beginning of the series.
In keeping with the code examples used in the previous articles, Java will be the language used to implement the concepts in code. One of the reasons that I like to use Java is because you can download the Java compiler for personal use at the Sun Microsystems web site http://java.sun.com/. You can download the standard edition, J2SE 5.0, at http://java.sun.com/j2se/1.5.0/download.jsp to compile and execute these applications. I often reference the Java J2SE 5.0 API documentation and I recommend that you explore the Java API further. Code listings are provided for all examples in this article as well as figures and output (when appropriate). See the first article in this series for detailed descriptions for compiling and running all the code examples.
In a previous column, you continued to learn about the various Java collections by covering the ArrayList collection. Before I proceed with coverage of more collections, I will stop and concentrate on the topic of interfaces. The concept of an interface is core to any object-oriented design or implementation.
Interfaces are an extremely important concept; however, they are also very tricky to understand. One problem is that there are many programming concepts that are referred to as an interface. The following three bullet items are the most common, but not the only, concepts that are called an interface.
- Graphical User Interface
- Interface versus Implementation
- Object-oriented interface (as in Java and/or .NET)
Graphical User Interface
Anyone who has used a computer is familiar with a Graphical User Interface or GUI. This term relates to a screen that contains graphical components—such as buttons, labels, and menu items—that is meant to interface with a user. Filling out an on-line form is a great example of a GUI.
Interface versus Implementation
In a design exercise, the interface versus implementation question is also a key object-oriented concept, but it is not the same thing as a Java/.NET interface. You can go back to the second article in this series and look at the example of generating electricity.
The design interface is the fundamental means of communication between objects. Each class design specifies the interfaces for the proper instantiation and operation of objects. Any behavior that the object provides must be invoked by a message sent using one of the provided interfaces. The interface should completely describe how users of the class interact with the class. In Java, the methods that are part of the interface are designated as public.
Interfaces do not normally include attributes, only methods. If a user needs access to an attribute, a method is created to return the attribute (a getter). If a user wants the value of an attribute, a method that returns the value of the attribute is called. In this way, the object that contains the attribute controls access to it. This is of vital importance, especially in terms of testing and maintenance.
If you control the access to the attribute, when a problem arises, you do not have to worry about tracking down every piece of code that might have changed the attribute. It can only be changed in one place (the setter).
Only the public attributes and methods are considered the interface. The user should not see any part of the implementation interacting with an object solely through class interfaces. In many cases, there will be methods that also should be hidden and not be part of the interface. Thus, the implementation can change and it will not affect the user's code.
Figure 1 illustrates the interface/implementation paradigm using real-world objects rather than code. The toaster obviously requires electricity. To get this electricity, the cord from the toaster must be plugged into the electrical outlet, which is the interface. All the toaster needs to do to get the required electricity is to use a cord that complies with the electrical outlet specifications; this is the interface between the toaster and the electricity.
The fact that the actual implementation is a coal-powered electric plant is not the toaster's concern. In fact, for all the toaster cares, the implementation could be a nuclear power plant or a local power generator. With this model, any appliance can get electricity, as long as it conforms to the interface specification seen in Figure 1.
Figure 1: Interface/Implementation
In this column, you will concentrate on this type of interface, which I call the object-oriented interface. This type of interface is provided by Java and .NET.
The interface is the services that are presented to an end user. In the best case, only the services that the end user needs are presented. Of course, which services the user needs may be a matter of opinion. If you put 10 people in a room and ask each of them to do an independent design, you might receive 10 totally different designs. There is nothing wrong with this. However, as a rule of thumb, the interface to a class should contain only what the user needs to know. In the toaster example, the user only needs to know that the toaster must be plugged into the interface—which in this case is the electrical outlet
Perhaps the most important issue when designing a class is identifying the audience, or users, of the class.
The implementation details of the interface services are hidden from the user. One goal regarding the implementation should be kept in mind: A change to the implementation should not require a change to the user's code. This may seem a bit confusing, but this goal is at the heart of the design issue.
Remember that the interface includes the syntax to call a method and return a value. If this interface does not change, the user does not care whether the implementation is changed. As long as the programmer can use the same syntax and retrieve the same value, that is all that matters.
Recall that in the toaster example, although the interface is always the electric outlet, the implementation could change from a coal power plant to a nuclear power plant without affecting the toaster. There is one very important caveat to be made here: The coal or nuclear plant must also conform to the interface specification. If the coal plant produces AC power, but the nuclear plant produces DC power, there is a problem. The bottom line is that both the user and the implementation must conform to the interface specification.
To Reuse or Not to Reuse?
One of the major advantages touted by O-O proponents is that you can write code once, and then reuse it to your heart's content. This is true to a certain degree. As with all design approaches, the utility and the reusability of code depends on how well it was designed and implemented. O-O design does not hold the patent on code reuse. There is nothing stopping anyone from writing very robust and reusable code in a non–O-O language. Certainly, there are countless numbers of routines and functions, written in structured languages such as COBOL and C, that are of high quality and quite reusable.
Thus, it is clear that following the O-O paradigm is not the only way to develop reusable code. Yet, the O-O approach does provide several mechanisms for facilitating the development of reusable code. One way to create reusable code is to create frameworks. In this chapter, you focus on using interfaces and abstract classes to create frameworks and encourage reusable code.
What Is a Framework?
Hand-in-hand with the concept of code reuse is the concept of standardization, which is sometimes called plug-and-play. The idea of a framework revolves around these plug-and-play and reuse principles. One of the classic examples of a framework is a desktop application. Take an office suite application as an example. The document editor that I am currently using has a menu bar that includes multiple menu options. These options are similar to those in the presentation package and the spreadsheet software. In fact, the first six menu items (File, Edit, View, Insert, Format, and Tools) are the same in all three programs. Not only are the menu options similar, but the first toolbar looks remarkably alike as well (New, Open, Save, and so on). Below the toolbars is the document area—whether it is for a document, a presentation, or a spreadsheet. The common framework makes it easier to learn various applications within the office suite. It also makes a developer's life easier by allowing maximum code reuse.
The fact that all these menu bars have a similar look and feel is obviously not an accident. In fact, when you develop in most integrated development environments, you get certain things without having to create them yourself. When you create a window in a Windows environment, you get elements such as the main title bar and the file close button in the top-right corner. When you double-click on the main title bar, the screen always minimizes/maximizes. When you click on the close button in the top-right corner, the application always terminates. This is all part of the framework.
For example, to create an applet in Java, you would bring up the API documentation for the Applet class and take a look at the public interfaces it presents. By using these APIs, you can create a valid Java applet and conform to required standards. If you follow these standards, your applet will be set to run in Java-enabled browsers.
What Is a Contract?
In the context of this article, you will consider a contract to be any mechanism that requires a developer to comply with the specifications of an Application Programming Interface (API). Often, an API is referred to as a framework. The online dictionary Dictionary.com defines a contract as "an agreement between two or more parties, especially one that is written and enforceable by law."
This is exactly what happens when a developer uses an API—with the project manager or business owner representing the law. In short, when using contracts, the developer is required to comply with the rules defined in the framework. This includes issues such as method names, number of parameters, and so on. In short, standards are created to facilitate good coding practices.
Note: The term contract is widely used in many aspects of business, including software development. Do not confuse the concept here with other possible software design concepts called contracts.
Enforcement is vital because it is always possible for a developer to break a contract. Without enforcement, a rogue developer could decide to reinvent the wheel and write his or her own code rather than use the code provided by the framework. There is little benefit to a standard if people routinely disregard or circumvent it. In Java and the .NET languages, the two ways to implement contracts are to use abstract classes and interfaces.