Eliza 2 - Thinking objectively (object oriented programming)

by Ravi Bhavnani


In this version of Eliza, we'll concentrate on solving the 2 hard problems we conveniently stubbed earlier:

    Pattern FindMatchingPatternForInput
        (string input)
    {
        // Stubbed for now.  We'll figure out what it does later.
        // For now just return a Pattern.
        return new Pattern();
    }

    string GenerateResponseForPattern
        (Pattern pattern,
         string input)
    {
        // Stubbed for now.
        return string.Format("You said \"{0}\".", input);
    }

Both these problems have to do with a central theme: a "pattern". Rather than writing code, let's first think about what a pattern looks like (i.e. how it's described) and how it behaves.

In C#:

  • "What does it look like?"
    means "What are this object's properties?"

  • "How does it behave?"
    means "What are this object's methods?"

Properties and methods of a real-world object

If you tried to come up with a representation of a car (instead of a pattern), you'd probably come up with this object definition:

  • Class: Car

  • Properties:
    Name Example values
    Make Honda, Toyota, BMW, etc.
    Model CRX, RAV-4, 325, etc.
    Year 2005, 2017, etc.
    Number of doors 2, 4
    Is SUV Yes, No
    Number of cylinders 4, 6, 8
    Transmission Manual, Automatic
    Is all wheel drive Yes, No
    Top speed (mph) 90, 100, 120, 135
    Is engine running Yes, No
    Name of current gear P, R, N, D, 1, 2, 3, 4

  • Methods:
    • void StartEngine()
    • void TurnOffEngine()
    • void SelectGear(string gearName)
    • void TurnSteeringWheel (int numDegrees)
    • void PressGasPedal (double inchesToPress)
    • void PressBrakePedal (double inchesToPress)
    • int GetCurrentSpeedInMph()

STOP!  DON'T READ ANY FURTHER.
TO HELP DEFINE THE Pattern CLASS, WALK AWAY FROM YOUR COMPUTER AND THINK ABOUT WHAT A PATTERN DOES.  MAKE SOME NOTES ON A PIECE OF PAPER.  WHEN YOU'RE DONE, COME BACK AND COMPARE YOUR NOTES WITH THE ANALYSIS BELOW.

Properties and methods of a pattern

Clearly a pattern is something we try to match the user's input to. If the user input matches a pattern, we ask the pattern to generate an appropriate response. For example, let's consider a pattern that checks if the user's input contains the word "BECAUSE".

If it does, the pattern should respond with one of the following human-like replies:

  • Is that the real reason?
  • Don't any other reasons come to mind?
  • Does that reason seem to explain anything else?
  • What other reasons might there be?

From this analysis, we can deduce that a pattern revolves around a key phrase ("BECAUSE" in the above example). A pattern can be asked if it a user's input matches the phrase, and if it does, the pattern can be asked to generate a human-like response.

In other words, a pattern has the following property:

    string PhraseToMatch {
        get;
        set;
    }

and the following methods:

    bool MatchesInput
        (string input)
    {
        // To be written...
    }

    string GenerateResponse()
    {
        // To be written...
    }

First version of the Pattern class

Looks like we're ready to write (at least the first version of) the Pattern class in C#. Take a stab at this.

Feel free to leave out the hard part (the internals of the methods) by stubbing them for now. We're not being lazy - we're just delaying writing the details of the methods until we've broken them down into a few simpler methods. Good old stepwise refinement!

STOP - DON'T READ ANY FURTHER.  WRITE THE Pattern CLASS BY USING Pattern.cs AS A STARTING POINT.  STUB OUT THE INTERNALS OF THE METHODS, BUT ENSURE YOUR PROGRAM CONTINUES TO COMPILE AND RUNS CORRECTLY.  THEN COME BACK TO THIS PAGE.

When you've finished writing the first version of Pattern, ask yourself these questions and update your Pattern class if necessary:

  • How do I create (i.e. construct) an instance of a Pattern?
  • Do the properties and methods of Pattern have the right access modifiers?
  • Which properties/methods should be static?
  • Upon constructing a Pattern, is the instance I constructed guaranteed to always be valid? (i.e. can a constructed Pattern ever fall into an invalid state?)

NOW COMPARE YOUR WORK TO THE Pattern CLASS IN THE SOLUTION.  HOW DID YOU DO?
 

Most of the content at this site is copyright © Ravi Bhavnani.
Questions or comments?  Send mail to ravib@ravib.com