In the future I may run into a problem, and today I will be well prepared for this. The problem is with inheritance, polymorphism and composition in the context of C ++. How can we reorganize the โreuse of inheritance codeโ into a composition and still be able to maintain a polymorphic approach?
What I'm looking for here is a "guide" on this. I came with a very simplified example to show you, and I believe that you can read it and refine it in the answer that I need.
class Multilingual_entity {
public:
enum class t_languages {LAN_ENGLISH, LAN_RUSSIAN, LAN_CHINESE};
private:
std::map<t_languages, std::string> texts;
public:
std::string set_text(t_language t, const std::string s) {texts[t]=s;}
void get_text(t_language t) const {return texts.at(t);}
}
It will be expanded later ...
class Category_shopping_article:public Multilingual_entity {
private:
unsigned int pk_identifier;
public:
unsigned int get_pk() const {return pk_identifier;}
}
class Shopping_article:public Multilingual_entity {
private:
unsigned int category_identifier;
float price;
public:
}
And it is applied like this:
void fetch_all_titles_for(Multilingual_entity& m);
Category_shopping_article get_category(unsigned int pk) {
Category_shopping_article result=get_me_category_by_pk(pk);
fetch_all_titles_for(result);
return result;
}
std::vector<Shopping_article> get_articles_by_category(const Category_shopping_article& cat) {
std::vector<Shopping_article> result=get_me_articles_by_category_id(cat.get_pk());
for(Shopping_article& a : result) fetch_all_titles_for(a);
return result;
}
, : ( , ) , -. , , "fetch_all_titles_for" :
void fetch_all_titles_for(Multilingual_entity& m) {
Database_table T=m.get_database_language_table();
Database_criteria C=m.get_database_language_criterie();
std::map<Multilingual_entity::t_languages, const::std::string> texts=Database_object::get_me_the_info_i_need(T, C);
for(const std::pair<Multilingual_entity::t_languages, const::std::string>& p : texts) m.set_texts(p.first, p.second);
}
, , jumpstart, , . , Multilingual_entity... , "text_review", , :
class Category_shopping_article: {
private:
unsigned int pk_identifier;
Multilingual_entity titles;
public:
unsigned int get_pk() const {return pk_identifier;}
std::string set_title(t_language t, const std::string s) {titles.set_text(t, s);}
void get_title(t_language t) const {return titles.get_text(t);}
}
class Shopping_article: {
private:
unsigned int category_identifier;
float price;
Multilingual_entity titles;
Multilingual_entity descriptions;
public:
std::string set_title(t_language t, const std::string s) {titles.set_text(t, s);}
void get_title(t_language t) const {return titles.get_text(t);}
std::string set_description(t_language t, const std::string s) {descriptions.set_text(t, s);}
void get_description(t_language t) const {return descriptions.get_text(t);}
}
, ... (, ), - "fetch_all_titles_for (Multilingual_entity & m)", . " ", , , .
... - ? , ?. - :
class Multilingual_consumer {
private:
std::vector<Multilingual_entity> entities;
public:
Multilingual_entity& add_entity() {
entities.push_back(Multilingual_entity);
return entities.back();
}
Multilingual_entity& get_entity(unsigned int i) {return entities.at(i);}
};
class Category_shopping_article:public Multilingual_consumer {
private:
unsigned int pk_identifier;
enum entities{TITLE, DESCRIPTION};
public:
unsigned int get_pk() const {return pk_identifier;}
Category_shopping_article() {
add_entity();
add_entity();
}
void get_title(Multilingual_entity::t_language t) const {return get_entity(TITLE).get_text(t);}
void get_description(Multilingual_entity::t_language t) const {return get_entity(DESCRIPCION).get_text(t);}
}
. , ?
.