Created
June 18, 2020 01:58
-
-
Save circleupx/b45befb07266b01283964b88c50837ce to your computer and use it in GitHub Desktop.
Extensible Object Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BaseObject : IExtensibleObject<BaseObject> | |
{ | |
private DateTime _startDate; | |
private ExtensionCollection<BaseObject> _extensions; | |
public DateTime StartDate | |
{ | |
get { return _startDate; } | |
set { _startDate = value; } | |
} | |
public BaseObject() | |
{ | |
StartDate = DateTime.Now; | |
_extensions = new ExtensionCollection<BaseObject>(this); | |
} | |
#region IExtensibleObject<BaseObject> Members | |
public IExtensionCollection<BaseObject> Extensions | |
{ | |
get | |
{ | |
return _extensions; | |
} | |
} | |
#endregion | |
} | |
public class DateTimeConverterExtension : IExtension<BaseObject> | |
{ | |
private BaseObject _owner; | |
#region IExtension<BaseObject> Members | |
public void Attach(BaseObject owner) | |
{ | |
_owner = owner; | |
_owner.StartDate = owner.StartDate.ToUniversalTime(); | |
} | |
public void Detach(BaseObject owner) | |
{ | |
_owner.StartDate = _owner.StartDate.ToLocalTime(); | |
} | |
#endregion | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
BaseObject obj = new BaseObject(); | |
Console.WriteLine("Local Time: "+obj.StartDate); | |
obj.Extensions.add(new DateTimeConverterExtension()); | |
Console.WriteLine("UTC: "+obj.StartDate); | |
DateTimeConverterExtension ext = obj.Extensions.Find<DateTimeConverterExtension>(); | |
obj.Extensions.Remove(ext); | |
Console.WriteLine("Local Time: "+obj.StartDate); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment