C# 如何通过decorator设计模式扩展密封类?
装饰图案用法:C# 如何通过decorator设计模式扩展密封类?,c#,oop,design-patterns,C#,Oop,Design Patterns,装饰图案用法: 任何人都可以演示如何通过装饰模式扩展密封类功能。从.NET中继承是不可能的。所以,若您想创建继承您的密封类的decorator,那个么答案是——这是不可能的 public class Decorator : SealedClass // not possible 如果您的密封类实现了一些接口或继承了一些基类,那么您可以创建继承相同类/接口的装饰器。Decorator在客户看来并不像你们的类,但你们可以用它来装饰你们的类 public sealed class SealedCla
任何人都可以演示如何通过装饰模式扩展密封类功能。从.NET中继承是不可能的。所以,若您想创建继承您的密封类的decorator,那个么答案是——这是不可能的
public class Decorator : SealedClass // not possible
如果您的密封类实现了一些接口或继承了一些基类,那么您可以创建继承相同类/接口的装饰器。Decorator在客户看来并不像你们的类,但你们可以用它来装饰你们的类
public sealed class SealedClass : InterfaceA
public class Decorator : InterfaceA
另一个选项-创建一些新接口(它可以看起来完全像您的密封类接口)并为此接口实现decorator。然后为此接口创建密封类适配器。客户端将使用您的新界面。您将能够装饰适配器
public class Decorator : InterfaceB // same members as SealedClass have
public class Adapter : InterfaceB
{
private SealedClass adaptee;
public Adapter(SealedClasss adaptee)
{
this.adaptee = adaptee;
}
public int Member1()
{
return adaptee.Member1();
}
// etc for each member of SealedClass
}
您的密封类实现了一些接口还是有一些基类?在这里看到类定义会很好