Java Design Pattern modeling and implementation of cainiao series (20) interpreter Pattern

Source: Internet
Author: User

Java Design Pattern modeling and implementation of cainiao series (20) interpreter Pattern

 

Interpreter mode (Interpreter): defines the value obtained after an operation between an object and an object. Generally, it is mainly used in the development of compilers in OOP development, so it has a narrow application scope.

I. uml modeling:

 

Ii. Code Implementation

 

/*** Interpreter mode (Interpreter): defines the value obtained after an operation between an object and an object. ** Generally, it is mainly used in the development of compilers in OOP development, so it has a narrow application scope. ** Example: first define an Entity class, encapsulate two variables num1, num2 */class Entity {private double num1; private double num2; public Entity (double num1, double num2) {this. num1 = num1; this. num2 = num2;} public double getNum1 () {return num1;} public void setNum1 (double num1) {this. num1 = num1;} public double getNum2 () {return num2;} public void setNum2 (double num2) {this. num2 = num2 ;}/ *** operation interface */interface Operatable {public double interpreter (Entity entity );} /*** addition operation */class AddOperation implements Operatable {@ Overridepublic double interpreter (Entity entity) {return entity. getNum1 () + entity. getNum2 () ;}/ *** subtraction operation */class MinusOperation implements Operatable {@ Overridepublic double interpreter (Entity entity) {return entity. getNum1 ()-entity. getNum2 () ;}}/*** client Test class ** @ author Leo */public class Test {public static void main (String [] args) {/*** create addition and subtraction operations */AddOperation addOperation = new AddOperation (); MinusOperation minusOperation = new MinusOperation (); /*** 1. Step-by-step operation */double addResult = addOperation. interpreter (new Entity (20, 30); double minusResult = minusOperation. interpreter (new Entity (20, 30); System. out. println (addResult = + addResult); System. out. println (minusResult = + minusResult);/*** 2. Hybrid Operation */double mixResult = new AddOperation (). interpreter (new Entity (addOperation. interpreter (new Entity (20, 30), minusOperation. interpreter (new Entity (40, 50); System. out. println (mixResult = + mixResult );}}

 

Iii. Summary

 

The interpreter mode is used for various interpreters, such as the interpreter of a regular expression.

 

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.