装饰者模式_Decorator


装饰者模式:

例子:对StringBuilder进行装饰

public class MyString
    {
        private StringBuilder sb = new StringBuilder();
        public static implicit operator MyString(string s)
        {
            var ms = new MyString();
            ms.sb.Append(s);
            return ms;
        }

        public static MyString operator +(MyString msb,string s)
        {
            msb.Append(s);
            return msb;
        }
        public MyString Append(string s)
        {
            sb.Append(s);
            return this;
        }

        public override string ToString()
        {
            return sb.ToString();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            MyString my = "123";
            my += "456";
            Console.WriteLine(my);
        }
    }