Bir koleksiyona bir şey eklemenin en yaygın yolunun, Addbir koleksiyonun sağladığı bir tür yöntemi kullanmak olduğunu düşünüyorum:
class Item {}
var items = new List<Item>();
items.Add(new Item());
ve aslında bu konuda alışılmadık bir şey yok.
Ancak merak ediyorum neden böyle yapmıyoruz:
var item = new Item();
item.AddTo(items);
ilk yönteme göre bir şekilde daha doğal görünüyor . Bu, Itemsınıfın aşağıdaki gibi bir özelliği olduğunda Parent:
class Item
{
public object Parent { get; private set; }
}
ayarlayıcıyı özel yapabilirsiniz. Bu durumda elbette bir genişletme yöntemi kullanamazsınız.
Ama belki yanılıyorum ve bu modeli daha önce hiç görmedim çünkü bu çok nadir? Böyle bir kalıp olup olmadığını biliyor musunuz?
Gelen C#bir uzantı yöntem için yararlı olacaktır
public static T AddTo(this T item, IList<T> list)
{
list.Add(item);
return item;
}
Diğer dillere ne dersiniz? Sanırım birçoğunda Itemsınıf bir ICollectionItemarabirim diyelim .
Güncelleme-1
Biraz daha düşünüyorum ve birden fazla koleksiyona bir öğe eklemek istemiyorsanız bu desen gerçekten yararlı olacaktır.
test ICollectablearayüzü:
interface ICollectable<T>
{
// Gets a value indicating whether the item can be in multiple collections.
bool CanBeInMultipleCollections { get; }
// Gets a list of item's owners.
List<ICollection<T>> Owners { get; }
// Adds the item to a collection.
ICollectable<T> AddTo(ICollection<T> collection);
// Removes the item from a collection.
ICollectable<T> RemoveFrom(ICollection<T> collection);
// Checks if the item is in a collection.
bool IsIn(ICollection<T> collection);
}
ve örnek bir uygulama:
class NodeList : List<NodeList>, ICollectable<NodeList>
{
#region ICollectable implementation.
List<ICollection<NodeList>> owners = new List<ICollection<NodeList>>();
public bool CanBeInMultipleCollections
{
get { return false; }
}
public ICollectable<NodeList> AddTo(ICollection<NodeList> collection)
{
if (IsIn(collection))
{
throw new InvalidOperationException("Item already added.");
}
if (!CanBeInMultipleCollections)
{
bool isInAnotherCollection = owners.Count > 0;
if (isInAnotherCollection)
{
throw new InvalidOperationException("Item is already in another collection.");
}
}
collection.Add(this);
owners.Add(collection);
return this;
}
public ICollectable<NodeList> RemoveFrom(ICollection<NodeList> collection)
{
owners.Remove(collection);
collection.Remove(this);
return this;
}
public List<ICollection<NodeList>> Owners
{
get { return owners; }
}
public bool IsIn(ICollection<NodeList> collection)
{
return collection.Contains(this);
}
#endregion
}
kullanımı:
var rootNodeList1 = new NodeList();
var rootNodeList2 = new NodeList();
var subNodeList4 = new NodeList().AddTo(rootNodeList1);
// Let's move it to the other root node:
subNodeList4.RemoveFrom(rootNodeList1).AddTo(rootNodeList2);
// Let's try to add it to the first root node again...
// and it will throw an exception because it can be in only one collection at the same time.
subNodeList4.AddTo(rootNodeList1);
add(item, collection), ama bu iyi bir OO stili değil.
item.AddTo(items)uzantı yöntemleri olmadan bir diliniz olduğunu varsayalım: doğal ya da değil, addTo'u desteklemek için her türün bu yönteme ihtiyacı vardır ve uygulamayı destekleyen her tür koleksiyon için bunu sağlar. Bu, şimdiye kadar duyduğum her şey arasında bağımlılık getirmenin en iyi örneği gibi : P - Bence buradaki yanlış öneri, 'gerçek' hayata bazı programlama soyutlamalarını modellemeye çalışıyor. Bu genellikle yanlış gidiyor.