I want to write a Bounded Priority Queue class. This is essentially a priority queue but has a bound on the number of elements that can be there in the queue. So, if I insert a new element and the queue is full, then I see if the element is greater than the top of the queue. If yes, then I discard the new element. If no, then I remove the top element and insert this new element (and the queue gets automatically reorganized). I have currently implemented BoundedPriorityQueue as derived from PriorityQueue having a "maxCount" member and I have overridden the add method and added the functionality I described above. I am wondering if I should consider doing it the composition way rather than the inheritance way. My current approach seems simple though.
If I were to go for composition, then I have to derive from the AbstractQueue class, implement the methods and then compose the priority queue with it.
Thanks,
Venk
in my opinion it is better to use composition. because, inheritance this way is the violation of Liskov substitution principle. derived types shouldn't replace base types' functionality. this link will be useful: prefer composition over inheritance
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With