- Forward


Abstract Classes and Interfaces
Design Issues


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Review
Back SMYC Forward
  • Abstract Class:
    • A class without any instances
  • Interface:
    • A set of services provided by a class
Review (cont.)
Back SMYC Forward
  • Similarities:
    • Both can contain abstract methods
  • Differences:
    • An interface only contains abstract methods
    • An abstract class can contain concrete methods
Design Issues
Back SMYC Forward
  • When to use an abstract class:
    • You have several classes that provide the same functionality in the same way but you do not want to move that functionality into a concrete parent because it doesn't make sense to have instances of the parent
  • When to use an interface:
    • You have classes that provide the same functionality in different ways and/or are not related hierachically
    • You need to reveal the capabilities of an object without revealing its class
Some Examples in Java
Back SMYC Forward
  • Abstract Classes:
    • AbstractButton : A generic button-like object in a GUI
    • AbstractCollection : A skeletal implementation of a class that contains a "set" of other objects
  • Interfaces:
    • Comparable : Objects that can be ordered
    • ActionListener : Objects that can be performed of GUI actions (e.g., button clicks)
A Design Exercise
Back SMYC Forward
  • Motivation:
    • The only way to get better is to practice
  • The System to be Designed:
    • The Madison Music Machine 3000: A software implementation of a "karaoke machine" that can play (instrumental) music while simultaneously displaying the lyrics to be sung.
A Design Exercise (cont.)
Back SMYC Forward

An Initial Design

madisonmusicmachine-v1
A Design Exercise (cont.)
Back SMYC Forward

An Improved Design

madisonmusicmachine-v2
A Design Exercise (cont.)
Back SMYC Forward

More Improvements

madisonmusicmachine-v3
A Design Exercise (cont.)
Back SMYC Forward

Eliminating the MadisonMusicMachine Class

madisonmusicmachine-v4
A Design Exercise (cont.)
Back SMYC Forward

Synchronizing the Music and Lyrics

madisonmusicmachine-v5
A Design Exercise (cont.)
Back SMYC Forward

Improved Synchronization

madisonmusicmachine-v6
A Design Exercise (cont.)
Back SMYC Forward

Adding a Presenter Interface

madisonmusicmachine-v7
A Design Exercise (cont.)
Back SMYC Forward

Combining the Lyrics and Music Classes

madisonmusicmachine-v8
A Design Exercise (cont.)
Back SMYC Forward

Adding a PlayList Class

madisonmusicmachine-v9
A Simple Implementation
Back SMYC Forward

AbstractLineElement

javaexamples/oopbasics/musicmachine/AbstractLineElement.java
 

Lyric

javaexamples/oopbasics/musicmachine/Lyric.java
 

Note

javaexamples/oopbasics/musicmachine/Note.java
 
A Simple Implementation (cont.)
Back SMYC Forward

Presenter

javaexamples/oopbasics/musicmachine/Presenter.java
 

Console

javaexamples/oopbasics/musicmachine/Console.java
 

InstrumentSynthesizer

javaexamples/oopbasics/musicmachine/InstrumentSynthesizer.java
 
A Simple Implementation (cont.)
Back SMYC Forward

MetronomeListener

javaexamples/oopbasics/musicmachine/MetronomeListener.java
 
A Simple Implementation (cont.)
Back SMYC Forward

Part

javaexamples/oopbasics/musicmachine/Part.java
 
A Simple Implementation (cont.)
Back SMYC Forward

Song

javaexamples/oopbasics/musicmachine/Song.java
 
Concluding Thoughts
Back SMYC Forward
  • The Design Process:
    • Could have been improved by differentiating between conceptual models (for analysis) and class models (for engineering design)
  • The Design Itself:
    • Could have been improved using design patterns, most notably the Model-View-Controller Pattern
There's Always More to Learn
Back -