解释器模式(interpreter)

来一个大写小转换的例子:

Expression.java

package com.prl.patterns.Interpreter;

import java.util.LinkedHashMap;
import java.util.Map;

public abstract class Expression {
	protected Map<String,Integer> table = new LinkedHashMap<String,Integer>();
	
	public Expression(){
		table.put("一",1);
		table.put("二",2);
		table.put("三",3);
		table.put("四",4);
		table.put("五",5);
		table.put("六",6);
		table.put("七",7);
		table.put("八",8);
		table.put("九",9);
	}
	
	public void interpret(Context context){
		String statment = context.getStatment();
		if(statment.length()<= 0){
			return;
		}
		
		for(Map.Entry<String,Integer> entry:table.entrySet()){
			statment = context.getStatment();
			
			String key = entry.getKey();
			int value = entry.getValue();
			//System.out.println("key="+key+",value="+value);
			String endfix = key+getPostfix();
			System.out.println("endfix:"+endfix);
			if(statment.endsWith(endfix)){
				int newData = context.getData() + value*getMultiplier();
				String newStatement = context.getStatment().substring(0, context.getStatment().length()-getLenth());
				context.setData(newData);
				context.setStatment(newStatement);
				System.out.println("newData:"+newData+",newStatement="+newStatement);
				
				if(context.getStatment().endsWith("零")){
					context.setStatment(context.getStatment().substring(0, context.getStatment().length()-1));
				}
				break;
			}
		}		
		
		
	}
	
	public abstract String getPostfix();
	public abstract int getMultiplier();
	public int getLenth(){
		return this.getPostfix().length() + 1;
	}
}

 GeExpression.java

package com.prl.patterns.Interpreter;

public class GeExpression extends Expression {

	@Override
	public int getMultiplier() {
		return 1;
	}

	@Override
	public String getPostfix() {		
		return "";
	}
}

 

SiExpression.java

package com.prl.patterns.Interpreter;

public class SiExpression extends Expression {

	@Override
	public int getMultiplier() {
		return 10;
	}

	@Override
	public String getPostfix() {		
		return "十";
	}

}

 

BaiExpression.java

package com.prl.patterns.Interpreter;

public class BaiExpression extends Expression {

	@Override
	public int getMultiplier() {
		return 100;
	}

	@Override
	public String getPostfix() {		
		return "百";
	}
}

 

QianExpression.java

package com.prl.patterns.Interpreter;

public class QianExpression extends Expression {

	@Override
	public int getMultiplier() {
		return 1000;
	}

	@Override
	public String getPostfix() {		
		return "千";
	}
}

 

WanExpression.java

package com.prl.patterns.Interpreter;

import java.util.ArrayList;
import java.util.List;

public class WanExpression extends Expression {

	@Override
	public int getMultiplier() {
		return 10000;
	}

	@Override
	public String getPostfix() {		
		return "万";
	}

	@Override
	public void interpret(Context context) {		
	if (context.getStatment().length() == 0)
        return;

        List<Expression> tree = new ArrayList<Expression>();
        tree.add(new GeExpression());
        tree.add(new SiExpression());
        tree.add(new BaiExpression());
        tree.add(new QianExpression());
        
        if (context.getStatment().endsWith(this.getPostfix()))
        {
            int temp = context.getData();
            context.setData(0);
            context.setStatment(context.getStatment().substring(0,context.getStatment().length()-1));
            for(Expression exp:tree)
            {
                exp.interpret(context);
            }
            context.setData(temp+this.getMultiplier()*context.getData());
        }

	}
}

 

 

下面是主程序 :

package com.prl.patterns.Interpreter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class App {
	protected Map<String,Integer> table = new HashMap<String,Integer>();
	
	public void initApp(){
		table.put("一",1);
		table.put("二",2);
		table.put("三",3);
		table.put("四",4);
		table.put("五",5);
		table.put("六",6);
		table.put("七",7);
		table.put("八",8);
		table.put("九",9);
	}
	
	public void test(){
		initApp();
		String roman = "二十三万六千四百零五十二";
		
		Context context = new Context(roman);
		
		List<Expression> tree = new ArrayList<Expression>();
		tree.add(new GeExpression());
		tree.add(new SiExpression());
		tree.add(new BaiExpression());
		tree.add(new QianExpression());
		tree.add(new WanExpression());	
		
		for(Expression exp:tree)
        {
            exp.interpret(context);
        }
		System.out.println(roman+"="+context.getData());
	}
	
	public static void main(String[] args) {
		App app = new App();
		app.test();
	}

}

 

你可能感兴趣的:(java)