Java design mode interpreter mode __java design pattern

Source: Internet
Author: User


Step one: Create an Interpreter interface (where an auxiliary parameter is passed in, so we first create an auxiliary class context, optional)

Public class Context {
    private int num1;;
    private int num2;
	public int getNum1 () {return
		num1;
	}
	public void setNum1 (int num1) {
		this.num1 = num1;
	}
	public int getNum2 () {return
		num2;
	}
	public void setNum2 (int num2) {
		this.num2 = num2;
	}
	public context (int num1, int num2) {
		super ();
		THIS.NUM1 = NUM1;
		this.num2 = num2;
	}

Public interface Expression {public
     int interpret;
	}

Step two: Build the interpreter specific implementation class

public class minus implements expression{the public

	int interpret (context context) {return
		context.getnum1 ()- Context.getnum2 ();
	}


Step Three: Test

public class Test {public
    static void Main (string[] args) {
		int result= new minus (). Interpret (new context (5, 5) );
		System.out.println ("5-5=" +result);
	}


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.