装饰者模式用于串起来各个部分
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DecorateSample
{
//虚拟构件
abstract class VisualCompont
{
public abstract void DisPlay();
}
//窗体具体构件类
class Windows:VisualCompont
{
public override void DisPlay()
{
Console.WriteLine("windows系统显示以下内容:");
}
}
//文本框具体构件类
class Linux : VisualCompont
{
public override void DisPlay()
{
Console.WriteLine("Linux显示以下内容:");
}
}
//抽象装饰类
class CompontDecorator:VisualCompont
{
private VisualCompont compont;//增加父类引用,关键标志
public CompontDecorator (VisualCompont compont)
{
this.compont = compont;
}
public override void DisPlay()
{
compont.DisPlay();
}
}
class JpgDecorator : CompontDecorator
{
public JpgDecorator(VisualCompont compont):base(compont)
{
}
public override void DisPlay()
{
base.DisPlay();
Console.WriteLine("jpg图片");
}
}
class PngDecorator : CompontDecorator
{
public PngDecorator(VisualCompont compont) : base(compont)
{
}
public override void DisPlay()
{
base.DisPlay();
Console.WriteLine("png图片");
}
}
class Program
{
static void Main(string[] args)
{
VisualCompont window, windowJpg, windowPng,windowAnd;
window = new Windows();
window.DisPlay();
Console.WriteLine("jpg装饰windows:--------");
windowJpg = new JpgDecorator(window);
windowJpg.DisPlay();
Console.WriteLine("png装饰windows:--------");
windowPng = new PngDecorator(window);
windowPng.DisPlay();
Console.WriteLine("jpg和png装饰windows:--------");
windowAnd=new PngDecorator(windowJpg);
windowAnd.DisPlay();
Console.Read();
}
}
}