📄 decorator.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace DesignDll
{
/*
* 装饰模式 动态的给对象添加一些额外的职责,就增加功能来说,装饰模式比生成模式更加灵活
*/
/// <summary>
/// 定义一个人员类
/// </summary>
public class person
{
//构造函数
public person()
{
}
public ArrayList alPerson = new ArrayList();
//public ListBox listBox = new ListBox();
/// <summary>
/// 重写构造函数操作
/// </summary>
/// <param name="name"></param>
public person(string name)
{
this._name = name;
//this.listBox = listBox;
}
//人员姓名
private string _name;
//人员姓名
public string name
{
get { return name; }
set { _name = value; }
}
/// <summary>
/// 显示某某的操作过程
/// </summary>
public virtual void Show()
{
/// 某某的操作
//listBox.Items.Add(string.Format("装扮的{0}", _name));
alPerson.Add(string.Format("装扮的{0}", _name));
}
}
/// <summary>
/// 服饰类
/// </summary>
public class Finery : person
{
protected person Component;
public void Decorate(person component)
{
this.Component = component;
}
public override void Show()
{
if (Component != null)
{
Component.Show();
}
//base.Show();
}
}
/// <summary>
/// 装饰类 大T恤
/// </summary>
public class TShirte : Finery
{
public override void Show()
{
//
alPerson.Add("穿上大T恤");
base.Show();
}
}
/// <summary>
/// 装饰类 牛仔裤
/// </summary>
public class BigTrouser : Finery
{
public override void Show()
{
alPerson.Add("穿上牛仔裤");
base.Show();
}
}
/// <summary>
/// 装饰类 帽子
/// </summary>
public class Cat : Finery
{
public override void Show()
{
alPerson.Add("头上带帽子");
base.Show();
}
}
/// <summary>
/// 装饰类 运动鞋
/// </summary>
public class SportShoose : Finery
{
public override void Show()
{
alPerson.Add("脚上穿运动鞋");
base.Show();
}
}
/// <summary>
/// 装饰类 皮鞋
/// </summary>
public class SpShoose : Finery
{
public override void Show()
{
alPerson.Add("脚上穿皮鞋");
//base.Show();
}
}
/// <summary>
/// 抽象的操作类,其中抽象出一个方法
/// </summary>
abstract class Component
{
public abstract void Operation();
}
/// <summary>
/// 继承抽象的方法,重写了一个方法
/// </summary>
class ConcreteComponent :Component
{
public override void Operation()
{
Console.WriteLine("具体操作对象!");
//throw new Exception("The method or operation is not implemented.");
}
}
/// <summary>
/// 抽象类也可以继承抽象类
/// </summary>
abstract class Decorator : Component
{
protected Component component;
public void SetComponent(Component _component)
{
this.component = _component;
}
public override void Operation()
{
if (component != null)
component.Operation();
//throw new Exception("The method or operation is not implemented.");
}
}
/// <summary>
/// 用来继承该Decorator类
/// </summary>
class ConcreateDescoratorA : Decorator
{
private string _stype;
public string stype
{
get { return _stype; }
set { _stype = value; }
}
public override void Operation()
{
base.Operation();
stype = "ConcreateDescoratorA";
Console.WriteLine("ConcreateDescoratorA 操作");
}
}
/// <summary>
///
/// </summary>
class ConcreateDescoratorB : Decorator
{
public override void Operation()
{
base.Operation();
Console.WriteLine("ConcreateDescoratorB 操作");
}
public void GetConcreateDescoratorB()
{
Console.WriteLine("GetConcreateDescoratorB 操作");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -