package
com.zj.original;
import
com.zj.utils.NoMethodTypeException;
publicclass
Method {
privateint
_type
;
publicstaticfinalint
POST
= 0;
publicstaticfinalint
GET
= 1;
publicstaticfinalint
PUT
= 2;
publicstaticfinalint
DELETE
= 3;
public
Method(
int
type) {
_type
= type;
}
public
String getMethod()
throws
NoMethodTypeException {
switch
(
_type
) {
case
POST
:
return
"This is POST method"
;
case
GET
:
return
"This is GET method"
;
case
PUT
:
return
"This is PUT method"
;
case
DELETE
:
return
"This is DELETE method"
;
default
:
thrownew
NoMethodTypeException();
}
}
publicboolean
safeMethod() {
if
(
_type
==
GET
)
returntrue
;
else
returnfalse
;
}
publicboolean
passwordRequired() {
if
(
_type
==
POST
)
returnfalse
;
else
returntrue
;
}
}
|
package
com.zj.subclass;
publicabstractclass
Method {
publicfinalstatic
Method createPostMethod() {
returnnew
PostMethod();
}
publicfinalstatic
Method createGetMethod() {
returnnew
GetMethod();
}
publicfinalstatic
Method createPutMethod() {
returnnew
PutMethod();
}
publicfinalstatic
Method createDeleteMethod() {
returnnew
DelMethod();
}
abstractpublic
String getMethod();
publicboolean
safeMethod() {
returnfalse
;
}
publicboolean
passwordRequired() {
returntrue
;
}
}
|
package
com.zj.subclass;
publicclass
PostMethod
extends
Method {
@Override
public
String getMethod() {
return
"This is POST method"
;
}
@Override
publicboolean
passwordRequired() {
returnfalse
;
}
}
|
package
com.zj.subclass;
publicclass
GetMethod
extends
Method{
@Override
public
String getMethod() {
return
"This is GET method"
;
}
@Override
publicboolean
safeMethod() {
returntrue
;
}
}
|
package
com.zj.subclass;
publicclass
PutMethod
extends
Method {
@Override
public
String getMethod() {
return
"This is PUT method"
;
}
}
|
package
com.zj.subclass;
publicclass
DelMethod
extends
Method{
@Override
public
String getMethod(){
return
"This is DELETE method"
;
}
}
|
package
com.zj.state;
publicinterface
MethodType {
String getTypeDescription();
String getMethodDescription();
boolean
isSafe();
boolean
isRequired();
}
|
package
com.zj.state;
publicclass
Post
implements
MethodType{
public
String getMethodDescription() {
return
"This is POST method"
;
}
public
String getTypeDescription() {
return
"===POST==="
;
}
publicboolean
isRequired() {
returnfalse
;
}
publicboolean
isSafe() {
returnfalse
;
}
}
|
package
com.zj.state;
publicclass
Get
implements
MethodType{
public
String getMethodDescription() {
return
"This is GET method"
;
}
public
String getTypeDescription() {
return
"===GET==="
;
}
publicboolean
isRequired() {
returntrue
;
}
publicboolean
isSafe() {
returntrue
;
}
}
|
package
com.zj.state;
publicclass
Put
implements
MethodType{
public
String getMethodDescription() {
return
"This is PUT method"
;
}
public
String getTypeDescription() {
return
"===PUT==="
;
}
publicboolean
isRequired() {
returntrue
;
}
publicboolean
isSafe() {
returnfalse
;
}
}
|
package
com.zj.state;
publicclass
Delete
implements
MethodType{
public
String getMethodDescription() {
return
"This is DELETE method"
;
}
public
String getTypeDescription() {
return
"===DELETE==="
;
}
publicboolean
isRequired() {
returntrue
;
}
publicboolean
isSafe() {
returnfalse
;
}
}
|
package
com.zj.state;
publicclass
Method {
private
MethodType
_type
;
public
Method() {
_type
=
null
;
}
public
Method(MethodType type) {
_type
= type;
}
public
String getMethod() {
return
_type
.getMethodDescription();
}
publicboolean
safeMethod() {
return
_type
.isSafe();
}
publicboolean
passwordRequired() {
return
_type
.isRequired();
}
publicvoid
changeType(MethodType type) {
_type
= type;
}
publicvoid
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());
}
}
}
|
package
com.zj.utils;
publicclass
Client {
staticvoid
print(String s) {
System.
out
.println(s);
}
staticvoid
print(Boolean b) {
System.
out
.println(b);
}
publicstaticvoid
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.subclass.Method del2 = com.zj.subclass.Method
.
createDeleteMethod();
print(del2.getMethod());
print(del2.safeMethod());
print(del2.passwordRequired());
print(
"===state==="
);
new
com.zj.state.Method().runAllMethods();
}
}
|
本文出自 “子 孑” 博客,请务必保留此出处http://zhangjunhd.blog.51cto.com/113473/68070