Design API for class that contains collection of another class

by Aadarsh Sharma   Last Updated November 15, 2017 06:05 AM

This is follow up of my last question. I have tried to make this example simple. I have class Hold which contains a vector of int and class CollectHold which containts the vector of collection of class Hold I have two methods in class CollectHold, void Insert(size_t index, value) and std::vector<int> GetVec(). Every time Insert method is called it has to determine which of the collection of vector of class Hold it has to insert. This for me looks like really bad design. Also for GetVec I have to append all the vector on the fly every time this function is called and looks really memory intensive.

Do you think the API design here is appropriate? By Appropriate I mean the naming convention, optimization for GetVec memory wise and fast indexing for Insert method of class CollectHold

I was also thinking of of costume iterator (iterator design pattern) inside class CollectHold.

#include <vector>

class Hold{
  hold(size_t n):vec(n,0){}

  void Insert(size_t index, value){ = value;

  std::vector<int> GetVec(){
    return vec;

  std::vector<int> vec;

// lets assume during initialization they are initialized by zero
//holder[0] = [0][0][0][0]
//holder[1] = [0][0][0]
//holder[2] = [0][0][0][0][0]

class CollectHold{


   void AddHold(size_t size) {
      Hold tmp(size);

   void Insert(size_t index, value){
     // determine while of the holder's vector this index 
     // belongs to then 

      // holder[1].at(1) = value

   std::vector<int> GetVec(){
    // append holder[0] holder[1] holder[2] on the fly and return  

   std::vector<Hold> holder;

int main(){

   CollectHold collect_holder;


   collect_holder.insert(6, 7);

   std::vector<int> get_all_val = collect_holder.GetVec();

Related Questions

Is it a good idea to experiment with design patterns?

Updated October 16, 2017 19:05 PM