您的位置:首页 > 其它

策略模式的扩展——策略枚举

2011-06-28 12:48 141 查看
策略枚举就是这样一个枚举:

它是一个枚举。

它是一个浓缩了的策略模式的枚举。

public enum Calculator {
//加法运算
ADD("+"){
public int exec(int a,int b){
return a + b;
}
},

//减法运算
SUB("-"){
public int exec(int a,int b){
return a - b;
}
};

String value = "";

//定义成员值类型
private Calculator(String _value){
this.value = _value;
}

//获得枚举成员的值
public String getValue(){
return this.value;
}

//声明一个抽象函数
public abstract int exec(int a,int b);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: