设计模式 - 代理模式(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
设计模式 - 代理模式(proxy pattern) 未使用代理模式 具体解释的更多相关文章
- 乐在其中设计模式(C#) - 代理模式(Proxy Pattern)
原文:乐在其中设计模式(C#) - 代理模式(Proxy Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 代理模式(Proxy Pattern) 作者:webabcd 介绍 为 ...
- 设计模式系列之代理模式(Proxy Pattern)——对象的间接访问
说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...
- 二十四种设计模式:代理模式(Proxy Pattern)
代理模式(Proxy Pattern) 介绍为其他对象提供一个代理以控制对这个对象的访问. 示例有一个Message实体类,某对象对它的操作有Insert()和Get()方法,用一个代理来控制对这个对 ...
- 代理模式(Proxy pattern)
代理模式(proxy pattern):作用:为其他对象提供一种代理,以控制对这个对象的访问.代理对象在客户端对象和目标对象之间起中介的作用. 代理模式涉及到的角色: 抽象角色:声明真实对象和代理对象 ...
- 设计模式(十三):从“FQ”中来认识代理模式(Proxy Pattern)
我们知道Google早就被墙了,所以FQ才能访问Google呢,这个“FQ”的过程就是一个代理的过程.“代理模式”在之前的博客中不止一次的提及过,之前的委托回调就是代理模式的具体应用.今天我们就从“F ...
- C#设计模式——代理模式(Proxy Pattern)
一.概述在软件开发中,有些对象由于创建成本高.访问时需要与其它进程交互等原因,直接访问会造成系统速度慢.复杂度增大等问题.这时可以使用代理模式,给系统增加一层间接层,通过间接层访问对象,从而达到隐藏系 ...
- 设计模式——代理模式(Proxy Pattern)
代理模式(Proxy),为其他对象提供一种代理以控制对这个对象的访问. UML图: 模型设计: Subject类: package com.cnblog.clarck; /** * Subject 类 ...
- 设计模式 -- 代理模式 (Proxy Pattern)
定义: 为其他对象提供一种代理以控制对这个对象的访问: 角色: 1,抽象主题类,(接口或者抽象类),抽象真实主题和代理的共有方法(如下Subject类): 2,具体实现的主题类,继承或者实现抽象主题类 ...
- 13.代理模式(Proxy Pattern)
using System; namespace Test { //抽象角色:声明真实对象和代理对象的共同接口. //代理角色:代理对象角色内部含有对真实对象的引用,从而可以操作真实对象, //同时代理 ...
随机推荐
- vue中如何将时间对象转换成字符串
借鉴element-admin中封装好的方法 import { parseTime } from '@/utils'// 在utils目录下的index.js文件中,方法如下 /** * Parse ...
- 勒索病毒 -- “永恒之蓝”NSA 武器免疫工具
“永恒之蓝”NSA 武器免疫工具 针对 445 端口:445端口是一个毁誉参半的端口,他和139端口一起是IPC$入侵的主要通道.有了它我们可以在局域网中轻松访问各种共享文件夹或共享打印机,但也正是因 ...
- hdu 5669 Road
题目大意 \(n\)个点,\(m\)次连边 每次连边为\((a\sim b)\leftrightarrow (c\sim d),cost=w\) 即在\((a-b)\)区间内的点向\((c-d)\)区 ...
- unbuntu 矫正电脑系统时间
sudo tzconfig,如果命令不存在请使用 dpkg-reconfigure tzdata
- 在android app中使用STL库(转)
1.在jni目录下新建Application.mk; 加入 APP_STL := stlport_static右边的值还可以换成下面几个: system - 使用默认最小的C++运行库,这样生成的应用 ...
- django+vue+nginx生产环境部署配置
部署环境: 1. linux redhat 7.1 2.python 3.6.3 3. vue 4. nginx 5. gunicorn 6. supervisord 安装: 一. 基础环境安装 1. ...
- Scrapy笔记:CrawSpider中rules中的使用
scrapy.spiders.crawl.CrawlSpider类的使用 这个类比较适用于对网站爬取批量网页,相比于Spider类,CrawlSpider主要使用规则(rules)来提取链接 rule ...
- springBoot AOP切面编程
AOP 为 Aspect Oriented Programming 的缩写,意为 面向切面编程.AOP 为spring 中的一个重要内容,它是通过对既有程序定义一个切入点,然后在其前后切入不同的执行内 ...
- 源码分析——迁移学习Inception V3网络重训练实现图片分类
1. 前言 近些年来,随着以卷积神经网络(CNN)为代表的深度学习在图像识别领域的突破,越来越多的图像识别算法不断涌现.在去年,我们初步成功尝试了图像识别在测试领域的应用:将网站样式错乱问题.无线领域 ...
- Machine Learning Done Wrong【转】
1. Take default loss function for granted Many practitioners train and pick the best model using the ...