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.