其他分享
首页 > 其他分享> > 设计模式-》装饰器模式

设计模式-》装饰器模式

作者:互联网

例子

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


参考地址

标签:WriteData,string,ReadData,模式,装饰,dataSource,设计模式,data,public
来源: https://www.cnblogs.com/icxldd/p/15799639.html