Quiero ser capaz de definir algunos objetos y adjuntar algunos "comportamientos" a ese objeto donde la implementación está en el comportamiento que no está en el objeto. Rails-like: acts_as_taggable. Como ejemplo concreto, quiero decir que las Tareas se pueden etiquetar. No quiero tener que codificar nada en Tarea sobre etiquetas más allá de "habilitar" el comportamiento a través de ... ¿una interfaz? Ahí radica mi pregunta. No puede poner la implementación en una interfaz. No quiero contaminar mi clase BaseObject [abstract?] Con todos de las posibles implementaciones.C# - Cómo hacer MÚLTIPLES "mixins" correctamente con interfaces y/o clases abstractas
Objetos: tarea, un
Comportamientos:. Taggable, enviarse por correo electrónico, imprimir, aplazable (
Una tarea puede ser etiquetado, enviado por correo electrónico, impreso, y se remitió Una nota puede ser etiquetada, enviado por correo electrónico, impreso , pero no diferido.
baseObject
public class BaseObject
{
Guid ID { get; set; }
}
tag.cs
public class Tag : BaseObject
{
public Guid Id { get; set; }
public String Title { get; set; }
}
itaggable.cs
public interface ITaggable
{
void AddTag(Tag tag);
... other Tag methods ...
}
task.cs
public class Task : BaseObject, ITaggable, IEmailable, IPrintable
{
Task specified functionality... nothing about "taggging"
}
note.cs
...
TagCollection.cs
public class TagCollection : List<Tag>
{
public string[] ToStringArray()
{
string[] s = new string[this.Count];
for (int i = 0; i < this.Count; i++)
s[i] = this[i].TagName;
return s;
}
public override string ToString()
{
return String.Join(",", this.ToStringArray());
}
public void Add(string tagName)
{
this.Add(new Tag(tagName));
}
Implementación de ITaggable se ve algo como
{
private TagCollection _tc;
private TagCollection tc
{
get
{
if (null == _tc)
{
_tc = new TagCollection();
}
return _tc;
}
set { _tc = value; }
}
public void AddTag(Tag tag)
{
tc.Add(tag);
}
public void AddTags(TagCollection tags)
{
tc.AddRange(tags);
}
public TagCollection GetTags()
{
return tc;
}
}
Entonces, ¿cuál es la mejor forma correcta/para hacer esto?
Jason
Interesante, tendré que pensar en esto más. – user166255