ฉันคิดว่าวิธีทั่วไปในการเพิ่มบางอย่างลงในคอลเลกชันคือการใช้Add
วิธีการบางอย่างที่คอลเลกชันมีให้:
class Item {}
var items = new List<Item>();
items.Add(new Item());
และไม่มีอะไรผิดปกติเกี่ยวกับเรื่องนั้น
ฉันสงสัยว่าทำไมเราไม่ทำแบบนี้:
var item = new Item();
item.AddTo(items);
มันดูเป็นธรรมชาติมากกว่าวิธีแรกเลย นี้จะมี andvantange ว่าเมื่อItem
ชั้นมีคุณสมบัติเช่นParent
:
class Item
{
public object Parent { get; private set; }
}
คุณสามารถทำให้สุนัขเซทเทอร์เป็นส่วนตัวได้ ในกรณีนี้แน่นอนคุณไม่สามารถใช้วิธีการขยายได้
แต่บางทีฉันผิดและฉันไม่เคยเห็นรูปแบบนี้มาก่อนเพราะมันผิดปกติเหรอ? คุณรู้หรือไม่ว่ามีรูปแบบดังกล่าวหรือไม่
ในC#
วิธีการขยายจะเป็นประโยชน์สำหรับการที่
public static T AddTo(this T item, IList<T> list)
{
list.Add(item);
return item;
}
แล้วภาษาอื่น ๆ ล่ะ? ฉันเดาว่าส่วนใหญ่ของพวกเขาในItem
ชั้นเรียนจะต้องให้มันเรียกว่าICollectionItem
อินเตอร์เฟซ
ปรับปรุง-1
ฉันกำลังคิดเกี่ยวกับมันเพิ่มขึ้นอีกเล็กน้อยและรูปแบบนี้จะมีประโยชน์จริง ๆ เช่นถ้าคุณไม่ต้องการให้เพิ่มรายการลงในคอลเลกชันหลายรายการ
ICollectable
อินเตอร์เฟซการทดสอบ:
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);
}
และการใช้งานตัวอย่าง:
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
}
การใช้งาน:
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)
แต่จะไม่ใช่สไตล์ OO ที่ดี
item.AddTo(items)
สมมติว่าคุณมีภาษาที่ไม่มีวิธีการขยาย: เป็นธรรมชาติหรือไม่, เพื่อรองรับ addTo ทุกประเภทจะต้องใช้วิธีนี้และให้มันสำหรับคอลเลกชันทุกประเภทที่สนับสนุนการต่อท้าย นั่นเป็นตัวอย่างที่ดีที่สุดในการแนะนำการพึ่งพาระหว่างทุกสิ่งที่ฉันเคยได้ยิน: P - ฉันคิดว่าสถานที่ที่ผิดพลาดที่นี่กำลังพยายามสร้างแบบจำลองการเขียนโปรแกรมบางอย่างที่เป็นนามธรรมเพื่อชีวิต 'จริง' มันมักจะผิดพลาด