oop - In C++, should iterable types be non-polymorphic? -


a bit of background:

i working on assignment oop course consists in designing , implementing phone book manager around various design patterns.

in project there 3 classes around action happens:

  • phonebook;
  • contact (the types stored in phone book);
  • contactfield (fields stored in contact).

contactmanager must provide way iterate on contacts in 2 modes: unfiltered , filtered based on predicate; contact must provide way iterate on fields.

how decided implement:

all design patterns books came across recommend coding interface first thought extract interface each of above classes , make them implement it.

now have create kind of polymorphic iterator things smooth adapted java iterator interface write forward iterators.

the problems:

  • the major setback design lose interoperability stl <algorithm> , syntactic sugar offered range based loops.

  • another issue came across iterator<t>::remove() function. if want iterator can alter sequence iterates on (remove elements) fine if don't want behavior i'm not sure do.

    i see in java 1 can throw unsupportedoperationexception isn't bad since (correct me if i'm wrong) if exception isn't handled application terminated , stack trace shown. in c++ don't have luxury (unless run debugger attached think) , honest i'd rather prefer catch such errors @ compile time.


the easiest way out (that see) of mess avoid using interfaces on iterable types in order accommodate own stl compatible iterators. increase coupling i'm not sure have impact in long run (not in sense project become throw away code of course). guess won't however, i'd hear elders opinion before proceed design.

i take different approach.

firstly, iteration on contact pretty simple since it's single type of iteration , can provide begin , end methods allow iteration on underlying fields.

for iteration on phonebook still provide normal begin , end, , provide for_each_if function use iterate on contacts interesting, instead of trying provide super-custom iterator skips on un-interesting elements.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -