代理模式:
- public interface GumballMachineRemote extends Remote {//必须扩展java.rmi.Remote
- public int getCount() throws RemoteException;//准备支持的方法必须抛出RemoteException
- public String getLocation() throws RemoteException;
- public State getState() throws RemoteException;
- //所有返回类型必须是原语类型或可序列化类型
- }
- public interface State extends Serializable {//扩展Serializable接囗,实现序列化,这样所有子类中的State就可以在网络上传送了
- public void insertQuarter();
- public void ejectQuarter();
- public void turnCrank();
- public void dispense();
- }
- public class NoQuarterState implements State {
- transient GumballMachine gumballMachine;//对State的每个实现,我们都在GumballMachine实例变量前加上transient关键字,告诉JVM不要序列化这个字段
- //其它方法
- }
- public class GumballMachine
- extends UnicastRemoteObject implements GumballMachineRemote
- {
- //这里有实例变量
- public GumballMachine(String location, int numberGumballs) throws RemoteException {//构造器需要抛出RemoteException,因为超类是这么做的
- //这里代码
- }
- public int getCount() {
- return count;
- }
- public State getState() {
- return state;
- }
- public String getLocation() {
- return location;
- }
- //其他方法
- }
- package headfirst.proxy.javaproxy;
- import java.lang.reflect.*;
- 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().equals("setHotOrNotRating")) {
- throw new IllegalAccessException();//禁止操作,抛出运行时异常
- } else if (method.getName().startsWith("set")) {
- return method.invoke(person, args);
- }
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- }
- return null;
- }
- }
- package headfirst.proxy.javaproxy;
- import java.lang.reflect.*;
- 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().equals("setHotOrNotRating")) {
- return method.invoke(person, args);
- } else if (method.getName().startsWith("set")) {
- throw new IllegalAccessException();
- }
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- }
- return null;
- }
- }
- PersonBean getOwnerProxy(PersonBean person) {
- return (PersonBean) Proxy.newProxyInstance(
- person.getClass().getClassLoader(),
- person.getClass().getInterfaces(),
- new OwnerInvocationHandler(person));
- }
- PersonBean getNonOwnerProxy(PersonBean person) {
- return (PersonBean) Proxy.newProxyInstance(
- person.getClass().getClassLoader(),
- person.getClass().getInterfaces(),
- new NonOwnerInvocationHandler(person));
- }
- package headfirst.proxy.javaproxy;
- import java.lang.reflect.*;
- import java.util.*;
- public class MatchMakingTestDrive {
- Hashtable datingDB = new Hashtable();
- public static void main(String[] args) {
- MatchMakingTestDrive test = new MatchMakingTestDrive();
- test.drive();
- }
- public MatchMakingTestDrive() {
- initializeDatabase();
- }
- public void drive() {
- PersonBean joe = getPersonFromDatabase("Joe Javabean");
- PersonBean ownerProxy = getOwnerProxy(joe);
- System.out.println("Name is " + ownerProxy.getName());
- ownerProxy.setInterests("bowling, Go");
- System.out.println("Interests set from owner proxy");
- try {
- ownerProxy.setHotOrNotRating(10);
- } catch (Exception e) {
- System.out.println("Can't set rating from owner proxy");
- }
- System.out.println("Rating is " + ownerProxy.getHotOrNotRating());
- PersonBean nonOwnerProxy = getNonOwnerProxy(joe);
- System.out.println("Name is " + nonOwnerProxy.getName());
- try {
- nonOwnerProxy.setInterests("bowling, Go");
- } catch (Exception e) {
- System.out.println("Can't set interests from non owner proxy");
- }
- nonOwnerProxy.setHotOrNotRating(3);
- System.out.println("Rating set from non owner proxy");
- System.out.println("Rating is " + nonOwnerProxy.getHotOrNotRating());
- }
- PersonBean getOwnerProxy(PersonBean person) {
- return (PersonBean) Proxy.newProxyInstance(
- person.getClass().getClassLoader(),
- person.getClass().getInterfaces(),
- new OwnerInvocationHandler(person));
- }
- PersonBean getNonOwnerProxy(PersonBean person) {
- return (PersonBean) Proxy.newProxyInstance(
- person.getClass().getClassLoader(),
- person.getClass().getInterfaces(),
- new NonOwnerInvocationHandler(person));
- }
- PersonBean getPersonFromDatabase(String name) {
- return (PersonBean)datingDB.get(name);
- }
- void initializeDatabase() {
- PersonBean joe = new PersonBeanImpl();
- joe.setName("Joe Javabean");
- joe.setInterests("cars, computers, music");
- joe.setHotOrNotRating(7);
- datingDB.put(joe.getName(), joe);
- PersonBean kelly = new PersonBeanImpl();
- kelly.setName("Kelly Klosure");
- kelly.setInterests("ebay, movies, music");
- kelly.setHotOrNotRating(6);
- datingDB.put(kelly.getName(), kelly);
- }
- }
- package headfirst.proxy.javaproxy;
- 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);
- }
- package headfirst.proxy.javaproxy;
- public class PersonBeanImpl implements PersonBean {
- String name;
- String gender;
- String interests;
- int rating;
- int ratingCount = 0;
- public String getName() {
- return name;
- }
- public String getGender() {
- return gender;
- }
- public String getInterests() {
- return interests;
- }
- public int getHotOrNotRating() {
- if (ratingCount == 0) return 0;
- return (rating/ratingCount);
- }
- public void setName(String 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++;
- }
- }
















