设计模式-装饰器模式

Posted fanfan-90

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式-装饰器模式相关的知识,希望对你有一定的参考价值。

 

案例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;
        }
    }
View Code

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();
        }
    }
View Code

 

 

 

未完待续...

以上是关于设计模式-装饰器模式的主要内容,如果未能解决你的问题,请参考以下文章

编程模式之Go语言如何实现装饰器

设计模式之装饰器模式

装饰器模式

C++装饰器模式的实现

C++装饰器模式的实现

C++装饰器模式的实现