首页 > 其他分享 >CSharp: Decorator Pattern in donet 6

CSharp: Decorator Pattern in donet 6

时间:2022-11-15 23:11:54浏览次数:42  
标签:donet string Pattern void source data public Decorator

 

    /// <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

  

 

标签:donet,string,Pattern,void,source,data,public,Decorator
From: https://www.cnblogs.com/geovindu/p/16894410.html

相关文章

  • CSharp: Composite Pattern in donet 6
     ///<summary>///组合模式CompositePattern亦称:对象树、ObjectTree、CompositePattern///</summary>publicabstractclassGift{......
  • AdoNetFirstDay
    01SqlConnectionDemousingSystem;usingSystem.Collections.Generic;usingSystem.Data.SqlClient;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;......
  • CSharp: Bridge Pattern in donet 6
     ///<summary>///桥接模式BridgePattern-StructuralPattern///</summary>publicabstractclassDocument{///<summary>......
  • CSharp: Adapter Pattern in donet 6
     ///<summary>///适配器模式亦称:封装器模式、Wrapper、AdapterAdapterPattern///</summary>publicclassEmployee{///<s......
  • [Design Patterns] Creational patterns创建型模式
    摘抄:这里最大的障碍在于硬编码被实例化的类。创建型模式提供了不同的方式来去除需要实例化它们的代码中对具体类的显示指定。如果CreateMaze调用虚函数而不是construct......
  • CSharp: Prototype Pattern in donet 6
     ///<summary>///原型模式PrototypePattern///</summary>publicinterfaceIColorPrototype{///<summary>///......
  • CSharp: Singleton Pattern in donet 6
     ///<summary>///单例模式单件模式、SingletonPattern///</summary>publicabstractclassBaseGreeter{publicvirtualvoid......
  • CSharp: Classes, Structures, And Records in c# 10(donet 6)
     #regionRecordTypesRecordClassinC#10///<summary>///Positionalparameterssyntax///位置参数的语法///</summary>///<pa......
  • Python decorator
    此处只做标记,没有深入更深层次了解!!!初步的了解,是decorator库可以通过注解的方式扩展方法,扩展是在不改变原有函数的情况下进行的,如下图所示:    另外,网上也看到一些......
  • CSharp: Abstract Factory Pattern in donet 6
     usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceGeovinu.Du.DuAbstractFactory......