当前位置: 首页 > news >正文

设计模式 - 代理模式(proxy pattern) 未使用代理模式 具体解释

代理模式(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











相关文章:

  • xsd验证允许空值
  • GO JsonStr 2 obj
  • FlexPaper:使用flash在线展示pdf
  • 《JavaScript高级程序设计》笔记:引用类型(五)
  • 数据库事务的四大特性
  • 嵌入Web资源的方法
  • js实现的时钟
  • python 输出当前行号
  • WannaCry勒索比特币蠕虫病毒解决方案
  • 构建之法阶段小记五
  • 带你理解JavaScript闭包
  • 用Browserify来实现CommonJS的浏览器加载
  • 几个收藏
  • Laravel源码入门-启动引导过程(六)LoadEnvironmentVariables
  • 判断窗体 show完成
  • python3.6+scrapy+mysql 爬虫实战
  • “寒冬”下的金三银四跳槽季来了,帮你客观分析一下局面
  • 「面试题」如何实现一个圣杯布局?
  • AzureCon上微软宣布了哪些容器相关的重磅消息
  • C++类中的特殊成员函数
  • JavaScript 奇技淫巧
  • java中的hashCode
  • Laravel Mix运行时关于es2015报错解决方案
  • leetcode98. Validate Binary Search Tree
  • Mybatis初体验
  • npx命令介绍
  • 从地狱到天堂,Node 回调向 async/await 转变
  • 从零开始的无人驾驶 1
  • 给新手的新浪微博 SDK 集成教程【一】
  • 构建二叉树进行数值数组的去重及优化
  • 微信小程序填坑清单
  • 学习HTTP相关知识笔记
  • Linux权限管理(week1_day5)--技术流ken
  • 东超科技获得千万级Pre-A轮融资,投资方为中科创星 ...
  • ​云纳万物 · 数皆有言|2021 七牛云战略发布会启幕,邀您赴约
  • (1) caustics\
  • (C语言)字符分类函数
  • (pojstep1.3.1)1017(构造法模拟)
  • (Repost) Getting Genode with TrustZone on the i.MX
  • (附源码)计算机毕业设计ssm高校《大学语文》课程作业在线管理系统
  • (每日持续更新)信息系统项目管理(第四版)(高级项目管理)考试重点整理第3章 信息系统治理(一)
  • (已解决)vue+element-ui实现个人中心,仿照原神
  • (转)C#调用WebService 基础
  • ../depcomp: line 571: exec: g++: not found
  • .Net 8.0 新的变化
  • .net 获取url的方法
  • .NET6实现破解Modbus poll点表配置文件
  • .Net8 Blazor 尝鲜
  • @FeignClient 调用另一个服务的test环境,实际上却调用了另一个环境testone的接口,这其中牵扯到k8s容器外容器内的问题,注册到eureka上的是容器外的旧版本...
  • [120_移动开发Android]008_android开发之Pull操作xml文件
  • [2019.2.28]BZOJ4033 [HAOI2015]树上染色
  • [Big Data - Kafka] kafka学习笔记:知识点整理
  • [C++数据结构](22)哈希表与unordered_set,unordered_map实现
  • [CentOs7]搭建ftp服务器(2)——添加用户
  • [EFI]Acer Aspire A515-54g电脑 Hackintosh 黑苹果efi引导文件