- Forward


Indicator Methods
A Programming Pattern


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation
Back SMYC Forward
  • The Idea:
    • Problems in which an indicator’s value must be calculated before it can be used in another expression
  • An Example:
    • "The show must go on" (i.e., the number of seats sold for a particular show time must be used to calculate an indicator, setting it to 0 when no tickets have been sold and setting it to 1 otherwise)
Review
Back SMYC Forward
  • A Specific Solution that Leads to Code Duplication:
    • javaexamples/programmingpatterns/IndicatorMethods.java (Fragment: review0)
       
  • A Better Specific Solution:
    • javaexamples/programmingpatterns/IndicatorMethods.java (Fragment: review1)
       
The Pattern
Back SMYC Forward
  • A General Solution for Threshold Indicators:
    • javaexamples/programmingpatterns/IndicatorMethods.java (Fragment: indicator)
       
  • Other General Solutions:
    • The parameters of the method and the logic in the body of the method vary with the problem
Examples of Threshold Indicators
Back SMYC Forward
  • The Show Must Go On:
    • javaexamples/programmingpatterns/IndicatorMethods.java (Fragment: shows)
       
  • Parking Tickets:
    • javaexamples/programmingpatterns/IndicatorMethods.java (Fragment: parking)
       
Another Example
Back SMYC Forward
  • Basic Metabolic Rate (BMR):
    • \(b = 5.00 + 10.00 m + 6.25 h - 5.00 a - 161.00 \delta_f \)
  • The Indicator Method:
    • javaexamples/programmingpatterns/IndicatorMethods.java (Fragment: female)
       
  • Using the Indicator Method:
    • javaexamples/programmingpatterns/IndicatorMethods.java (Fragment: bmr)
       
A Warning
Back SMYC Forward
  • Don't Overgeneralize:
    • This pattern is often not appropriate for boolean methods
  • An Inappropriate Use of the Pattern:
    • javaexamples/programmingpatterns/IndicatorMethods.java (Fragment: old0)
       
  • The Appropriate Solution:
    • javaexamples/programmingpatterns/IndicatorMethods.java (Fragment: old1)
       
There's Always More to Learn
Back -