定义:
代理模式模式:代理模式给某一个对象提供一个代理对象,并由代理对象控制对原对象的引用。通俗的来讲代理模式就是我们生活中常见的中介。代理类与被代理类都实现相同接口
装饰器模式与代理模式的区别:
两者都对类进行扩展,只是作用不同。
它们相同点是:代理类、被代理类继承一个接口,装饰类与被装饰类也继承一个接口
代理模式强调让别人帮你做和业务无关的工作(记录日志、缓存)。代理模式为了实现对对象的控制,因为被代理类往往不希望直接获得
装饰器模式强调增强自身,扩展业务,装饰模式是对客户以透明的方式扩展对象功能,是继承方案的一个替代方案
静态代理:
每一个被代理类都创建一个代理类,用来增加业务无关的功能,缺点是代码量大,代理模式实现了AOP思想
动态代理:
不需要针对每个被代理类都增加一个代理类,可以借助一些组件实现
案例1:代理Connection、Command对象,为增删改操作添加事件
Connection
public sealed class SqlConnectionWrapper : IDbConnection { private Action<IDbCommand> trace = null; private IDbConnection connection = null; public SqlConnectionWrapper(IDbConnection connection, Action<IDbCommand> trace=null) { this.connection = connection; this.trace = trace; } public event Action<IDbCommand> Trace { add { trace += value; } remove { trace -= value; } } public string ConnectionString { get => connection.ConnectionString; set => connection.ConnectionString = value; } public int ConnectionTimeout => connection.ConnectionTimeout; public string Database => connection.Database; public ConnectionState State => connection.State; public IDbTransaction BeginTransaction() { return connection.BeginTransaction(); } public IDbTransaction BeginTransaction(IsolationLevel il) { return connection.BeginTransaction(il); } public void ChangeDatabase(string databaseName) { connection.ChangeDatabase(databaseName); } public void Close() { connection.Close(); } public IDbCommand CreateCommand() { var cmd = connection.CreateCommand(); return new SqlCommandWrapper(cmd, this.trace); } public void Dispose() { connection.Dispose(); } public void Open() { connection.Open(); } public override string ToString() { return this.connection.ConnectionString; } }
Command
public sealed class SqlCommandWrapper : IDbCommand { private Action<IDbCommand> trace = null; private IDbCommand command = null; public SqlCommandWrapper(IDbCommand command, Action<IDbCommand> trace = null) { this.command = command; this.trace = trace; } public string CommandText { get => command.CommandText; set => command.CommandText = value; } public int CommandTimeout { get => command.CommandTimeout; set => command.CommandTimeout = value; } public CommandType CommandType { get => command.CommandType; set => command.CommandType = value; } public IDbConnection Connection { get => command.Connection; set => command.Connection = value; } public IDataParameterCollection Parameters { get => command.Parameters; } public IDbTransaction Transaction { get => command.Transaction; set => command.Transaction = value; } public UpdateRowSource UpdatedRowSource { get => command.UpdatedRowSource; set => command.UpdatedRowSource = value; } public void Cancel() { command.Cancel(); } public IDbDataParameter CreateParameter() { return command.CreateParameter(); } public void Dispose() { command.Dispose(); } public int ExecuteNonQuery() { trace?.Invoke(this); return command.ExecuteNonQuery(); } public IDataReader ExecuteReader() { trace?.Invoke(this); return command.ExecuteReader(); } public IDataReader ExecuteReader(CommandBehavior behavior) { trace?.Invoke(this); return command.ExecuteReader(behavior); } public object ExecuteScalar() { trace?.Invoke(this); return command.ExecuteScalar(); } public void Prepare() { command.Prepare(); } }
未完待续...