samedi 27 août 2016

design pattern, a vector of objects, each of two possible types

I need to store objects of exactly 2 types in a vector, the both types have almost nothing in common ...

After storing them in the vector, i want to iterate over that vector and perform an action, depending on the type

my thoughts so far ...
(1) polymorphism :
overkill, and wouldnt help me much, as i probably would do a

if(dynamic_cast<T1>() != nullptr) {
    ...
} else {
   ...
}

(2) merge both types (methods and fields) and add a boolean, representing if its type 1 or 2

both patterns seem totally clumsy to me, there is propably a total simple solution, i simply wont see ...

The first type is something like this :

struct PatternMatch {
  int length;
  int indexInDict;
}

the second

struct NoMatch {
  std::string rawChars;
}

Aucun commentaire:

Enregistrer un commentaire