CS535 Object-Oriented Programming & Design
Fall Semester, 1996
Doc 31, Some OO Problems/Issues
[To Lecture Notes Index]
San Diego State University -- This page last updated Dec 5, 1996
Contents of Doc 31, Some OO Problems/Issues
- References
- Some OO Problems/Issues
- Yoyo Problem1
- Variables Limit Reusability2
- Memory Management
- Code Management
- Documentation
- Diagrams
Budd, An Introduction to Object-Oriented Programming,, 1991
Cockburn, The Interaction of Social Issues and Software Architecture,
Communications of the ACM, vol. 39, No 10 pp 40-46, October 1996
Taenzer, Ganti, and Podar, Object-Oriented Software Reuse: The Yoyo Problem,
Journal of Object-Oriented Programming, 2(3): pp 30-35, 1989
Allen Wirfs-Brock and Brian Wilkerson, Variables Limit Reusability, Journal
of Object-Oriented Programming, 2(1): pp 34-40, 1989
Yoyo problem
Variables limit reuse
Memory management
Code management
Documentation
abstract class One {
public void A() { print( "A, 1" ); B(); C(); }
public void C() { print( "C, 1" ); }
abstract public void B();
abstract public void D();
public void print( String message ) {
System.out.println( message );
}
}
class Two extends One {
public void B() { print( "B, 2" ); D();}
public void D() { print( "D, 2" ); }
}
class Three extends Two {
public void A() { print( "A, 3" ); super.A(); }
public void B() { print( "B, 3" ); super.B(); }
}
class Four extends Three {
public void A() { print( "A, 4" ); super.A(); }
public void C() { print( "C, 4" ); super.C(); }
}
class Five extends Four {
public void D() { print( "D, 5" ); super.D(); }
}
Yoyo Problem
What is the result of the following program?
public static void main( String args[] ){
Five yoyo = new Five();
yoyo.A();
}
Control Flow Trace for Function A in Class Five
Output of Program
A, 4 D, 5
A, 3 D, 2
A, 1 C, 4
B, 3 C, 1
B, 2
Direct access to fields of a class limits the ability to modify a class
Example:
class test
{
public void foo()
{
value[20] = value[10] + 5;
}
public void bar() { stuff not shown };
private float value[] = new float[ 100 ];
}
If one changes "value" to a linked list, then all methods accessing "value"
must be modified
All subclasses also must be changed
Variables Limit Reusability
Solution: Use accessor methods
class test
{
public void foo()
{
setValue(20, getValue(10) + 5);
}
public void bar() { stuff not shown };
protected float getValue(int location)
{ return value[location]; }
float test::setValue(int location, float newValue)
{ value[location] = newValue; };
private float value[] = new float[ 100 ];
}
Some argue that all methods in class test should use accessor methods to access
fields of test
Some argue that methods in class test can access fields of test (value)
directly, but all subclasses of test must use accessor methods to access
fields of test
Complication: Complex fields!
If a field is a complex object with multiple operations, then how many
operations do we need to access the field?
If Course has 20 methods does GraderProgram need 20 accessor methods to access
activeCourse?
class GraderProgram
{
Course activeCourse = new Course();
// Accessor methods for activeCourse
protected void addStudent( String name, String ID )
{
activeCourse.addStudent( name, ID );
}
protected void someMethodInGraderProgram()
{
lots of stuff here
addStudent( name, ID );
}
Design Principle[3]Variation Behind
Interface
Intent
- Protect the system integrity across changes
Force
- Things change
Principle
-
- Create an interface around predicted points of variation
Counterforce
- Too many interfaces means complex, slow software
Object-Oriented programs tend to make heavy use of dynamic memory allocation
- class A { ... };
-
- A myVariable = new A();
-
- myVariable = new A();
Memory leaks
- C++ programs suffer from memory leaks
-
- Use destructors to release memory from objects
-
- Provide programmers with memory leak detectors
-
Stresses memory manager
- Program performance can suffer
Operations
- Average size of an operation in OO program is smaller than in a procedural
language
-
- OO programs contain more operations than procedural programs
Files
- C++ utilizes two files per class
- Java utilizes one file per class
-
- Inherited information is spread across multiple files
Things needing documentation
Variables | Classes |
Statements | Class relationships |
Operations | | inheritance |
State | | collaborations |
Public interface | Objects |
Private operations and state | Object relations |
Contracts | Subsystems |
Programs | |
Views
Inside verses outside
Static verses dynamic
Analysis, design, code
Documentation
Variables
Statements
Methods
State
-
- Require same type of documentation as in functional programming
Classes: three views
-
- User of class
-
- Subclass
-
- Designer/implementor/maintainer
Public interface of a class or subsystem
- Need to know services rendered, not implementation
Private operations and state
- Documentation is for implementor and maintainer
Template for Documenting a Class
Name: identifier
Documentation: text description
Visibility: exported/private/imported
Cardinality: 0/1/n
Hierarchy
Superclasses: list of class names
Subclasses: list of class names
Generic parameters: for template classes
Public Members
Uses: list of classes used by me
Data members: list of data members & text description
Function members: list of function declarations
Protected members
Data members: (see above)
Function members: (see above)
Private Members
Data members: (see above)
Function members: (see above)
Subclass must implement
Function members: (see above)
Hierarchy graph: location of graph
State machine graph: graph(s)
Complexity
Space: text description
Time: text description
Persistence: persistent/transitory
Concurrency: sequential/concurrent
Template for Documenting an Operation
Name: identifier
Documentation: text description
Category: modifier/selector/iterator/
constructor/destructor
Inheritance: new/refine/override
Formal Parameters: parameter declarations
Return value: type/class name
Preconditions: PDL
Action: PDL
Postconditions: PDL
Exceptions: list of exceptions
Concurrency: sequential/concurrent
Complexity
Space: text description
Time: text description
PDL - program description language
Class Collaborations
Association Ternary Association
Multiplicity of Associations Link Attribute
Aggregation
Messages
Diagrams
Event Flow Diagram
Object Diagrams
Diagrams for Dynamic Behavior
Event Trace Diagram
State Diagram of Car Transmission
Diagrams for Dynamic BehaviorConcurrency
Petri Net