结构型模式——装饰器模式 示例代码
2009-02-10 16:59
295 查看
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;
}
}
}
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;
}
}
}
相关文章推荐
- 设计模式之抽象工厂模式代码示例
- 原型模式示例代码
- zeroMQ/jzmq REQ/REP(c/s)模式及java代码示例
- 【java】 代理模式代码示例
- Singleton模式的C++实现研究(示例代码)
- 命令模式详解及示例代码演示
- 合成模式代码示例
- 【设计模式】java单实例模式代码示例
- VBS中的Singleton类(单例模式)的代码示例
- 关于JAVA中状态设计模式的讲解示例代码
- 设计模式之访问者模式代码示例
- 装饰者模式示例代码
- 【JS设计模式】调停者模式代码示例
- Java使用设计模式中迭代器模式构建项目的代码结构示例
- runLoop运行模式示例代码
- java Signleton模式详解及示例代码
- 单例模式代码示例
- Prototype 模式示例代码 (C++)
- 设计模式之工厂模式代码示例
- ZeroMQ的ZMQ_STREAM模式写的服务器反向先发请求的示例代码