设计模式笔记(一)
来源:互联网 发布:mac杀毒软件哪个好用 编辑:程序博客网 时间:2024/04/29 10:10
设计模式笔记(一)
建立一个独立的对象池管理员,内部私有类指向管理对象,用PoolItem对象封装的目的是打上inUse指示。接口方法有入池、读取和出池。
//: singleton:PoolManager.java package singleton; import java.util.*; public class PoolManager { private static class PoolItem { boolean inUse = false; Object item; PoolItem(Object item) { this.item = item; } } private ArrayList items = new ArrayList(); public void add(Object item) { items.add(new PoolItem(item)); } static class EmptyPoolException extends Exception { } public Object get() throws EmptyPoolException { for (int i = 0; i < items.size(); i++) { PoolItem pitem = (PoolItem) items.get(i); if (pitem.inUse == false) { pitem.inUse = true; return pitem.item; } } // Fail early: throw new EmptyPoolException(); // return null; // Delayed failure } public void release(Object item) { for (int i = 0; i < items.size(); i++) { PoolItem pitem = (PoolItem) items.get(i); if (item == pitem.item) { pitem.inUse = false; return; } } throw new RuntimeException(item + " not found"); } } // /:~尽管对象池管理员不使用单例模式,但是子类连接池管理员是单例的。我认为这里使用了Adapter设计模式,接口做了映射。连接池管理员继承了对象池管理员,但并没有屏蔽父类的方法。
//: singleton:ConnectionPoolDemo.javapackage singleton;import junit.framework.*;interface Connection {Object get();void set(Object x);}class ConnectionImplementation implements Connection {public Object get() {return null;}public void set(Object s) {}}class ConnectionPool { // A singletonprivate static PoolManager pool = new PoolManager();public static void addConnections(int number) {for (int i = 0; i < number; i++)pool.add(new ConnectionImplementation());}public static Connection getConnection()throws PoolManager.EmptyPoolException {return (Connection) pool.get();}public static void releaseConnection(Connection c) {pool.release(c);}}public class ConnectionPoolDemo extends TestCase {static {ConnectionPool.addConnections(5);}public void test() {Connection c = null;try {c = ConnectionPool.getConnection();} catch (PoolManager.EmptyPoolException e) {throw new RuntimeException(e);}c.set(new Object());c.get();ConnectionPool.releaseConnection(c);}public void test2() {Connection c = null;try {c = ConnectionPool.getConnection();} catch (PoolManager.EmptyPoolException e) {throw new RuntimeException(e);}c.set(new Object());c.get();ConnectionPool.releaseConnection(c);}public static void main(String args[]) {junit.textui.TestRunner.run(ConnectionPoolDemo.class);}} // /:~
- 设计模式笔记(一)
- 设计模式笔记(一)
- 设计模式笔记(一)
- 设计模式笔记(一)--设计原则
- 设计模式笔记(一) Observer模式
- java设计模式学习笔记(一)-- 简述设计模式
- 设计模式学习笔记(一)设计模式六大原则
- 设计模式学习笔记(一)
- 设计模式学习笔记一(概述)
- 学习笔记(一)设计模式
- Java与设计模式 (笔记一)
- 《大话设计模式》学习笔记(一)
- 《HeadFirst 设计模式》学习笔记(一)
- JAVA设计模式笔记整理(一)
- 设计模式学习笔记--引言(一)
- 《大话设计模式》笔记摘要 (一)
- 设计模式笔记一(strategy pattern)
- Java 设计模式学习笔记(一)
- API Design for ios 译文
- 总结数据库设计的原则技巧
- C++ 重载(overload)、重写(overrride)、重定义(redefine)总结
- Oracle客户端目录简介
- 传值
- 设计模式笔记(一)
- Maxiee的Vim入门日记(4)——安装windows下的Cscope
- 断言
- [leetcode] Scramble String
- 将字符串变成大写----C++实现
- Codeforces D. Points
- poj 2155 Matrix
- 什么是API
- 使用instantclient_11_2 和PL/SQL Developer工具包连接oracle 11g远程数据库