Design Pattern---interpreter mode

Source: Internet
Author: User

The usual scenarios for interpreter mode are calculators, or formulas.

That is to say, the symbol of interpretation satisfies the element of the formula, and the client side replaces the contents of each element.

 Package com.jayfulmath.designpattern.experssion; Import Java.util.HashMap;  Public Abstract class experssion {    // parse formulas and values where the key value in Var is the parameter in the formula, and the value is the specific number     Public Abstract int Interpreter (hashmap<string, integer> var);}
 Packagecom.jayfulmath.designpattern.experssion;ImportJava.util.HashMap; Public classVarexperssionextendsexperssion {PrivateString Key; @Override Public intInterpreter (Hashmap<string, integer>var) {System.out.println ("Varexperssion Interpreter key:" +key); returnVar.get (key); }     Publicvarexperssion (String key) {Super();  This. Key =key; }        }
 public  abstract  class  symbolexperssion     experssion {experssion left;    Experssion right;  public   Symbolexperssion (experssion        Left, experssion right) { super   ();          left;     this . Right =
 import   Java.util.HashMap;  public  class     Addexperssion extends   Symbolexperssion {  public   Addexperssion (Experssion left,            Experssion right) { super   (left, right); } @Override  public  int  in Terpreter (hashmap<string, Integer> Var) { return   .right.interpreter (Var); }    }
 Packagecom.jayfulmath.designpattern.experssion;ImportJava.util.HashMap; Public classSubexperssionextendssymbolexperssion { PublicSubexperssion (experssion left, experssion right) {Super(left, right); } @Override Public intInterpreter (Hashmap<string, integer>var) {        return Super. Left.interpreter (Var)-Super. Right.interpreter (Var); }}

Design Pattern---interpreter mode

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.