Context

  1. Input data that is to be processed sequentially.
  2. Each element of the input is to be processed as it is input, but when certain elements appear in a certain order, then that subsequence of the elements is to be processed as a group.
Solution
  1. Define a class Group that represents a group of one or more input elements.
  2. Define a class Grouper that can be used to form groups of input data.
  3. Grouper must have a reference to the source of the input so that it can access each element of the input in sequence.
  4. Grouper must have methods that return the current and next group of data elements and that determine if any more groups remain in the input. Call these methods currentGroup(), nextGroup(), and hasNext(), respectively.
Example applications:
  1. Counting letters and diphthongs in a text. For example, the count of "the heat" would be
     
    'a' 1
    'e' 2
    'h' 1
    't' 1
    'th' 1
    

  2. Transliterating alphabets (eg transliterate English letters to the representation of Greek characters in the word processing system TeX):
     
       transliterate("sophos") 
    
    would return
    \sigma\omicron\phi\omicron\sigma
    

The pattern could be extended to use the template or strategy pattern to generalize how the data groups are processed.