0

i have to implement data structures that support abstract Numbers

Collection is a collection of numbers

sorted is a sorted collection

list is a list collection of numbers (include duplicates)

set is a collection of numbers without dup.

Linked and Array are the format of the saved data (linked meaning diffuse in the heap and array meaning a contious amount of memory)

then there are combinations of classes (a list that is array called arraylist and etc. in the picture)

a simple uml:

uml3;

each of the last four combinatios (last row in the pic) need to implement a method call sorted that make a sorted copy of their own (return Sorted type). i thought to make 4 new classes that are inheritance from sorted and the classes in the last row (for ex. an ArrayListSorted class that inheritance from Sorted class and ArrayList class) but then i have the diamond problem (in the same ex. Sorted and ArrayList are both Collection offspring)

any better idea for disign?

aviadch
  • 321
  • 1
  • 5
  • 17
  • 1
    There's a common view that you should [prefer composition over inheritance](http://stackoverflow.com/questions/49002/prefer-composition-over-inheritance). Depending on your implementation language, though, a diamond inheritance pattern isn't necessarily a problem. Unusual, and I wonder if maybe someone has been defining base classes because they can, but if you can have multiple inheritance you should be able to deal with diamond inheritance issues. –  May 17 '13 at 12:44
  • why not just add a parameter Sorted* sorted in the Collection class, which get updated every time sorted() is called ? – lucasg May 17 '13 at 13:24
  • georges, the thing is that i dont write the main function. i was told that in the main they expect that sorted will return a Sorted object – aviadch May 17 '13 at 14:47

1 Answers1

2

In C++ the diamond problem can be solved by using virtual public inheritance. With virtual public inheritance the base class is inherited only once:

class A {...};
class B : virtual public A {...};
class C : virtual public A {...};
class D : public B, public C {...};
Claudio
  • 10,614
  • 4
  • 31
  • 71