- Forward


Outbound Parameters
A Programming Pattern in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation
Back SMYC Forward
  • An Observation:
    • In Java, a method can only return a single primitive value or reference and this can be inconvenient
  • An Example:
    • A method that needs to return the minimum and maximum of an array
  • Obvious Solutions:
    • Return an array
    • Create a Range class that has two attributes and return an instance of it
  • An Alternative Solution:
    • Use a parameter instead
Review
Back SMYC Forward
  • Variable Types:
    • Primitive types
    • Reference types
  • Parameter Passing:
    • All parameters in Java are passed by value (i.e., a copy is made)
Thinking About the Problem
Back SMYC Forward
  • A First (Incorrect) Thought:
    • Since parameters are passed by value it is impossible to have outbound parameters
  • A Deeper Thought:
    • A copy of a reference is an alias, so mayber reference types can be used
Thinking About the Problem
Back SMYC Forward
  • Three Situations:
    • The parameter is a mutable reference type
    • The parameter is a primitive type
    • The parameter is an immutable reference type
  • Handling These Situations:
    • When the parameter is mutable, the method can change its attributes to "return" information
    • When the parameter is a primitive type or immutable, you can wrap it
Thinking About the Problem (cont.)
Back SMYC Forward

A Mutable Parameter

javaexamples/programmingpatterns/outboundparameters/Range.java
 
Thinking About the Problem (cont.)
Back SMYC Forward

A Wrapper for an Immutable Reference Type

javaexamples/programmingpatterns/outboundparameters/ColorWrapper.java
 
The Pattern
Back SMYC Forward
  • The Simplest Version:
    • 1. Create a wrapper class if necessary
    • 2. Declare a method with an appropriate signature
    • 4. Perform the necessary operations in the body of the method
    • 5. Modify the attributes of the outbound parameter
  • Using this Version:
    • The invoker of the method must then construct an "empty" instance of the outbound parameter and pass it to the method
The Pattern
Back SMYC Forward
  • An Improvement:
    • Allow the invoker to indicate whether the method should construct the outbound parameters
  • The Improved Version:
    • 3. At the top of the method, check to see if the outbound parameter is null and, if it is, construct an instance of the outbound parameter
    • 6. Return the outbound parameter
  • Using this Version:
    • The invoker of the method must then construct an "empty" instance of the outbound parameter and pass it to the method
Examples
Back SMYC Forward

Outbound Arrays

javaexamples/programmingpatterns/outboundparameters/OutboundParameters.java (Fragment: extremes)
 
Examples (cont.)
Back SMYC Forward

Outbound Mutable Objects

javaexamples/programmingpatterns/outboundparameters/OutboundParameters.java (Fragment: extrema)
 
Examples (cont.)
Back SMYC Forward

Outbound Primitive Types

javaexamples/programmingpatterns/outboundparameters/OutboundParameters.java (Fragment: summarize)
 
Examples (cont.)
Back SMYC Forward

Outbound Immutable Objects

javaexamples/programmingpatterns/outboundparameters/OutboundParameters.java (Fragment: purpleOut)
 
There's Always More to Learn
Back -