Question

I have a design question. When writing an application where there are several components that share some attributes, and differ in others both in terms of GUI (windows forms) and back end, how would one theoretically approach this?

For example, I have an application where I have 4 different types of a product. The forms for entering product details all share 3 different fields. So far, easy - have a base class, then derive the 4 forms out of this base class. However, say, 2 products share a certain behaviour which is THE SAME for both of them. Of course, I could have an interface which defines methods, then have these 2 product entry forms implement this interface, but since interface provides no default implementation, wouldn't it be a waste to do that? Since I would have to provide an implementation in each class and if it is exactly the same, it would be code repetition.

I could of course put this method in the base class and have 2 forms derived from it. If my forms share more than one common element and not share other elements, what would be the sanest, most logical way to represent this without having to rip my hair out?

No correct solution

OTHER TIPS

Why not have the 2 unrelated classes derive directly from your base class and then create a new class with the common functionality and is derived from the base class and have the remaining two classes inherit from that new class?

BaseClass
CommonClass : BaseClass
ClassA : BaseClass
ClassB : BaseClass
ClassC : CommonClass
ClassD : CommonClass

One way to get common behaviour into two classes is for the two classes to contain that behaviour as a member, instead of inheriting that behaviour: for forms, this might mean defining a 'user control' (i.e. a reusable collection of common controls).

Hard to tell without any specific layout. generally, the other , often more flexible form of reuse is doing composition instead of inheritance. In fact the control hierarchy shows just how strong composition can go. Maybe you can separate out parts of your UI into custom controls that are assembled based on the specific product layout you are after

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow