Java design patterns - Interpreter mode

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/cblstc/article/details/93735996

Article Directory

background

Modification can change shape, and further each property has changed. Combining the plurality of body assembly, it becomes more powerful. The case body and the deformation simulation arithmetic, to achieve the interpreter mode.

achieve

Expressions Interface

public interface Expression {

	String interpreter();
	
}

End of expression: represents the name of the body

/**
 * 终结符:代表着机体的名称
 */
public class RobotTerminalExpression implements Expression {
	
	private String name;
	
	public RobotTerminalExpression(String name) {
		super();
		this.name = name;
	}

	@Override
	public String interpreter() {
		return name;
	}

}

Non-terminal expression: Deformation

/**
 * 非终结符:变形操作
 */
public class TransformNonTerminalExpression implements Expression {
	
	private Expression exp;

	public TransformNonTerminalExpression(Expression exp) {
		super();
		this.exp = exp;
	}

	public Expression getExp() {
		return exp;
	}

	public void setExp(Expression exp) {
		this.exp = exp;
	}

	@Override
	public String interpreter() {
		String transformTo = "";
		switch (exp.interpreter()) {
		case "R1":
			transformTo = "R-Wing";
			break;
		case "野生兽":
			transformTo = "野生兽(FM)";
			break;
		default:
			transformTo = "非法的变形操作!";
			break;
		}
		
		return transformTo;
	}

}

Non-terminal expression: fit

/**
 * 非终结符-》合体操作
 */
public class CombineNonTerminalExpression implements Expression {
	
	private Expression exp1;
	
	private Expression exp2;
	
	private Expression exp3;
	
	public CombineNonTerminalExpression(Expression exp1, Expression exp2) {
		super();
		this.exp1 = exp1;
		this.exp2 = exp2;
	}

	public CombineNonTerminalExpression(Expression exp1, Expression exp2, Expression exp3) {
		super();
		this.exp1 = exp1;
		this.exp2 = exp2;
		this.exp3 = exp3;
	}

	@Override
	public String interpreter() {
		if ("R1".equals(exp1.interpreter()) && "R2".equals(exp2.interpreter()) && "R3".equals(exp3 == null? "" : exp3.interpreter())) {
			return "SRW";
		} else if ("Gunner".equals(exp1.interpreter()) && "晓击霸MKII-R".equals(exp2.interpreter())) {
			return "MKII-Gunner";
		} else {
			return "非法的合体操作!";
		}
	}

}

test

public class Test {

	public static void main(String[] args) {
		// 终结符
		Expression ancientIronExpression = new RobotTerminalExpression("古铁");
		Expression r1Expression = new RobotTerminalExpression("R1");
		Expression r2Expression = new RobotTerminalExpression("R2");
		Expression r3Expression = new RobotTerminalExpression("R3");
		Expression gunnerExpression = new RobotTerminalExpression("Gunner");
		Expression mkiiExpression = new RobotTerminalExpression("晓击霸MKII-R");
		Expression wildExpression = new RobotTerminalExpression("野生兽");
		
		// 测试变形
		TransformNonTerminalExpression transformExpression = new TransformNonTerminalExpression(ancientIronExpression);
		System.out.println("古铁 变形->" + transformExpression.interpreter());
		transformExpression.setExp(r1Expression);
		System.out.println("R1 变形->" +transformExpression.interpreter());
		transformExpression.setExp(wildExpression);
		System.out.println("野生兽 变形->" +transformExpression.interpreter());
		
		System.out.println("-----------------");
		// 测试合体
		CombineNonTerminalExpression combineExpression = new CombineNonTerminalExpression(gunnerExpression, mkiiExpression);
		System.out.println("Gunner 合体  晓击霸MKII-R->" + combineExpression.interpreter());
		combineExpression = new CombineNonTerminalExpression(r1Expression, r2Expression, r3Expression);
		System.out.println("R1 合体  R2 合体 R3->" + combineExpression.interpreter());
		combineExpression = new CombineNonTerminalExpression(r1Expression, r2Expression);
		System.out.println("R1 合体  R2->" + combineExpression.interpreter());
	}
	
}

Test Results

古铁 变形->非法的变形操作!
R1 变形->R-Wing
野生兽 变形->野生兽(FM)
-----------------
Gunner 合体  晓击霸MKII-R->MKII-Gunner
R1 合体  R2 合体 R3->SRW
R1 合体  R2->非法的合体操作!

to sum up

Finally, a design pattern 22 - interpretation mode, but also in a more difficult to understand, in order not to use the operator of this example, I thought fit and deformation operations of the body, as it approximation operator, may not be so suitable examples .

Article 22 is actually driven out intermittently within a month's time, nor to go into specific scenarios and the advantages and disadvantages of each mode, these articles will continue to modify and improve, wrong with the inevitable, I hope Tell me critical of attitude towards these articles.

If you have not played the game, some episodes may not have resonates.
This is an innovative version you have not played, just to experience three minutes, you'll like me fall in love with the game - by zzh

Guess you like

Origin blog.csdn.net/cblstc/article/details/93735996