简体   繁体   中英

DESIGN — Remove by name or object?

Does it make more sense to remove an object using its name/id or passing the actual object? For example:

void MyList::remove(MyObject &myObject) { /* blah */ }
// or
void MyList::remove(std::string id) { /* blah */ }

I've used both but I can't really see what the advantages vs disadvantages. Is there a preferred standard?

EDIT: this would probably be a better example providing what I'm trying to do:

Let's say I have an Account class with a collection of Transactions. Am I better to pass Transaction object or the id of the Tranaction?

class Account
{
  private List<Transaction> transaction = new List<Transaction>();

  public void Remove(Transaction transaction) { }
  // OR
  public void Remove(string name) { }
  // OR
  public void Remove(Guid id) { }
}

NOTE: this question has both C++ and C# code...

You may not have the item reference always. So it is better to have remove methods by name or id. You can decide which one is preferable (name or id) according to your business requirement. name or id has to be unique otherwise it will remove the wrong item. So the business requirement has to decide it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM