views:

32

answers:

3

Hello,

I have an already existing order class with many properties and methods. I need to now create a suborder class which is a subset of the order class. It not only has many of the fields that order class has, it also would have many of the same calculation methods maybe with one or two lines of difference in some places. The order class then needs to contain a list of suborder objects. How would I go about creating the suborder class? Should I derive it from the order class?

A: 

If a suborder does not share all of the properties of order, I would not derive Suborder from Order.

Instead, make a common base class that has the shared behavior and properties and derive both Order and Suborder from the base class.

Nate C-K
A: 

It seems to be a case for refactoring...
Although the Order class readily exists, it appears that some of its properties and methods need to be moved to the OrderSubClass.
If somehow some of the properties and behaviors of the two class are common, creating a common base class (or an interface) may be appropriate as well.

Once the above is arranged, the Order class can become a container for SubOrders, which seems to be the direction indicated in the question.

mjv
A: 

The Composite Pattern could give you a tip on a desired design.

But I would have hierarchy like this:

OrderBase
  |--> MainOrder
  |--> Suborder

or if MainOrder and Suborder are different enough, then interface would be better to use:

MainOrder <--- IOrder
Suborder  <--- IOrder

But this will not share the implementation.

Dmytrii Nagirniak