博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
对象池模式和代理模式配合使用的例子
阅读量:6891 次
发布时间:2019-06-27

本文共 3075 字,大约阅读时间需要 10 分钟。

 

1、Connection接口

1 package com.xinye.test.pool3;2 3 public interface Connection {4     void set(Object obj);5     void get();6 }

2、Connection实现

1 package com.xinye.test.pool3; 2  3 public class ConnectionImpl implements Connection { 4  5     @Override 6     public void set(Object obj) { 7  8     } 9 10     @Override11     public void get() {12 13     }14 15 }

3、Connection代理类

1 package com.xinye.test.pool3; 2 /** 3  *  4  * @author xinye 5  * 6  */ 7 public class ConnectionProxy implements Connection{ 8     private ReleaseablePoolItem
releaseable = null; 9 public ConnectionProxy(ReleaseablePoolItem
rel){10 this.releaseable = rel;11 }12 @Override13 public void set(Object obj) {14 releaseable.getReference().set(obj);15 }16 17 @Override18 public void get() {19 releaseable.getReference().get();20 }21 22 }

4、连接池

1 package com.xinye.test.pool3; 2  3 public class ConnectionPool { 4     private static PoolManager
manager = new PoolManager
(); 5 private ConnectionPool(){ 6 7 } 8 public static void add(int num){ 9 for(int i = 0;i < num;i++){10 manager.add(new ConnectionImpl());11 }12 } 13 public static Connection getConnection(){14 ReleaseablePoolItem
conn = manager.get();15 if(conn == null){16 return null;17 }18 return new ConnectionProxy(conn);19 }20 21 }

5、池子中放的东西

1 package com.xinye.test.pool3;2 3 public class PoolItem 
{4 public boolean isUse = false;5 public T item;6 public PoolItem(T t){7 this.item = t;8 }9 }

6、释放对象的时候不能再使用

1 package com.xinye.test.pool3; 2 /** 3  *  4  * @author  5  * 6  * @param 
7 */ 8 public class ReleaseablePoolItem
{ 9 public PoolItem
reference;10 public boolean released = false;11 public ReleaseablePoolItem(PoolItem
reference){12 13 this.reference = reference;14 15 }16 public T getReference(){17 if(released){18 throw new RuntimeException("Tried to use reference after it was released");19 }20 return reference.item;21 }22 public void release(){23 released = true;24 reference.isUse = false;25 }26 }

7、池子管理对象

1 package com.xinye.test.pool3; 2  3 import java.util.ArrayList; 4  5 public class PoolManager 
{ 6 private ArrayList
> list = new ArrayList
>(); 7 8 public void add(T t){ 9 list.add(new PoolItem
(t));10 }11 public ReleaseablePoolItem
get(){12 int len = list.size();13 for(int i = 0;i < len;i++){14 PoolItem
item = list.get(i);15 if(item.isUse == false){16 item.isUse = true;17 return new ReleaseablePoolItem
(item);18 }19 }20 return null;21 }22 }

 

转载于:https://www.cnblogs.com/xinye/p/3908394.html

你可能感兴趣的文章
Python 函数(可变参数)
查看>>
FFmpeg中HLS文件解析源码
查看>>
IPMI总结
查看>>
UVA 11988 Broken Keyboard (链表)
查看>>
去掉影响美观的横滚动条
查看>>
「2019冬令营提高组」树
查看>>
配置OWASP的ModSecurity规则
查看>>
laravel 5.1 单元测试 Cannot modify header information 错误
查看>>
周四作业
查看>>
iOS - 正则表达式判断邮箱、身份证..是否正确
查看>>
golang 转换markdown文件为html
查看>>
JS的console使用
查看>>
SpringMVC最简单配置应用
查看>>
8.Appium的基本使用-3(安装JDK、android-sdk)
查看>>
区块链开发(1)基本概念
查看>>
java中的深层复制和浅层复制
查看>>
pytest自动化1:兼容unittest代码实例
查看>>
spring集成freemaker 制作短信模板
查看>>
从 Nginx 优秀的核心架构设计,揭秘其为何能支持高并发?
查看>>
Linux第四章 进程
查看>>