SuperSet to Subset Classes without breaking legacy code
Seeing i am really new to OOP, This could have been avoided have i known better. I started about a year with my Class Library and made a Class Lets call it Tree and up until now the Tree class has been working fine , it does exactly what i need.
Well, now i've noticed that i have different types of Tree's
I would like to move some functions of tree to its own Namespace and create new ones for these other trees , How do this without breaking systems that already implements the cu开发者_开发问答rrent Tree?
Is there some way i can "Forward" calls from the existing to the new
Depending on the new types of Trees, you might have a SuperTree that contains a Tree instance to perform some of its functions, but provides additional functionality that is unique to the SuperTree. You do the call 'forwarding' by writing wrapper methods for the functions that you want directly handled by the internal Tree instance.
In some cases, it might also make sense for SuperTree to descend from Tree, but only if Tree was designed with this type of extensibility in mind and you might want to pass a SuperTree in place of a Tree.
Think of it like this:
A building may contain a steel frame and relies upon it, but it is not a type of steel frame.
A hotel is a kind of building and therefore has the things that buildings have, like doors and windows.
If your Tree is more like a component of a larger system (the framing of a building), then you expand on its functionality by composing it as a private member of a new class. If it's a rich entity of its own, with well-defined extensibility points, you can descend new classes from Tree that simply expand on its existing behavior.
精彩评论