Friday, January 24, 2014

Interpreter Design Pattern

The interpreter pattern is used to interpret a passed-in object using a set of definitions.

//The interpreter interface
public interface StringInterpreter {
      public void interpret(String input);
}

//The interpreter class
public class CharToNumber implements Interpreter {
 
      public void interpret(String input) {
            String result = "";
            for (int i=0; i < input.length(); i++) {
                  char c = input.charAt(i);
                  if (c == 'A') {
                        result += "1";
                  } else if (c=='B') {
                        result += "2"
                  } else {
                         result += "9";
                  }
            }
      }    
}

//The Runner
public class interpterDemo {
      public static void main (String[] args) {
            Interpreter inter = new CharToNumber();
            inter.interpret("ABABAABBCCCC");
      }
}

----------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

No comments:

Post a Comment