Contents
The Essence of Object Oriented
Programming with Java and UML
by
Bruce E. Wampler, Ph.D.
Contents
Preface
Why This Book?
Who Is This Book For?
Overview of Chapters
About the Author
Acknowledgements
Chapter 1: Objects, UML, and Java
Introduction
Object Orientation
Object-Oriented Languages
Object-Oriented Design and the UML
The Payoff of Objects
Chapter Summary
Chapter 2: The Essence of Objects
What Is an Object-Oriented System?
file:///C|/oobook/EOOPTOC.html (1 of 8) [13/03/2003 02:55:02 }Ç
Contents
Fundamental Properties of an Object-Oriented System
Abstraction with Objects
Encapsulated Classes
Communication via messages
Object Lifetime
Class Hierarchies
Polymorphism
Software Methodologies
The Elements of a Software Project
The Essence of Object-Oriented Analysis
Object Discovery
Evaluate Candidate Objects
Determine Object Hierarchies
Discover Object Attributes
Discover Object Operations
The Essence of Object-Oriented Design
Some Design Guidelines
Get The Big Picture
Encapsulation
Designing Classes
Inheritance
General Guidelines
The Build and Release Phases
Building the Software
Releasing the Software
More on the UML
Chapter Summary
Resources
OOAD
CRC
Chapter 5: Object-Oriented Graphical User Interfaces
with Swing
Graphical User Interfaces
A Typical Application
Dialog Boxes
Events
file:///C|/oobook/EOOPTOC.html (3 of 8) [13/03/2003 02:55:02 }Ç
Movie Helper Classes
Review
Chapter Summary
file:///C|/oobook/EOOPTOC.html (4 of 8) [13/03/2003 02:55:02 }Ç
Contents
Chapter 7: Design Patterns
What are Design Patterns?
Using Design Patterns
Design Pattern Description Template
The Gang of Four Patterns
Creational Patterns
Structural Patterns
Behavioral Patterns
Example Design Patterns used by Wmvc and MovieCat
MVC
Observer Pattern
Observer Pattern in Wmvc
Command Pattern in Wmvc
Other Patterns used in Wmvc and MovieCat
Chapter Summary
Resources
Chapter 8: Refactoring
What is Refactoring?
The Basic Refactoring Process
When Do You Refactor?
Code Smells
When not to refactor
Some Refactorings
Refactoring Categories
Some Specific Refactorings
CASE, Modeling, and UML Tools
ArgoUML
MagicDraw
Rational Software
TogetherSoft
Other UML Tools
Other Java Tools
JUnit
Jikes
file:///C|/oobook/EOOPTOC.html (6 of 8) [13/03/2003 02:55:02 }Ç
Contents
Chapter 11: Programming - A Personal Perspective
Programming
Your Code Never Dies
Program With Style
Know What You Are Doing
Write Practice Programs
Practice Incremental Programming
The Tools Matter
Objects Really Help
Testing
Debugging
Don't Reinvent the Wheel
Sometimes It is Better to Do It Yourself
You Can Get Ideas Any Time
Get A Life
A Plan Matters
The Tools
Your Editor Really Matters
Know About the Time-Tested Tools
med for Flyheart
file:///C|/oobook/EOOPTOC.html (8 of 8) [13/03/2003 02:55:02 }Ç
Why This Book?
Preface
Why This Book?
The goal of this book is to cover the essence of what you need to
know to develop object-oriented software using Java and UML.
When you are through with this book, you should understand object-
oriented software development well enough to answer the following
questions:
●
What is object orientation?
●
What is the UML?
●
What is Object-Oriented Analysis and Design?
●
How do you do OOAD?
●
What are object-oriented development methodologies?
●
How do you use Java to write truly object-oriented programs?
●
What is Swing, and how can you use it to write object-oriented
graphical user interfaces?
●
What are design patterns?
●
What is refactoring?
oriented software development.
Chapter 2 covers the fundamental concepts of object orientation.
Object orientation has many important concepts, and of course, its
own vocabulary. It is very important for you to understand the main
concepts, and to be familiar with the specialized vocabulary. Even if
you already are familiar with some object-oriented concepts, you
should review them in this chapter.
Chapter 3 covers how to use Java to write object-oriented programs.
It is not really a Java tutorial, but rather concentrates on using Java to
file:///C|/oobook/Preface.html (2 of 6) [13/03/2003 02:55:04 }Ç
Why This Book?
implement object-oriented concepts. The first part of the chapter
covers very basic Java concepts. However, the later parts of the
chapter cover more advanced topics such as object lifetime, copies of
objects, and other concepts that are very important when working
with classes and objects.
Chapter 4 covers Object-Oriented Analysis and Design (OOAD).
Rather than focusing on any specific OOAD methodology, Chapter 4
covers the basic concepts that are important for any methodology.
The first four chapters cover the essence of object orientation.
Chapter 5 takes a look at Graphical User Interfaces (GUIs) and the
Java Swing library using the object-oriented perspective developed in
the previous chapters. This object-oriented introduction to Swing is a
somewhat different approach than is typically found in Swing
tutorials.
Chapter 6 ties everything together with a case study of a small Java
application. The fundamental OOAD concepts covered in Chapter 4
are used to design the application, and the Java and Swing concepts
covered in Chapter 3 and 5 are used for the implementation.
The remainder of the book is less comprehensive in its treatment. The
the University of New Mexico, a relationship that lasted, at least on a
part time basis, until 1997. But I just couldn't stay out of the PC
business. I decided to continue my work on grammar checking, and
in 1985 started a new PC software company with some partners in
San Francisco. That company, Reference Software International,
developed and marketed the Grammatik grammar checker. I was
Chief Scientist there, and built a fairly large software development
group to improve Grammatik and build other reference software
products. WordPerfect bought Reference Software in 1992, and I
went back to teaching at the University of New Mexico. It was there
that I first started thinking about writing a book about object-oriented
programming.
file:///C|/oobook/Preface.html (4 of 6) [13/03/2003 02:55:04 }Ç
Why This Book?
In the mean time, I designed and wrote an open source C++ GUI
framework called V. It is an easy to use framework for building
simple GUI applications on Windows and X, and is in widespread
use today. I also wrote the VIDE freeware editor and integrated
development environment, which is widely used.
Of all the advancements in software development I've witnessed over
the years, object-oriented programming has seemed to me the most
significant in terms of how much easier it makes the programming
task. Object-oriented programming in Java or C++ can really make a
difference when developing programs. While it doesn't solve all the
problems of software development, it makes the development easier,
and the long-term maintenance much easier. The result is a real gain
in programming productivity. It is well worth the effort to learn
object-oriented software development.
The goal of this book is to introduce you to the essence of object
orientation without overwhelming you with all the details of a
med for Flyheart
file:///C|/oobook/Preface.html (6 of 6) [13/03/2003 02:55:04 }Ç
Chapter 1
Chapter 1
Objects, UML, and Java
Introduction
This book is about object-oriented software development. Writing
real object-oriented programs that are used by real people is more
than slapping down a few lines of code in Java (or C++, Eiffel, or any
other object-oriented programming language). Ultimately, object-
oriented software development includes the complete process -
analysis of the problem, design of a solution, coding, and finally long-
term maintenance. Object-oriented development can make any size
program better - from a small web based application to a full-blown
business critical software system.
Object orientation has the potential for building great software, but
only if it is used as part of a complete process. Today, there are small,
agile development methodologies suitable for teams of two to ten or
so programmers, as well as large scale methodologies for huge
projects. Most of these development methodologies use or can benefit
from the UML, a modelling tool that can aid the design of any OO
system. But before you can understand and use any of these
methodologies, you need to move beyond merely getting a program
to work, and change your thinking to be object-oriented.
It has been said that any programming language can be used to write
object-oriented programs (and it has been done with C), but a true
file:///C|/oobook/Chapter1.html (1 of 7) [13/03/2003 02:55:04 }Ç
Chapter 1
OO programming language makes it a lot easier. Just because you use
how the box is put together inside are irrelevant while you are using
the box. What is important is that the object carries out its functions
and responsibilities correctly. A software object is not much different.
It has well-defined methods for interacting with the outside world,
and can provide information about its current state. The internal
representation, algorithms, and data structures are hidden from the
outside world.
Figure 1-2.
A Black Box
In the simplest terms, designing an OO system consists of identifying
what objects the system contains, the behaviors and responsibilities
of those objects, and how the objects interact with each other. OO can
produce elegant, easy to understand designs, which in turn leads to
elegant and easy to understand programs. Individual objects can often
be implemented and debugged independently. Libraries of existing
objects can be easily reused and adapted to new designs. Most
importantly, a good OO program is easy to modify, and resistant to
the introduction of bugs during program modification and
maintenance.
Object-oriented development is a major advance for software
file:///C|/oobook/Chapter1.html (3 of 7) [13/03/2003 02:55:04 }Ç
Chapter 1
development. Although it may not be a magic bullet that solves all
the problems associated with producing software, it is really better
than other methodologies. While development methodologies such as
structured design and programming have many valid points, many
which carry over and are used for OO development, object-oriented
designs are inherently easier to design and maintain over time.
Object-Oriented Languages
that means it will focus on general principals of object-oriented
programming that apply to any programming language. But this book
will also show how to translate object-oriented designs to real
programs using Java. The focus will be on how to use the capabilities
of the Java language to implement OO designs. It is not a tutorial on
learning Java. We assume that you've already learned the Java basics.
Now you are ready to learn about objects, and how to use Java to
write better programs.
Object-Oriented Design and the UML
There are several different object-oriented development
methodologies in use today. Each has its strengths and weaknesses.
The older, more traditional methodologies are often called
"heavyweight" methodologies, and are most useful for large software
projects involving tens or even hundreds of programmers over years
of development effort. The newer methodologies are called
"lightweight" or "agile" methodologies, and are more appropriate for
smaller projects. Many of these are quite new and still being
standardized as this book was being written.
Design and development methodologies have always needed a
graphical notation to express the designs. In the past, one of the major
problems has been that each major methodology has had its own
graphical notation. This has all changed with the emergence of the
UML (Unified Modeling Language) as the standard notation. Any of
the current design methodologies, heavyweight or agile, use or can
benefit from the UML.
file:///C|/oobook/Chapter1.html (5 of 7) [13/03/2003 02:55:04 }Ç
Chapter 1
The UML originated in the mid-1990's from the efforts of James
Rumbaugh, Ivar Jacobson, and Grady Booch (The Three Amigos).
There is a standard specification of the UML coordinated by the
med for Flyheart
file:///C|/oobook/Chapter1.html (7 of 7) [13/03/2003 02:55:04 }Ç
Chapter 2
Chapter 2
The Essence of Objects
So, what exactly is Object Orientation? It is a problem solving
technique used to develop software systems. Object orientation is the
culmination of years of experience in finding an effective way to
develop software, and is certainly the dominant method used to
develop major software systems today. Object orientation is a
technique of modeling a real-world system in software based on
objects. The object is the core concept. An object is a software model
of a real-world entity or concept.
Almost anything can be modeled as an object in software. For
example, you could model a temperature sensor as an object. Or, in a
more abstract system, you could model color as an object. Even
something as basic as a number can be considered an object that has a
value and a type. Typically, each object has an associated set of
attributes such as value, state, or whatever else is needed to model the
object. Sensor attributes might include a state such as active or
inactive, an attribute indicating its current value, and information
about its physical location. Objects usually provide the ability to
modify their state as well. In addition to keeping track of the current
temperature, for example, a temperature sensor object might provide
a way to turn the sensor on or off.
The attributes, or data, used inside an object are really only a tiny part
of what an object is and does. An object also has a set of
file:///C|/oobook/Chapter2.html (1 of 42) [13/03/2003 02:55:09 }Ç
Chapter 2
file:///C|/oobook/Chapter2.html (2 of 42) [13/03/2003 02:55:09 }Ç
Chapter 2
class is a description or definition of the characteristics of objects that
belong to that class. An object is a single instance or member of a
class that is created and exists while the program is running. A class
may have just a single object or any number of member objects
existing at any given time. All members of a class have similar
behavior.
For example, consider a software system used to monitor various
sensors in a factory. One obvious kind of object present is such a
system is a sensor. A class called Sensor would be defined and used
to model physical sensors. The class would define the basic
characteristics of any Sensor, such as its location, value, and
identification number, as well as a set of services used to carry out its
responsibilities. Each individual physical sensor in the system would
be represented as an object belonging to the class Sensor, and have
specific values for the attributes described by the class definition.
The class description includes the means of accessing and changing
the state of individual object members of that class. A common
representation of color is called RGB, where the color is specified by
the values of its red, green, and blue components. One possible
design of a class called Color could provide the means of
manipulating the color by both retrieving and setting the RGB values
of a Color object.
In an object-oriented system, it is typical to describe one class based
on a pre-existing class, either by extending the description of a higher
level class, or by including the description of another class within the
current class. For example, you could create one class that describes
the general characteristics of all sensors, and then more specialized
classes that describe specific sensors such as temperature or pressure
oriented language to implement a system using classes or objects, but
the system must have all the following properties to be considered a
true object-oriented system.
file:///C|/oobook/Chapter2.html (4 of 42) [13/03/2003 02:55:09 }Ç