Design Pattern-interpreter Pattern

Source: Internet
Author: User
Why use interpreter mode?

In some cases, we need to explain the class content. For example, we need to set "What day of the week ?" Translation into English, Japanese, and Thai respectively. The Interpreter mode is required.

What is interpreter mode?

Explains the content of a class and has multiple classes.

How to Use interpreter Mode

UML

Code:

Translate. Class

 
Public abstract class translate {public abstract void translate (context );}

Japan. Class

 
Public class Japanese extends translate {@ overridepublic void translate (context) {// todo auto-generated method stubsystem. out. println ("put" + context. getstr () + "translated into Japanese. ");}}

English. Class

 
Public class English extends translate {@ overridepublic void translate (context) {// todo auto-generated method stubsystem. out. println ("put" + context. getstr () + "translated into English. ");}}

Thai. Class

 
Public class Thai extends translate {@ overridepublic void translate (context) {// todo auto-generated method stubsystem. out. println ("put" + context. getstr () + "translated into Thai. ");}}

Context. Class

 
Public class context {private string STR; public context (string Str) {This. setstr (STR);} Public String getstr () {return STR;} public void setstr (string Str) {This. STR = STR ;}}

Client. Class

 
Public class client {public static void main (string [] ARGs) {context = new context ("what is today's day of the week? "); Translate = new Japanese (); translate. translate (context); translate = new English (); translate. translate (context); translate = new Thai (); translate. translate (context );}}

Final running result:

What day is today? Translated into Japanese.
What day is today? Translated into English.
What day is today? Translated into Thai.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.