We have ten groups: A, B, ..., I, J. In each group we have a variable number of contacts. In group A we have 3 contacts: A1, A2, A3. In group B we have 5 contacts: B0, B1, B2, B3, B4. ... bla, bla, bla ... In group J we have 3 contacts: J4, J5, J6. Contacts can be added to or removed from groups. I have an application named ContactBook that receives a STATUS message from the server when a group closes down or opens up. The application has two tabs: "Online Contacts" and "All contacts".
I have two implementations:
I) The first implementation calls a function every time a STATUS message is received. bool groupIsOnline[10]; list<String> group[10]; list<String> online; list<String> all; void on_STATUS_received() { for(int i=0;i<10;i++) { all.append(group[i]) if (groupIsOnline[i]) online.append(group[i]); } }
II)The second implementation parses the addressbook every time I display the contacts. list<Contact> addressbook; list<String> getAllContacts() { list<String> all; for(int i=0;i<addressbook;i++) { all.append(addressbook[i].name) } return all; } list<String> getOnlineContacts() { list<String> online; for(int i=0;i<addressbook;i++) { if (addressbook[i].group.isOnline) online.append(addressbook[i].name); } return online; }
The first implementation has one 'groupIsOnline' check per group. The second implementation has one 'groupIsOnline' check per contact. The first implementation calls function on_STATUS_received() every time we receive a STATUS message from the server. The second implementation calls function getOnlineContacts() every time I select the "Online Contacts" tab in my application.
If I get STATUS messages very often but enter once in a while in the "Online Contacts" tab, the second implementation is better. If I flip tabs often and receive very few STATUS messages, the first implementation is better.
Anyone knows some good books that discuss this kind of OOP design decisions?
PS: Somehow related: Design, OOP about a game
PPS: I am not an English native speaker.
Aucun commentaire:
Enregistrer un commentaire