Object Oriented Software Design
Last modified Sunday, 07-Jul-2024 16:06:39 UTC.
Object-Oriented Design
Concepts
Focussed on the data rather than the manipulation
Larger software components are constructed from smaller software components (objects)
Objects can be customized and combined in many situations
Benefits:
The analysis, design, and coding phases merge. Software objects represent the "real" objects being modeled.
Objects are reusable, which avoids "re-inventing the wheel"
Development is incremental
Study existing systems - it may be possible to reuse or adapt an existing solution
Note: Not all problems are "best" solved with one technique or another
Object Oriented Design Process
Analysis
Domain
Identify entities in the problem domain (these often correspond to classes or objects for the design phase)
Actors
are things that do stuff in the reality
Function points
List what things are done by actors
List what things are done by non-actors (can be none)
Scenarios
Walk through typical sequences of events
Ensure all entities and functions are identified
Design
Classes and objects
Domain entities correspond to classes.
Things that appear in the scenarios correspond to objects - ensure that classes are listed for them.
Data of objects and classes
Determine if object (per item) or class (shared) data
Determine data types
Determine final or not
Determine if any should be public (in the Java sense)
Determine which can be accessed (in the general sense)
Determine which can be mutated (in the general sense)
Methods of objects and classes
Function points correspond to methods
Input and display methods are common
For objects
Constructors - what are suitable default values?
toString
Determine accessors and mutators (from data)
Differentiate between public (accessor and mutator) and private (helper) methods
Iterate Analysis and Design, as project evolves
Overall control
Use
imperative techniques
Example
Develop a control system for monitoring and controlling
fire fighting
scenarios
Existing systems - none :-)
Class exercise
Java packages
Many classes can be reused in different programs
The idiot way to achieve that is to copy the required
.java
files into the directory of the new program and recompile.
This would result in many copies of the source (imagine copying
String.java
for every program that uses strings!)
The smart Java way to do this is to create packages
Recall
The Big Picture
from
Java Basics
.
Packages
A package contains one or more related classes
The classes can be used by
import
ing them into programs, e.g.,
importing from the
java.util
package
Mechanics of making a package in IntelliJ
Choose a name for the package
Make a package with that name in the
src
folder
Put the
.java
files in the subdirectory
Add a line at the top of each of the
.java
files
package package_name_here;
Mechanics of using a package
Work in the directory containing the package directory
OR
Use the -cp flag of
javac
and
java
. In IntelliJ this requires setting dependencies.
OR
Put the package directory in the
CLASSPATH
environment variable. In IntelliJ this is done by editing the run configuration.
Use
import
statements to import the classes
Use
.*
after the package name to import all classes
Use
.class_name_here
after the package name to import specific classes from the package
If you don't want the
.java
files to be accessible, move them out to somewhere safe.
Example
My package directory is in the project directory.
The package name is
violence
The
Gun.java
,
Cop.java
, and
Car.java
classes will be part of the package, and are moved into the
violence
directory.
The
package
statement is added to the top of each of
Gun.java
,
Cop.java
, and
Car.java
I'll leave the source code there
UseViolence.java
uses the package
Package visibility
If there are methods in a class that should not be available for use outside the package, do not declare those methods as
public
.
By providing no visibility modifier, they have "package visibility", which means they are available only within the package.
Example: The
fire()
method in
Gun.java
. Only a Cop can fire a Gun.
If there are classes that should not be available for use outside the package, do not declare those classes as
public
.
Example: In the
violence
package, have a
Bullet
class that might be used by the
Gun
class ...
Bullet.java
. Only Cops and Guns can have Bullets.
Methods that are
protected
(see
the notes on inheritance
) also have package visibility.
Exercises
Do an object oriented design for an exam room situation.
Exam Style Questions
Name the two main steps, and each of their three substeps, in object oriented design. What design techniques are used for overall control?
What steps would have to be taken to put the files
Eat.java
,
My.java
, and
Pants.java
into a package called
Simpson
?
How are packages used by programs?