代理模式(proxy pattern) 未使用代理模式 詳解

來源:互聯網
上載者:User

如果需要監控(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("\nMighty Gumball, Inc.");          result.append("\nJava-enabled Standing Gumball Model #2004\n");          result.append("Inventory: " + count + " gumball");          if (count != 1) {              result.append("s");          }          result.append("\nMachine is " + state + "\n");          return result.toString();      }  }

本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/project/

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.