CSharp: Decorator Pattern in donet 6
/// <summary> ///装饰模式 Decorator Pattern 亦称: 装饰者模式、装饰器模式、Wrapper、Decorator /// </summary> public interface IDataSource { /// <summary> /// /// </summary> /// <param name="data"></param> void Write(string data); /// <summary> /// /// </summary> /// <returns></returns> string Read(); /// <summary> /// /// </summary> void ClearContent(); } /// <summary> /// 装饰模式 Decorator Pattern 亦称: 装饰者模式、装饰器模式、Wrapper、Decorator /// </summary> public class File : IDataSource { /// <summary> /// /// </summary> private readonly string _name; /// <summary> /// /// </summary> private readonly StringBuilder _data; /// <summary> /// /// </summary> /// <param name="name"></param> public File(string name) { _name = name; _data = new StringBuilder(); } /// <summary> /// /// </summary> /// <param name="data"></param> public void Write(string data) { _data.Append(data); Console.WriteLine($"写入文件: {_name}"); Console.WriteLine($"写成: {data}"); } /// <summary> /// /// </summary> /// <returns></returns> public string Read() => _data.ToString(); /// <summary> /// /// </summary> public void ClearContent() => _data.Clear(); } using Geovin.Du.DuDecorator.DataStorage.Components.Common; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Geovin.Du.DuDecorator.DataStorage.Decorators.Common { /// <summary> /// 装饰模式 Decorator Pattern 亦称: 装饰者模式、装饰器模式、Wrapper、Decorator /// </summary> public abstract class DataSourceDecorator : IDataSource { /// <summary> /// /// </summary> protected readonly IDataSource _wrapee; /// <summary> /// /// </summary> /// <param name="dataSource"></param> protected DataSourceDecorator(IDataSource dataSource) { _wrapee = dataSource; } // The base decorator simply delegates all work to the wrapped component. // Extra behaviors can be added in concrete decorators. /// <summary> /// /// </summary> /// <param name="data"></param> public virtual void Write(string data) => _wrapee.Write(data); // With this approach, concrete decorators can call the parent implementation of the operation // instead of calling the wrapped object directly. // That simplifies extension of decorator classes. /// <summary> /// /// </summary> /// <returns></returns> public virtual string Read() => _wrapee.Read(); /// <summary> /// /// </summary> public void ClearContent() => _wrapee.ClearContent(); } } /// <summary> /// 装饰模式 Decorator Pattern 亦称: 装饰者模式、装饰器模式、Wrapper、Decorator /// </summary> public class CompressionDecorator : DataSourceDecorator { /// <summary> /// /// </summary> private readonly string _compressionBlock; /// <summary> /// /// </summary> /// <param name="wrapee"></param> public CompressionDecorator(IDataSource wrapee) : base(wrapee) { _compressionBlock = "--COMPRESSED--"; } /// <summary> /// /// </summary> /// <param name="data"></param> public override void Write(string data) { Console.WriteLine("压缩"); var compressedData = Compress(data); _wrapee.Write(compressedData); } /// <summary> /// /// </summary> /// <returns></returns> public override string Read() { var compressedData = _wrapee.Read(); return Decompress(compressedData); } /// <summary> /// /// </summary> /// <param name="data"></param> /// <returns></returns> private string Compress(string data) => $"{_compressionBlock}{data}{_compressionBlock}"; /// <summary> /// /// </summary> /// <param name="data"></param> /// <returns></returns> private string Decompress(string data) => data.Replace(_compressionBlock, string.Empty, StringComparison.InvariantCulture); } /// <summary> /// 装饰模式 Decorator Pattern 亦称: 装饰者模式、装饰器模式、Wrapper、Decorator /// </summary> public class EncryptionDecorator : DataSourceDecorator { /// <summary> /// /// </summary> private readonly string _encryptionBlock; /// <summary> /// /// </summary> /// <param name="wrapee"></param> public EncryptionDecorator(IDataSource wrapee) : base(wrapee) { _encryptionBlock = "##3NCRYPT3D##"; } /// <summary> /// /// </summary> /// <param name="data"></param> public override void Write(string data) { Console.WriteLine("加密"); var encryptedData = Encrypt(data); _wrapee.Write(encryptedData); } /// <summary> /// /// </summary> /// <returns></returns> public override string Read() { var encryptedData = _wrapee.Read(); return Decrypt(encryptedData); } /// <summary> /// /// </summary> /// <param name="data"></param> /// <returns></returns> private string Encrypt(string data) => $"{_encryptionBlock}{data}{_encryptionBlock}"; /// <summary> /// /// </summary> /// <param name="data"></param> /// <returns></returns> private string Decrypt(string data) => data.Replace(_encryptionBlock, string.Empty, StringComparison.InvariantCulture); } /// <summary> /// 装饰模式 Decorator Pattern 亦称: 装饰者模式、装饰器模式、Wrapper、Decorator /// </summary> public static class DataStorageExecutor { /// <summary> /// /// </summary> public static void Execute() { ConsoleExtension.WriteSeparator("装饰模式 Decorator Pattern--Data storage demo"); ProcessFileWithPlainData(); ProcessFileWithCompressedData(); ProcessFileWithCompressedAndEncryptedData(); } /// <summary> /// /// </summary> private static void ProcessFileWithPlainData() { IDataSource source = new Components.File("file.dat"); WriteAndRead(source); } /// <summary> /// /// </summary> private static void ProcessFileWithCompressedData() { IDataSource source = new Components.File("file.dat"); source = new CompressionDecorator(source); WriteAndRead(source); } /// <summary> /// /// </summary> private static void ProcessFileWithCompressedAndEncryptedData() { IDataSource source = new Components.File("file.dat"); source = new EncryptionDecorator(source); source = new CompressionDecorator(source); WriteAndRead(source); } /// <summary> /// /// </summary> /// <param name="source"></param> private static void WriteAndRead(IDataSource source) { source.Write("Hello world"); Console.WriteLine($"Read: {source.Read()}\n"); } }
调用:
Geovin.Du.DuDecorator.DataStorage.DataStorageExecutor.Execute(); Console.WriteLine();
输出:
Hello, Geovin Du! 学习 .net 6 装饰模式 Decorator Pattern--Data storage demo -------------------------------------------------- 写入文件: file.dat 写成: Hello world Read: Hello world 压缩 写入文件: file.dat 写成: --COMPRESSED--Hello world--COMPRESSED-- Read: Hello world 压缩 加密 写入文件: file.dat 写成: ##3NCRYPT3D##--COMPRESSED--Hello world--COMPRESSED--##3NCRYPT3D## Read: Hello world
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)