• 设计模式》装饰器模式


    例子

    • 本地文件,加密装饰器,压缩装饰器
        public interface IDataSource
        {
            /// <summary>
            /// 写入
            /// </summary>
            /// <param name="id"></param>
            /// <param name="data"></param>
            void WriteData(string data);
    
    
            /// <summary>
            /// 读取
            /// </summary>
            /// <param name="id"></param>
            /// <returns></returns>
            string ReadData();
        }
    
        public class FileDataSource : IDataSource
        {
            private readonly string _fileName;
    
            public FileDataSource(string fileName)
            {
                _fileName = fileName;
            }
    
            public void WriteData(string data)
            {
                using var file = File.Open(_fileName, FileMode.OpenOrCreate);
                var bytes = Encoding.UTF8.GetBytes(data);
                file.Write(bytes, 0, bytes.Length);
            }
    
            public string ReadData()
            {
                return File.ReadAllTextAsync(_fileName).Result;
            }
        }
    
        public abstract class DataSourceDecorator : IDataSource
        {
            private readonly IDataSource _dataSource;
    
            public DataSourceDecorator(IDataSource dataSource)
            {
                _dataSource = dataSource;
            }
    
            public virtual void WriteData(string data)
            {
                _dataSource.WriteData(data);
            }
    
            public virtual string ReadData()
            {
                return _dataSource.ReadData();
            }
        }
    
    
        public class EncryptionDecorator : DataSourceDecorator
        {
            public EncryptionDecorator(IDataSource dataSource) : base(dataSource)
            {
            }
    
            private string Encode(string data)
            {
                var bytes = Encoding.UTF8.GetBytes(data);
                return Convert.ToBase64String(bytes);
            }
    
            private string Decode(string data)
            {
                return Encoding.UTF8.GetString(Convert.FromBase64String(data));
            }
    
    
            public override string ReadData()
            {
                var data = base.ReadData();
                string deCodeResult = Decode(data);
                return deCodeResult;
            }
    
            public override void WriteData(string data)
            {
                string enCode = Encode(data);
                base.WriteData(enCode);
            }
        }
    
        public class CompressionDecorator : DataSourceDecorator
        {
            public CompressionDecorator(IDataSource dataSource) : base(dataSource)
            {
            }
    
            public override string ReadData()
            {
                var data = base.ReadData();
                string deCodeResult = CompressHelper.DecompressString(data);
                return deCodeResult;
            }
    
            public override void WriteData(string data)
            {
                string enCode = CompressHelper.CompressString(data);
                base.WriteData(enCode);
            }
        }
    
    
            private static void HardFileTest()
            {
                var file = new FileDataSource("1.txt");
                var encode = new EncryptionDecorator(file);
                var compress = new CompressionDecorator(encode);
                compress.WriteData("123123123");
                Console.WriteLine(compress.ReadData());
            }
    


    参考地址

  • 相关阅读:
    使用ANT入门例子
    React 项目中使用create-react-app创建项目引入antd样式不起作用的问题
    js中对象数组遍历,及区别
    vue中axios封装
    uni-app中开发需要注意事项
    git 提交代码和 合并分支 ,回滚代码
    vue..js3.0
    vue中强制组件重新渲染
    vuex中获取当前路由
    H5 video在移动端播放层级问题
  • 原文地址:https://www.cnblogs.com/icxldd/p/15799639.html
Copyright © 2020-2023  润新知