0
votes

I am trying to make this example as natural as possible to make it simple.

Lets say I have groups of items. eg item1, item2, item3, item4 etc.

Currently item is represented by struct:

// only contains data
struct item{
  string name;
  std::vector<string> parts_name;
  std::vector<double> parts_price;
  std::vector<string> item_color;
  ...
};

itemx item1, item2...

Then I have class called items

class items{
  public:
  // return name of all items
  vector<string> get_all_item();

  // return price of all parts 
  vector<double> get_all_parts_price();  

 private:
  // hold vector of item
  vector<shared_ptr<item>> list_of_items;

  // name of item and its parts price
  // eg: <item_1_part_1, $1.3>
  // eg: <item_1_part_2, $2.3>
  // eg: <item_2_part_1, $4.3>
  map<string, double>  parts_prices;
};

The problem with my current items class is that it is growing really huge, as feature of item increases.

My solution: make struct item contain function which distributes load to struct item from class items.

Is there any other better way or any design pattern that is meant for this kind of problem?

1
Probably just use a std::vector<item> - Justin
You should probably not return copies of vector... Simply store everything in std::vector<item> as mentioned by @Justin, get_all_items() returns a (const) std::vector<item>& and for the various part you can just create custom iterators that iterate over items and "sub"-vectors, this way you never have to create vector when you simply need to read something... - Holt
what does get_all_parts_price() want to return? The sum of all prices of all parts, or a list of prices and parts? - Richard Hodges
@RichardHodges list of prices of all the parts present in all the items .. basically all the value of parts_prices. - vanta mula
Why is color a vector? Why do items have both parts and their price? Why does items return a vector of prices, when each item has a vector of prices? What is item, and what does it represent in comparison to parts? You are doing OO design and asking for help, without describing what the objects are. Why are parts not items, what distinguishes them, and why do parts exist? - Yakk - Adam Nevraumont

1 Answers

1
votes

It is hard to tell you how to "design" your application based on your input - and I think you are focusing on the wrong aspect here. Your item struct contains vectors. That implies plural. But still, the class is called item.

In that sense: don't think about patterns here. Instead, step back and have a much closer look at the "real world things" you intend to model. You see, the core point of objects and classes is to built a model of the domain you are dealing with.

So a non-answer here: carefully look into the "real-world" relation that your objects have, and then focus on creating a OO model that is helpful to do work flows you intend to implement.