【发布时间】:2016-12-17 18:38:04
【问题描述】:
我定义了一个接口,它有一个事件和一个如下定义的属性。
public interface IMyInterface
{
event EventHandler SomeEvent;
string GetName();
string IpAddress { get; set; }
}
然后我创建了一个类并使用它,一切正常。
现在我想使用装饰器扩展这个类。 我不确定如何处理该事件。对于属性我想我很清楚,只是想要确认。
我定义了装饰器类如下。
public class LoggerDecorator : IMyInterface
{
private readonly IMyInterface _MyInterface;
private readonly ILog _MyLog;
public LoggerDecorator(IMyInterface myInterface, ILog myLog)
{
if (myInterface == null)
throw new ArgumentNullException("IMyInterface is null");
_MyInterface = myInterface;
if (myLog == null)
throw new ArgumentNullException("ILog instance is null");
_MyLog = myLog;
}
public string GetName()
{
// If needed I can use log here
_MyLog.Info("GetName method is called.");
return _MyInterface.GetName();
}
// Is this the way to set properties?
public string IpAddress
{
get
{
return _MyInterface.IpAddress;
}
set
{
// If needed I can use log here
_MyLog.Info("IpAddress is set.");
_MyInterface.IpAddress = value;
}
}
// But How to handle this evetn?? Please help. I am not clear about this.
public event EventHandler SomeEvent;
}
【问题讨论】:
标签: c# design-patterns decorator