Method Design Patterns
© 2003 Curtis Sollohub


Algorithmic Patterns Introduction Class Design Patterns

III. Method Design Patterns

There are three steps to successfully designing a method:

  1. defining the purpose of the method;
  2. deciding on the receives and returns;
  3. developing an algorithm.

The overall program design determines the purpose of each individual method. In this section we consider patterns for determining the receives and returns of methods. In some cases simple algorithms are also included. It turns out that, in most cases, the purpose of a method determines the rest. In other words, by matching the patterns below with the purpose of a method you are designing, you can save yourself a lot of work in designing your mehtods. And, you can feel more secure that your design is correct.

Note that the purpose statements below are very general. When designing a method, your purpose statements should be more specific, for example:

  • To input the cost of an item in the inventory.

  • To output the discounted price;

  • To calculate an employee's wage.

However, when you go to match the purpose statement of your method with the purpose's listed here, ignore the extra details you have provided. In other words, if your purpose statement includes the word 'input' or 'get', you probably should work with pattern A below. Likewise, if your purpose statement includes the word 'calculation' it should consider pattern C.

Once you have finished designing your methods, you may want to use the algorithmic patterns to help develop an algorithm or write the code. This is especially true if the method is at all complex. Simply try to match the purpose of the method with the purpose statements provided with each algorithmic pattern.
 

Pattern A: Basic Input

    Purpose: To input or get some value(s) from a user

    Goal: The appropriate variable(s) contain the value(s)

    Receives: Nothing

    Returns the variable

    Algorithm: Get the value(s) for the variable(s) from the user
                    // Consider Using algorithmic pattern A1.


Pattern B: Basic Output

    Purpose: To output some value(s)

    Goal: The screen displays the value(s)

    Receives: the value(s)

    Returns NONE

    Algorithm: Output the value(s)


Pattern C: Calculations

    Purpose: To perform some calculation

    Goal: Some variable holds the result of the calculation

    Receives: the value(s) needed for the calculation

    Returns: the value calculated


Pattern D: Message Display

    Purpose: To output a message

    Goal: The message is displayed

    Receives: None

    Returns: None

    Algorithm: Display the message.



Top of Section Algorithmic Patterns Class Design Patterns