• 设计模式


    代理模式(proxy pattern) 未使用代理模式 详细解释


    本文地址: http://blog.csdn.net/caroline_wendy


    部分代码參考: http://blog.csdn.net/caroline_wendy/article/details/37698747


    假设须要监控(monitor)类的某些状态, 则须要编写一个监控类, 并同过监控类进行监控.

    但只局限于本地, 假设须要远程监控, 则须要使用代理模式(proxy pattern).


    详细方法:

    1. 类中须要提供状态信息, 并提供一些get方法, 进行调用.

    /**
     * @time 2014年7月11日
     */
    package proxy;
    
    /**
     * @author C.L.Wang
     *
     */
    public class GumballMachine {
    
    	String location; //位置信息
    	
    	State soldOutState;
    	State noQuarterState;
    	State hasQuarterState;
    	State soldState;
    	State winnerState;
    	
    	State state = soldOutState;
    	int count = 0;
    	
    	/**
    	 * 
    	 */
    	public GumballMachine(String location, int numberGumballs) {
    		// TODO Auto-generated constructor stub
    		soldOutState = new SoldOutState(this);
    		noQuarterState = new NoQuarterState(this);
    		hasQuarterState = new HasQuarterState(this);
    		soldState = new SoldState(this);
    		winnerState = new WinnerState(this);
    		
    		this.location = location;
    		
    		this.count = numberGumballs;
    		if (numberGumballs > 0) {
    			state = noQuarterState;
    		}
    	}
    	
    	public void insertQuarter() {
    		state.insertQuarter();
    	}
    	
    	public void ejectQuarter() {
    		state.ejectQuater();
    	}
    
    	public void turnCrank() {
    		state.turnCrank();
    		state.dispense();
    	}
    	
    	public void setState(State state) {
    		this.state = state;
    	}
    	
    	public void releaseBall() {
    		System.out.println("A gumball comes rolling out the slot...");
    		if (count != 0)
    			count --;
    	}
    	
    	public int getCount() {
    		return count;
    	}
    	
    	public void refill(int count) {
    		this.count = count;
    		state = noQuarterState;
    	}
    	
    	public State getState() {
    		return state;
    	}
    	
    	public String getLocation() {
    		return location;
    	}
    	
    	public State getSoldOutState() {
    		return soldOutState;
    	}
    	
    	public State getNoQuarterState() {
    		return noQuarterState;
    	}
    	
    	public State getHasQuarterState() {
    		return hasQuarterState;
    	}
    	
    	public State getSoldState() {
    		return soldState;
    	}
    	
    	public State getWinnerState() {
    		return winnerState;
    	}
    	
    	public String toString() {
    		StringBuffer result = new StringBuffer();
    		result.append("
    Mighty Gumball, Inc.");
    		result.append("
    Java-enabled Standing Gumball Model #2004
    ");
    		result.append("Inventory: " + count + " gumball");
    		if (count != 1) {
    			result.append("s");
    		}
    		result.append("
    Machine is " + state + "
    ");
    		return result.toString();
    	}
    }
    

    2. 监控类(monitor class), 调用get方法, 进行监控输出.

    /**
     * @time 2014年7月12日
     */
    package proxy;
    
    /**
     * @author C.L.Wang
     *
     */
    public class GumballMonitor {
    
    	GumballMachine machine;
    	
    	/**
    	 * 
    	 */
    	public GumballMonitor(GumballMachine machine) {
    		// TODO Auto-generated constructor stub
    		this.machine = machine;
    	}
    	
    	public void report() {
    		System.out.println("Gumball Machine: " + machine.getLocation());
    		System.out.println("Current inventory: " + machine.getCount() + " gumballs.");
    		System.out.println("Current state: " + machine.getState());
    	}
    
    }
    

    3. 其余代码參考: http://blog.csdn.net/caroline_wendy/article/details/37698747


    4. 測试类, 实例化详细类, 并使用监控类, 进行监控.

    /**
     * @time 2014年7月11日
     */
    package proxy;
    
    /**
     * @author C.L.Wang
     *
     */
    public class GumballMachineTestDrive {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		GumballMachine gumballMachine = new GumballMachine("Seattle", 115);
    		GumballMonitor gumballMonitor = new GumballMonitor(gumballMachine);
    		gumballMonitor.report();
    	}
    
    }
    

    5. 输出:

    Gumball Machine: Seattle
    Current inventory: 115 gumballs.
    Current state: waiting for quater
    











  • 相关阅读:
    7.31实习报告
    7.30实习报告
    7.29实习报告
    7.28实习报告
    7.27实习报告
    2019-2020-1学期 20192418《网络空间安全专业导论》第八周学习总结
    2019-2020-1学期 20192418 《网络空间安全专业导论》第七周学习总结
    2019-2020-1学期 张曦 白胤廷 邢继元 宗俊豪小组《网络空间安全专业导论》小组讨论议题及脑图
    2019-2020-1学期 20192418《网络空间安全专业导论》第六周学习总结
    2019-2020-1学期 20192418 《网络空间安全专业导论》第五周学习总结
  • 原文地址:https://www.cnblogs.com/llguanli/p/6834293.html
Copyright © 2020-2023  润新知