装饰class或参数是什么意思?
What does it mean to decorate a class or parameter?
修饰或向 class 或参数添加属性是什么意思?
目的是什么,我什么时候这样做?
欢迎链接到资源和直接回答。
装饰器是四人组中描述的原始 23 种模式之一 "Design Patterns" book. They describe it well here。
总结:
Decorator : Add additional functionality to a class at runtime where
subclassing would result in an exponential rise of new classes
模式与语言无关。它们描述了面向对象编程中常见问题的解决方案。在不参考特定语言的情况下讨论它们是可能的,甚至是首选的。原书中的例子是用C++和Smalltalk写的。当本书于 1995 年首次出版时,Java 和 C# 都不存在。
当您在 C# 中添加装饰器时,就像在 class/method 中添加一个 属性。会有附加属性。
如果你写单元测试你会遇到这样一个简单的装饰器TestMethod
:
[TestMethod]
public void TestMethod1()
{
}
框架将使用装饰器来检查测试集中有哪些测试方法。
您可以查看属性 here
还有一篇关于 Writing Custom Attributes
的文章值得一读
装饰器不限于“[]”形式的装饰器。还有一个设计模式,其他人之前已经提到过。
修饰 class 意味着向现有 class 添加功能。例如,你有一个classSingingPerson
,有歌唱天赋。
public class SingingPerson
{
public string Talent = "I can sing";
public string DoTalent()
{
return Talent;
}
}
后来,您决定 SingingPerson
class 也应该可以跳舞,但不想改变现有的 class 结构。您所做的是通过创建另一个包含添加功能的 class 来装饰 SingingPerson
class。您将要创建的这个新 class 包含一个 SinginPerson
对象。
public class SingingAndDancingPerson {
SingingPerson person;
public string Talent { get; set; }
public SingingAndDancingPerson(SingingPerson person)
{
this.person = person;
}
public string DoTalent()
{
this.Talent = person.Talent;
this.Talent += " and dance";
return this.Talent;
}
}
当您尝试创建这些 classes 的实例时,输出将如下所示:
SingingPerson person1 = new SingingPerson();
Console.WriteLine("Person 1: " + person1.DoTalent());
SingingAndDancingPerson person2 = new SingingAndDancingPerson(person1);
Console.WriteLine("Person 2: " + person2.DoTalent());
Console.ReadLine();
修饰或向 class 或参数添加属性是什么意思?
目的是什么,我什么时候这样做?
欢迎链接到资源和直接回答。
装饰器是四人组中描述的原始 23 种模式之一 "Design Patterns" book. They describe it well here。
总结:
Decorator : Add additional functionality to a class at runtime where subclassing would result in an exponential rise of new classes
模式与语言无关。它们描述了面向对象编程中常见问题的解决方案。在不参考特定语言的情况下讨论它们是可能的,甚至是首选的。原书中的例子是用C++和Smalltalk写的。当本书于 1995 年首次出版时,Java 和 C# 都不存在。
当您在 C# 中添加装饰器时,就像在 class/method 中添加一个 属性。会有附加属性。
如果你写单元测试你会遇到这样一个简单的装饰器TestMethod
:
[TestMethod]
public void TestMethod1()
{
}
框架将使用装饰器来检查测试集中有哪些测试方法。
您可以查看属性 here
还有一篇关于 Writing Custom Attributes
的文章值得一读装饰器不限于“[]”形式的装饰器。还有一个设计模式,其他人之前已经提到过。
修饰 class 意味着向现有 class 添加功能。例如,你有一个classSingingPerson
,有歌唱天赋。
public class SingingPerson
{
public string Talent = "I can sing";
public string DoTalent()
{
return Talent;
}
}
后来,您决定 SingingPerson
class 也应该可以跳舞,但不想改变现有的 class 结构。您所做的是通过创建另一个包含添加功能的 class 来装饰 SingingPerson
class。您将要创建的这个新 class 包含一个 SinginPerson
对象。
public class SingingAndDancingPerson {
SingingPerson person;
public string Talent { get; set; }
public SingingAndDancingPerson(SingingPerson person)
{
this.person = person;
}
public string DoTalent()
{
this.Talent = person.Talent;
this.Talent += " and dance";
return this.Talent;
}
}
当您尝试创建这些 classes 的实例时,输出将如下所示:
SingingPerson person1 = new SingingPerson();
Console.WriteLine("Person 1: " + person1.DoTalent());
SingingAndDancingPerson person2 = new SingingAndDancingPerson(person1);
Console.WriteLine("Person 2: " + person2.DoTalent());
Console.ReadLine();