package M_Proxy. dynamic;
import java. lang. reflect. Proxy;
public class MatchMakingTestDrive {
public static void main ( String[ ] args) {
MatchMakingTestDrive test = new MatchMakingTestDrive ( ) ;
test. drive ( ) ;
}
private void drive ( ) {
PersonBean personBean = new PersonBeanImpl ( "张三" , "女" , "兴趣是上网" , 10 ) ;
PersonBean ownerProxy = getOwnerProxy ( personBean) ;
System. out. println ( ownerProxy. getName ( ) ) ;
ownerProxy. setInterests ( "bowling go" ) ;
System. out. println ( ownerProxy. getInterests ( ) ) ;
try {
ownerProxy. setHotOrNotRating ( 10 ) ;
} catch ( Exception e) {
System. out. println ( "修改评分失败" ) ;
}
System. out. println ( "============" ) ;
PersonBean nonOwnerProxy = getNonOwnerProxy ( personBean) ;
try {
nonOwnerProxy. setInterests ( "哈哈" ) ;
} catch ( Exception e) {
System. out. println ( "设置兴趣爱好失败" ) ;
}
nonOwnerProxy. setHotOrNotRating ( 8 ) ;
System. out. println ( nonOwnerProxy. getHotOrNotRating ( ) ) ;
}
private PersonBean getNonOwnerProxy ( PersonBean person) {
return ( PersonBean) Proxy. newProxyInstance ( person. getClass ( )
. getClassLoader ( ) , person. getClass ( ) . getInterfaces ( ) ,
new NonOwnerInvocationHandler ( person) ) ;
}
public PersonBean getOwnerProxy ( PersonBean person) {
return ( PersonBean) Proxy. newProxyInstance ( person. getClass ( )
. getClassLoader ( ) , person. getClass ( ) . getInterfaces ( ) ,
new OwnerInvocationHandler ( person) ) ;
}
}
package M_Proxy. dynamic;
import java. lang. reflect. InvocationHandler;
import java. lang. reflect. InvocationTargetException;
import java. lang. reflect. Method;
public class NonOwnerInvocationHandler implements InvocationHandler {
PersonBean person;
public NonOwnerInvocationHandler ( PersonBean person) {
this . person = person;
}
public Object invoke ( Object proxy, Method method, Object[ ] args)
throws IllegalAccessException {
try {
if ( method. getName ( ) . startsWith ( "get" ) ) {
return method. invoke ( person, args) ;
} else if ( method. getName ( ) . startsWith ( "setHotOrNotRating" ) ) {
return method. invoke ( person, args) ;
} else if ( method. getName ( ) . startsWith ( "set" ) ) {
throw new IllegalAccessException ( ) ;
}
} catch ( InvocationTargetException e) {
e. printStackTrace ( ) ;
}
return null;
}
}
package M_Proxy. dynamic;
import java. lang. reflect. InvocationHandler;
import java. lang. reflect. InvocationTargetException;
import java. lang. reflect. Method;
public class OwnerInvocationHandler implements InvocationHandler {
PersonBean person;
public OwnerInvocationHandler ( PersonBean person) {
this . person = person;
}
public Object invoke ( Object proxy, Method method, Object[ ] args)
throws IllegalAccessException {
try {
if ( method. getName ( ) . startsWith ( "get" ) ) {
return method. invoke ( person, args) ;
} else if ( method. getName ( ) . startsWith ( "setHotOrNotRating" ) ) {
throw new IllegalAccessException ( ) ;
} else if ( method. getName ( ) . startsWith ( "set" ) ) {
return method. invoke ( person, args) ;
}
} catch ( InvocationTargetException e) {
e. printStackTrace ( ) ;
}
return null;
}
}
package M_Proxy. dynamic;
public class PersonBeanImpl implements PersonBean {
String name;
String gender;
String interests;
int rating;
int ratingCount = 0 ;
public PersonBeanImpl ( String name, String gender, String interests,
int rating) {
this . name = name;
this . gender = gender;
this . interests = interests;
this . rating = rating;
ratingCount++ ;
}
public String getName ( ) {
return name;
}
public String getGender ( ) {
return gender;
}
public String getInterests ( ) {
return interests;
}
public int getHotOrNotRating ( ) {
return ratingCount == 0 ? 0 : rating / ratingCount;
}
public void setName ( String name) {
this . name = name;
}
public void setGender ( String gender) {
this . gender = gender;
}
public void setInterests ( String interests) {
this . interests = interests;
}
public void setHotOrNotRating ( int rating) {
this . rating += rating;
ratingCount++ ;
}
}
package M_Proxy. dynamic;
public interface PersonBean {
String getName ( ) ;
String getGender ( ) ;
String getInterests ( ) ;
int getHotOrNotRating ( ) ;
void setName ( String name) ;
void setGender ( String gender) ;
void setInterests ( String interests) ;
void setHotOrNotRating ( int rating) ;
}