Esempio n. 1
0
        public void Test()
        {
            // 建立对象,并对其进行两次装饰。 bold = false, color= black
            IText text = new TextObject();

            text = new BoldDecorator(text);
            text = new ColorDecorator(text);
            Assert.AreEqual <string>("<Black>hello</Black>", text.Content);

            // 动态找到需要更新的Decorator并修改相应属性
            // bold = false, color = red
            ColorState newColorState = new ColorState();

            newColorState.Color = Color.Red;
            IDecorator root = (IDecorator)text;

            root.Refresh <ColorDecorator>(newColorState);
            Assert.AreEqual <string>("<Red>hello</Red>", text.Content);

            // 动态找到需要更新的Decorator并修改相应属性
            // bold = true, color = red
            BoldState newBoldState = new BoldState();

            newBoldState.IsBold = true;
            root.Refresh <BoldDecorator>(newBoldState);
            Assert.AreEqual <string>("<Red><b>hello</b></Red>", text.Content);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            IText text = new TextObject();

            //默认不加粗、黑色字体
            text = new BoldDecorator(text);
            text = new ColorDecorator(text);
            Console.WriteLine(text.Content);  //< Black > hello </ Black >

            //修改为加粗、红色字体
            ColorState colorState = new ColorState();

            colorState.Color = Color.Red;
            BoldState boldState = new BoldState();

            boldState.IsBold = true;
            IDecorator root = (IDecorator)text;

            root.Refresh <ColorDecorator>(colorState);
            root.Refresh <BoldDecorator>(boldState);
            Console.WriteLine(text.Content); //< Red >< b > hello </ b ></ Red >

            //取消颜色设置
            colorState = null;
            root.Refresh <ColorDecorator>(colorState);
            Console.WriteLine(text.Content); //< b > hello </ b >
        }