c++ - Is there a pattern to track currently selected item in a container? Does state fits here? -


my original project complex wrote small demo illustrates problem , question.

my model contains list of objects. when call method increment(), applied current selected item. question how should track selected item?

my code below uses pointer currentitem track current selected item. real project has do/undo functionality via command pattern needs track. additionally have more states there keep track of including current selected item , previous selected item.

besides code below, have tried state design pattern have implement alot of functions in strategy applies object , seems complicate things. there specific pattern this? strategy suits here or overkill or perhaps simple plain pointer approach fine? please keep in mind application needs support do/undo functionality.

#include <qcoreapplication> #include <iostream> #include <qlist>   #define item_count  5  using namespace std;  class objecta { public:     objecta(string n, int p) {         name = n;         price = p;     }      string name;     int price;      void increment() {         price++;     } };  class containerobject { public:     qlist<objecta*> items;      containerobject() {         (int = 0; < item_count; i++)         {             string name = ("name " + std::to_string((_ulonglong)i));             objecta * item = new objecta(name, 0);             items.append( item );         }          // assume @ index 3 current item         setcurrentitem( items.at(2) );     }      objecta* currentitem;      void setcurrentitem(objecta * obj) {         currentitem = obj;     }      void increment(){         currentitem->increment();          if( currentitem->price >= 2)             setcurrentitem( items.at( 4) );     }      void print()     {         (int = 0; < item_count; i++)         {             objecta * item = items.at( );             cout << item->name << "  " << item->price << " added" << endl;         }          cout << endl;     }  };   int main(int argc, char *argv[]) {     qcoreapplication a(argc, argv);      containerobject container;      // initial values     container.print();      container.increment();     container.print();      container.increment();     container.print();      container.increment();     container.print();      return a.exec(); } 

first seems have multiple opened questions :

1) how should track selected item?

there no answer depends on container's type. here seems have list, pointer approach better fits compared index approach since number of elements can vary (you insert/append new items @ different locations)

2) type of pattern achieve this?

i haven't understood why tried implement state design pattern, assume because read assumption "it allows adapt behavior of object when internal state changes". think changing selected item not representative "internal state change" won't fit. it's used in state machine implementation.

for specific purpose of undo/do there anoter pattern called memento design pattern, can recalls important such previous selected item or , allows restore situation. guess simplify design when receiving command :

  • recall situation via memento (selected items...or more)
  • process command (update selected items...)

if needed on undo command :

  • restore situation

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -