代理模式(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("\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();
}
}

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

设计模式 - 代理模式(proxy pattern) 未使用代理模式 具体解释的更多相关文章

  1. 乐在其中设计模式(C#) - 代理模式(Proxy Pattern)

    原文:乐在其中设计模式(C#) - 代理模式(Proxy Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 代理模式(Proxy Pattern) 作者:webabcd 介绍 为 ...

  2. 设计模式系列之代理模式(Proxy Pattern)——对象的间接访问

    说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...

  3. 二十四种设计模式:代理模式(Proxy Pattern)

    代理模式(Proxy Pattern) 介绍为其他对象提供一个代理以控制对这个对象的访问. 示例有一个Message实体类,某对象对它的操作有Insert()和Get()方法,用一个代理来控制对这个对 ...

  4. 代理模式(Proxy pattern)

    代理模式(proxy pattern):作用:为其他对象提供一种代理,以控制对这个对象的访问.代理对象在客户端对象和目标对象之间起中介的作用. 代理模式涉及到的角色: 抽象角色:声明真实对象和代理对象 ...

  5. 设计模式(十三):从“FQ”中来认识代理模式(Proxy Pattern)

    我们知道Google早就被墙了,所以FQ才能访问Google呢,这个“FQ”的过程就是一个代理的过程.“代理模式”在之前的博客中不止一次的提及过,之前的委托回调就是代理模式的具体应用.今天我们就从“F ...

  6. C#设计模式——代理模式(Proxy Pattern)

    一.概述在软件开发中,有些对象由于创建成本高.访问时需要与其它进程交互等原因,直接访问会造成系统速度慢.复杂度增大等问题.这时可以使用代理模式,给系统增加一层间接层,通过间接层访问对象,从而达到隐藏系 ...

  7. 设计模式——代理模式(Proxy Pattern)

    代理模式(Proxy),为其他对象提供一种代理以控制对这个对象的访问. UML图: 模型设计: Subject类: package com.cnblog.clarck; /** * Subject 类 ...

  8. 设计模式 -- 代理模式 (Proxy Pattern)

    定义: 为其他对象提供一种代理以控制对这个对象的访问: 角色: 1,抽象主题类,(接口或者抽象类),抽象真实主题和代理的共有方法(如下Subject类): 2,具体实现的主题类,继承或者实现抽象主题类 ...

  9. 13.代理模式(Proxy Pattern)

    using System; namespace Test { //抽象角色:声明真实对象和代理对象的共同接口. //代理角色:代理对象角色内部含有对真实对象的引用,从而可以操作真实对象, //同时代理 ...

随机推荐

  1. 【08】Vue 之 vue-cli

    8.1. 前置知识学习 npm 学习 官方文档 推荐资料 npm入门 npm介绍 需要了解的知识点 package.json 文件相关配置选项 npm 本地安装.全局安装.本地开发安装等区别及相关命令 ...

  2. group by timestamp

    SELECT DATE_FORMAT( deteline, "%Y-%m-%d %H" ) , COUNT( * )  FROM test GROUP BY DATE_FORMAT ...

  3. Flask 服务器设置host=0.0.0.0之后外部仍然无法访问, 使用命令python *.py run启动OK

    IP设置成0.0.0.0,还是不能外部访问 if __name__ == '__main__': app.run(host=, debug=True) 使用的Pycharm IDE,使用的三角号运行的 ...

  4. Guice 4.1教程

    Guice是Google开发的一个开源轻量级的依赖注入框架,运行速度快,使用简单. 项目地址:https://github.com/google/guice/ 最新的版本是4.1,本文基于此版本. 0 ...

  5. centos7.2安装tomcat8

    环境: 阿里云centos7.2 tomcat8.0.32 jdk8.131 1 上传tomcat安装包到服务器的/home(个人习惯) 2 解压安装包 [root@iZt4n6h3u4k407nni ...

  6. Codeforces Gym100735 E.Restore (KTU Programming Camp (Day 1) Lithuania, Birˇstonas, August 19, 2015)

    E - Restore Given a matrix A of size N * N. The rows are numbered from 0 to N-1, the columns are num ...

  7. (1)WCF托管

    wcf 托管方式有很多种,常见的托管方式,iis,was,控制台,winfrom等. 先创建一个wcf服务 IService1.cs using System.ServiceModel; namesp ...

  8. [Python Cookbook] Numpy Array Slicing and Indexing

    1-D Array Indexing Use bracket notation [ ] to get the value at a specific index. Remember that inde ...

  9. POJ 1239 Increasing Sequences [DP]

    题意:略. 思路:进行两次dp. 第一次dp从前向后,用dp[x]表示从第x位向前dp[x]位可构成一个数字,且与前面的数组符合题意要求.最后求的dp[n]即为最后一个数字的长度. 而题目还有要求,所 ...

  10. [XJOI-NOI2015-13-C]白黑树

    题目大意: 给你一个$n(n\leq300000)$个结点的以$1$为根的树,结点有黑白两种颜色,每个点初始权值为$0$.进行以下2种共$m(m\leq300000)$次操作: 1.给定结点$u$,对 ...