设计模式 - 代理模式(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中通过路由跳转的三种方式
原文:https://blog.csdn.net/qq_40072782/article/details/82533477 router-view 实现路由内容的地方,引入组件时写到需要引入的地方需要 ...
- Gate Of Babylon(bzoj 1272)
Description Input Output Sample Input Sample Output 12 HINT /* 容斥+lucas+乘法逆元 首先,看到有限制的只有15个,因此可以用容斥原 ...
- Perm 排列计数(bzoj 2111)
Description 称一个1,2,...,N的排列P1,P2...,Pn是Magic的,当且仅当2<=i<=N时,Pi>Pi/2. 计算1,2,...N的排列中有多少是Magic ...
- ef code first transform,add ef power tools add-in,add tangible t4 editor for enhancement.
use ef power tools, as to .edmx file,right click at view, choose generate database from model, then ...
- ios 地图,系统升级为12后,进入地图,大头针全部默认展开问题,以及在选择不同距离的情况下,如何刷新地图的区域范围
1.第一个问题,大头针在ios12,默认展开问题,需要设置大头针视图的默认选中属性为NO - (MKAnnotationView *)mapView:(MKMapView *)mapView view ...
- 51nod 1006 最长公共子序列Lcs 【LCS/打印path】
1006 最长公共子序列Lcs 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). ...
- Jumpserver0.5使用说明
1.系统设置 a.基本设置,这里的ip是jumpserver所在的地址 b.邮件设置,得在qq邮箱中启用授权码 可参考:https://service.mail.qq.com/cgi-bin/help ...
- luogu P2285 [HNOI2004]打鼹鼠
题目描述 鼹鼠是一种很喜欢挖洞的动物,但每过一定的时间,它还是喜欢把头探出到地面上来透透气的.根据这个特点阿牛编写了一个打鼹鼠的游戏:在一个n*n的网格中,在某些时刻鼹鼠会在某一个网格探出头来透透气. ...
- 某考试 T1 str
一开始死磕sam,发现根本没法做...... 后来想了想,反正匹配子串的大部分不是sam就是 二分+hash啊,,,于是就想了想二分+hash,发现好像可以做啊! 就是假设我们要让 s1[1] 映射到 ...
- python在程序中通过Windows打开文件
import os os.startfile(r'D:\test_input.html')