}
public Method(MethodType type) {
_type = type;
}
public String getMethod() {
return _type.getMethodDescription();
}
public boolean safeMethod() {
return _type.isSafe();
}
public boolean passwordRequired() {
return _type.isRequired();
}
public void changeType(MethodType type) {
_type = type;
}
public void runAllMethods() {
MethodType[] types = new MethodType[] { new Post(), new Get(),
new Put(), new Delete() };
for (MethodType type : types) {
System.out.println(type.getTypeDescription());
changeType(type);
System.out.println(getMethod());
System.out.println(safeMethod());
System.out.println(passwordRequired());
}
}
}
3.测试
在测试类中,分别使用上面3中机制展示结果。它们的结果应该是一致的。
Client.java
package com.zj.utils;
public class Client {
static void print(String s) {
System.out.println(s);
}
static void print(Boolean b) {
System.out.println(b);
}
public static void main(String[] args) throws NoMethodTypeException {
print("===original===");
print("===POST===");
com.zj.original.Method post1 = new com.zj.original.Method(
com.zj.original.Method.POST);
print(post1.getMethod());
print(post1.safeMethod());
print(post1.passwordRequired());
print("===GET===");
com.zj.original.Method get1 = new com.zj.original.Method(
com.zj.original.Method.GET);
print(get1.getMethod());
print(get1.safeMethod());
print(get1.passwordRequired());
print("===PUT===");
com.zj.original.Method put1 = new com.zj.original.Method(
com.zj.original.Method.PUT);
print(put1.getMethod());
print(put1.safeMethod());
print(put1.passwordRequired());
print("===DELETE===");
com.zj.original.Method del1 = new com.zj.original.Method(
com.zj.original.Method.DELETE);
print(del1.getMethod());
print(del1.safeMethod());
print(del1.passwordRequired());
print("===subclass===");
print("===POST===");
com.zj.subclass.Method post2 = com.zj.subclass.Method
.createPostMethod();
print(post2.getMethod());
print(post2.safeMethod());
print(post2.passwordRequired());
print("===GET===");
com.zj.subclass.Method get2 = com.zj.subclass.Method.createGetMethod();
print(get2.getMethod());
print(get2.safeMethod());
print(get2.passwordRequired());
print("===PUT===");
com.zj.subclass.Method put2 = com.zj.subclass.Method.createPutMethod();
print(put2.getMethod());
print(put2.safeMethod());
print(put2.passwordRequired());
print("===DELETE===");
com.zj.subclas
|