public boolean passwordRequired() {
return false;
}
}
GetMethod.java
package com.zj.subclass;
public class GetMethod extends Method{
@Override
public String getMethod() {
return "This is GET method";
}
@Override
public boolean safeMethod() {
return true;
}
}
PutMethod.java
package com.zj.subclass;
public class PutMethod extends Method {
@Override
public String getMethod() {
return "This is PUT method";
}
}
DelMethod.java
package com.zj.subclass;
public class DelMethod extends Method{
@Override
public String getMethod(){
return "This is DELETE method";
}
}
使用面向对象技术替代switch-case和if-else(3)
时间:2011-03-23 zhangjunhd
2.使用state模式
如果希望对象在生存周期内,可以变化自己的状态,则可以选择state模式。
这里抽象状态为一个接口MethodType,四种不同的状态实现该接口。
<<interface>>MethodType.java
package com.zj.state;
public interface MethodType {
String getTypeDescription();
String getMethodDescription();
boolean isSafe();
boolean isRequired();
}
Post.java
package com.zj.state;
public class Post implements MethodType{
public String getMethodDescription() {
return "This is POST method";
}
public String getTypeDescription() {
return "===POST===";
}
public boolean isRequired() {
return false;
}
public boolean isSafe() {
return false;
}
}
Get.java
package com.zj.state;
public class Get implements MethodType{
public String getMethodDescription() {
return "This is GET method";
}
public String getTypeDescription() {
return "===GET===";
}
public boolean isRequired() {
return true;
}
public boolean isSafe() {
return true;
}
}
Put.java
package com.zj.state;
public class Put implements MethodType{
public String getMethodDescription() {
return "This is PUT method";
}
public String getTypeDescription() {
return "===PUT===";
}
public boolean isRequired() {
return true;
}
public boolean isSafe() {
return false;
}
}
Delete.java
package com.zj.state;
public class Delete implements MethodType{
public String getMethodDescription() {
return "This is DELETE method";
}
public String getTypeDescription() {
return "===DELETE===";
}
public boolean isRequired() {
return true;
}
public boolean isSafe() {
return false;
}
}
使用面向对象技术替代switch-case和if-else(4)
时间:2011-03-23 zhangjunhd
此时,在类Method中保存一个field表示MethodType,在某对象中,可以随时变化四种已知的状态(具体见runAllMethods()方法)。
Method.java
package com.zj.state;
public class Method {
private MethodType _type;
public Method() {
_type = null;
|