装饰模式是以对客户端透明的方式扩展对象的功能。是继承的替代方案。
UML图
参照夏昕的hibernate书中的连接池例子,来了解装饰模式如何应用。
先新建一个连接池接口。
ConnectionPool.java
package com.javapatterns.decorator.jdbc;
import java.sql.Connection;
public interface ConnectionPool {
public Connection getConnection();
public void releaseConncetion(Connection conn) ;
}
新建一个连接池
DBConnectionPool.java
package com.javapatterns.decorator.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
public class DBConnectionPool implements ConnectionPool{
private Vector pool;
private Map stmtPool = new HashMap();
private static final int POOL_MAX_SIZE = 30;
public synchronized Connection getConnection() {
if(pool == null)
pool = new Vector();
Connection conn;
if (pool.isEmpty())
conn = createConnection();
else {
int last_idx = pool.size() - 1;
conn = (Connection)pool.get(last_idx);
pool.remove(last_idx);
}
return conn;
}
private Connection createConnection(){
Connection conn = null;
try {
Class.forName("org.gjt.mm.mysql.Driver");
conn = DriverManager.getConnection("dbc:mysql://localhost:3306/test","root","root");
return conn;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public void releaseConncetion(Connection conn) {
if (pool.size() > POOL_MAX_SIZE) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
else {
pool.add(conn);
}
}
}
DBConnectionPool是一个简陋的连接池,它有一个问题,当客户端调用Connection.close()关闭连接,而不是调用DBConnectionPool.releaseConncetion(Connection conn)返还Connection.这样会导致连接池彻底失效。
新建一个Dectorator
package com.javapatterns.decorator.jdbc;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Savepoint;
import java.sql.Statement;
import java.util.Map;
public class ConnectionDectorator implements Connection {
Connection conn;
public ConnectionDectorator(Connection conn) {
this.conn = conn;
}
public void clearWarnings() throws SQLException {
this.conn.clearWarnings();
}
public void close() throws SQLException {
this.conn.close();
}
public void commit() throws SQLException {
this.conn.commit();
}
public Statement createStatement() throws SQLException {
return this.conn.createStatement();
}
public Statement createStatement(int resultSetType, int resultSetConcurrency)
throws SQLException {
return this.conn.createStatement(resultSetType, resultSetConcurrency);
}
public Statement createStatement(int resultSetType,
int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
return this.conn.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
}
public boolean getAutoCommit() throws SQLException {
return this.conn.getAutoCommit();
}
public String getCatalog() throws SQLException {
return this.conn.getCatalog();
}
public int getHoldability() throws SQLException {
return this.conn.getHoldability();
}
public DatabaseMetaData getMetaData() throws SQLException {
return this.conn.getMetaData();
}
public int getTransactionIsolation() throws SQLException {
return this.conn.getTransactionIsolation();
}
public Map getTypeMap() throws SQLException {
return this.conn.getTypeMap();
}
public SQLWarning getWarnings() throws SQLException {
return this.conn.getWarnings();
}
public boolean isClosed() throws SQLException {
return this.conn.isClosed();
}
public boolean isReadOnly() throws SQLException {
return this.conn.isReadOnly();
}
public String nativeSQL(String sql) throws SQLException {
return this.conn.nativeSQL(sql);
}
public CallableStatement prepareCall(String sql) throws SQLException {
return this.conn.prepareCall(sql);
}
public CallableStatement prepareCall(String sql, int resultSetType,
int resultSetConcurrency) throws SQLException {
return this.conn.prepareCall(sql, resultSetType, resultSetConcurrency);
}
public CallableStatement prepareCall(String sql, int resultSetType,
int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
return this.conn.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
}
public PreparedStatement prepareStatement(String sql) throws SQLException {
return this.conn.prepareStatement(sql);
}
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
throws SQLException {
return this.conn.prepareStatement(sql, autoGeneratedKeys);
}
public PreparedStatement prepareStatement(String sql, int[] columnIndexes)
throws SQLException {
return this.conn.prepareStatement(sql, columnIndexes);
}
public PreparedStatement prepareStatement(String sql, String[] columnNames)
throws SQLException {
return this.conn.prepareStatement(sql, columnNames);
}
public PreparedStatement prepareStatement(String sql, int resultSetType,
int resultSetConcurrency) throws SQLException {
return this.conn.prepareStatement(sql, resultSetType, resultSetConcurrency);
}
public PreparedStatement prepareStatement(String sql, int resultSetType,
int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
return this.conn.prepareStatement(sql, resultSetType, resultSetConcurrency);
}
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
this.conn.releaseSavepoint(savepoint);
}
public void rollback() throws SQLException {
this.conn.rollback();
}
public void rollback(Savepoint savepoint) throws SQLException {
this.conn.rollback(savepoint);
}
public void setAutoCommit(boolean autoCommit) throws SQLException {
this.conn.setAutoCommit(autoCommit);
}
public void setCatalog(String catalog) throws SQLException {
this.conn.setCatalog(catalog);
}
public void setHoldability(int holdability) throws SQLException {
this.conn.setHoldability(holdability);
}
public void setReadOnly(boolean readOnly) throws SQLException {
this.conn.setReadOnly(readOnly);
}
public Savepoint setSavepoint() throws SQLException {
return this.conn.setSavepoint();
}
public Savepoint setSavepoint(String name) throws SQLException {
return this.conn.setSavepoint(name);
}
public void setTransactionIsolation(int level) throws SQLException {
this.conn.setTransactionIsolation(level);
}
public void setTypeMap(Map arg0) throws SQLException {
this.conn.setTypeMap(arg0);
}
}
新建一个Concrete Decorator
package com.javapatterns.decorator.jdbc;
import java.sql.Connection;
import java.sql.SQLException;
public class PooledConnection extends ConnectionDectorator {
private ConnectionPool pool;
public PooledConnection(ConnectionPool pool, Connection conn) {
super(conn);
this.pool = pool;
}
@Override
public void close() throws SQLException {
pool.releaseConncetion(conn);
}
}
修改DBConnectionPool
package com.javapatterns.decorator.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
public class DBConnectionPool implements ConnectionPool{
private Vector pool;
private Map stmtPool = new HashMap();
private static final int POOL_MAX_SIZE = 30;
public synchronized Connection getConnection() {
if(pool == null)
pool = new Vector();
Connection conn;
if (pool.isEmpty())
conn = createConnection();
else {
int last_idx = pool.size() - 1;
conn = (Connection)pool.get(last_idx);
pool.remove(last_idx);
}
//decorator implementation
return new PooledConnection(this, conn);
}
...
}
当有多个Concrete Decorator类动态组合时,更能体现装饰模式的威力。当然,这里用动态代理代码会更简洁一些。
- 大小: 29.5 KB
分享到:
相关推荐
装饰模式(Decorator Pattern)是一种结构型设计模式,它在不改变原有对象的基础上,通过包裹一个对象并为其添加新的行为或责任,实现对对象功能的扩展。这种模式在软件开发中非常常见,尤其当需要在运行时动态改变...
在"DecoratorPattern.rar"这个压缩包中,包含了装饰器模式的简单案例demo和一个使用MindMaster绘制的脑图。通过这个案例,我们可以深入理解装饰器模式的工作原理和应用场景。 1. 装饰器模式的基本结构: - ...
在"03_DecoratorPattern 小菜扮靓"这个主题中,我们可以推断作者可能以轻松幽默的方式介绍了装饰器模式的概念。"小菜"可能是用来比喻待装饰的对象,"扮靓"则表示通过装饰增强其功能或特性。博文链接指向了iteye博客...
装饰者模式(Decorator Pattern)是一种结构型设计模式,它的定义是在不改变原有对象结构的基础上,动态地给该对象增加一些职责(即增加其额外功能)。这种模式允许向一个现有的对象添加新的功能,同时又不改变其...
装饰模式(Decorator Pattern)是一种结构型设计模式,它允许你向一个现有的对象添加新的功能,同时又不改变其结构。装饰模式通过创建一个装饰类,该类包装了原始类的实例,并在调用原始类方法之前或之后添加额外的...
装饰者模式(Decorator Pattern)是软件工程中一种用于动态地给对象添加职责的设计模式。它允许我们独立于对象的类来扩展对象的功能,无需修改原有代码就能增加新功能,遵循“开闭原则”。这种模式是一种结构型设计...
装饰者模式(Decorator Pattern)是一种结构型设计模式,它允许我们向对象添加新的行为或职责,而无需修改对象的原始代码。在C++中实现装饰者模式,可以让我们灵活地扩展对象的功能,同时保持代码的可读性和可维护性...
这是一个关于设计模式中的装饰者模式例子,C#,Vs205,Unity 5.6.3f1 (64-bit)有需要的请下载!本来不想设置积分,可是最低1分,没办法!
4. **Concrete Decorator(具体装饰者)**:实现了Decorator接口,添加了一些新的行为或属性,具体装饰者通常会持有并调用Component对象的某个方法,然后添加额外的功能。 在提供的"Test"文件中,可能包含了一个...
在"decoratorPattern"这个示例中,我们可能看到一个简单的Java实现,其中包含了一个核心组件(Component)接口或抽象类,一个具体组件(Concrete Component)类,以及一个或多个装饰器(Decorator)类。装饰器类通常...
装饰模式(Decorator Pattern)是一种结构型设计模式,它在不修改已有对象的代码情况下,通过增加额外的行为来扩展对象的功能。这种模式的核心在于装饰类与被装饰类具有相同的接口,这样客户端可以像处理原始对象...
装饰模式(Decorator Pattern)是一种结构型设计模式,它可以在不修改原有对象的基础上,通过添加新的职责来扩展对象的功能。装饰模式的核心在于它定义了一个与原类一致的接口,使得装饰类和原类可以互相替换,而...
装饰器模式(Decorator Pattern)是一种结构型设计模式,它允许在运行时向对象添加新的行为或职责,而无需修改对象的原始代码。在C#中,装饰器模式通过创建一个包装对象,该对象拥有与原对象相同的接口,来实现对原...
Java 实现装饰器模式(Decorator Pattern) 装饰器模式是结构型设计模式之一,它允许向一个现有的对象添加新的功能,同时又不改变其结构。这种模式创建了一个装饰类,用来包装原有的类,并在保持类方法签名完整性的...
【Java装饰模式(Decorator Pattern)】装饰模式是一种结构型设计模式,它允许在不修改已有对象的基础上,通过添加额外的功能来扩展对象的行为。这种模式的关键在于装饰类与被装饰类有相同的接口,使得它们可以互换...
装饰器模式(Decorator Pattern)是一种结构型设计模式,它允许在运行时给对象添加新的行为或职责,同时保持对象的接口不变。这种模式的核心在于,它动态地将责任附加到对象上,通过将对象包装在一个装饰类中来扩展...
在给定的压缩包文件中,包含了九种经典的设计模式示例,它们分别是:单例模式(Singleton)、策略模式(StrategyPattern)、适配器模式(AdapterPattern)、装饰者模式(DecoratorPattern)、抽象工厂模式...
装饰模式(Decorator Pattern)是设计模式中的一种结构型模式,它允许在运行时给对象添加新的行为或职责,而无需改变对象的类。在Java中,装饰模式通常通过继承和组合来实现,使得代码具有更好的扩展性和灵活性。...
在给定的代码文件`DecoratorPattern.cpp`和`DecoratorPattern.h`中,我们可以预期看到以下内容: - `DecoratorPattern.h`可能包含了抽象组件、装饰器和具体装饰器的类定义。抽象组件定义了公共接口,装饰器继承了这...