结构型模式——装饰器模式 示例代码

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace DecoratorGiven
{
    /// <summary>
    /// 一个组件类。
    /// </summary>
    public class Photo : Form
    {
        Image image;

        public Photo()
        {
            image = new Bitmap(@"L:/我的文档/My Pictures/http_imgload2.jpg");
            this.Size = image.Size;
            this.Text = "Baby Wife";
            this.Paint += new PaintEventHandler(Drawer);

        }

        public virtual void Drawer(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(image, e.ClipRectangle);//将图片绘画在整个窗体
        }

        static void Main()
        {
            Photo p = new Photo();
            TaggedPhoto foodTaggedPhoto, colorTagedPhoto, tag;
            BorderedPhoto composition;
            Hominid h;
            Application.Run(p);//显示原始图像

            foodTaggedPhoto = new TaggedPhoto(p, "Baby");//添加一个文字标签
            colorTagedPhoto = new TaggedPhoto(foodTaggedPhoto, "Love");//再次添加一个文字标签
            composition = new BorderedPhoto(colorTagedPhoto, Color.Blue);
            //合成标签1、标签2、边框的Photo,最后显示的是一个边框Photo。
            h = new Hominid(composition, Color.Green);//Hominid,绿色弧线。
            Application.Run(composition);
            Console.WriteLine(colorTagedPhoto.ListTaggedPhotos());
            Application.Run(h);

            p = new Photo();
            tag = new TaggedPhoto(p, "Sandy");
            composition = new BorderedPhoto(tag, Color.Yellow);
            Application.Run(composition);
            Console.WriteLine(tag.ListTaggedPhotos());
            Console.ReadKey();
        }
    }

    /// <summary>
    /// 画了红线。
    /// </summary>
    public class Hominid : Photo
    {
        Photo photo;
        Color color;
        public Hominid(Photo p, Color c)
        {
            photo = p;
            color = c;
        }

        public override void Drawer(object sender, PaintEventArgs e)
        {
            photo.Drawer(sender, e);
            e.Graphics.DrawEllipse(new Pen(color), new RectangleF(new PointF(this.Location.X / 2, this.Location.Y / 2), new SizeF(this.Width, this.Height)));
        }
    }

    /// <summary>
    /// 边框窗体。
    /// </summary>
    public class BorderedPhoto : Photo
    {
        Photo photo;
        Color color;
        public BorderedPhoto(Photo p, Color c)
        {
            photo = p;
            color = c;
            this.Size = p.Size;
        }

        public override void Drawer(object sender, PaintEventArgs e)
        {
            photo.Drawer(sender, e);//将当前窗体进行重绘(基类窗体)
            e.Graphics.DrawRectangle(new Pen(color, 10), e.ClipRectangle);//绘画边框
        }
    }

    /// <summary>
    /// 添加文字标签的窗体。
    /// </summary>
    public class TaggedPhoto : Photo
    {
        Photo photo;
        string tag;
        int number;

        //static int count;//当前标签总和
        static List<string> tags = new List<string>();//当前标签,静态

        public TaggedPhoto(Photo p, string t)
        {
            photo = p;
            tag = t;
            tags.Add(t);
            number = tags.Count;//当前标签位置
        }

        public override void Drawer(object sender, PaintEventArgs e)
        {
            photo.Drawer(sender, e);
            e.Graphics.DrawString(tag, new Font("宋体", 16), new SolidBrush(Color.Black), new PointF(80, 10 + number * 20));
        }

        public string ListTaggedPhotos()
        {
            string s = "所有标签:";
            tags.ForEach(delegate(string ss)
            {
                s += ss;
            });
            return s;
        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值