TOPIC 12.4.1
As operating systems have continued to grow in complexity, the job of programming user
interfaces has actually become simpler. This simplicity is largely due to the use of
object-oriented programming. The reusability, containment, and inheritance features
of the object-oriented programming paradigm make it a great tool for creating user
interfaces. Consider the web-browser you are currently using. Its user
interface consists of many components that you see in other programs: buttons, text
fields, dialog boxes, and windows. Therefore, it is very useful to make those
different components reusable. Some of the user interface components are contained
in other components. You would never see a button pop up all by itself.
Normally, a button would be contained in a dialog box, which would be the child of another
window. While these user interface components all serve different functions, they all
share some common properties such as location, size, and color. These shared
properties make use of inheritance.
Two popular languages for writing user interfaces are C++ and Java. Microsoft has made it even easier for application developers to write user interfaces by creating an extension of C++ called the Microsoft Foundation Classes otherwise known as MFC. Sun Microsystems created the Advanced Windowing Toolkit or AWT to help developers create user interfaces in Java. To show how object-oriented programming is used in user interface programming, the creation of a file dialog box will be shown in both C++/MFC and Java/AWT.
We have all used file dialog boxes. Whenever you open or save a file in your word processor, compiler, or text editor, you are using a file dialog box. Consider all of the different components included in a file dialog box. It contains multiple buttons, text fields, and other components.
Using MFC, a file dialog box is created using the following statements:
CFileDialog * myFileDialogPointer;
myFileDialogPointer = new CFileDialog(TRUE);
In the Microsoft Foundations Classes, a file dialog box inherits properties from four other classes: CObject, CWnd, CDialog, and CCommonDialog. The class hierarchy of these five classes can be seen in the following figure.
CObject
CWnd
CDialog
CCommonDialog
CFileDialog
Using the Java AWT, a file dialog box is created using the following statement:
FileDialog myFileDialog = new FileDialog(myFrame);
A Java file dialog box also inherits properties from four other classes: java.lang.Object, java.awt.Component, java.awt.Container, and java.awt.Window. The class hierarchy of these five classes can be seen in the following figure.
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Window
java.awt.Dialog
Although we used two different programming languages, both the C++ and Java created a file dialog box that looks like the following figure.
The object-oriented nature of these programming languages allows them to encapsulate a huge amount of complexity within the underlying classes and operating system. Look at all the flexibility contained in the file dialog box: the ability to look in multiple drives and directories, filter out files with a particular name or extension, and view varying levels of information about the files. As a programmer you get all of these features without having to write more than two lines of code.