Home >
I was recently asked what was the point of using base classes for creating custom components, instead of just putting everything in a single class instance? Well, there are several reasons that this is beneficial. Here are just a few.
Code Reuse
One of the main reasons to use inheritance (extending a base class) is for reusing code in various child classes. If you have a subset of classes, and all of them would share a particular code segment, then there is no need to duplicate that segment of code for every class instance. This would lead to multiple versions of the exact same code which would have to be maintained for different component instances. Creating a base class that encapsulates common logic saves you time.
Code Simplicity
Abstracting complex logic in base classes can allow you to keep your higher-level components clean and easy to understand. Rather than having a single component with multitudes of code to wrap your head around, you can have multiple classes with clearly defined code segments. This is purely for code readability and maintainability.
Code Flexibility
Equally as important as reuse and simplicity is flexibility of your code. Let's say you have two components that are very similar, however they have two very distinct behaviors. A base class could encapsulate all of the common logic, and in the child classes override logic in the base class for their own custom purposes. Or, let's say you want to change the behavior of an existing component; You can simply override functions to make the existing component behave in a new manner.
A simple example of this could be overriding a container to have a circular layout instead of a linear layout, or it could be overriding a mouse handler to perform a different function than the base component.
Maintenance
Code that is simple, flexible, and reusable is typically easier to maintain... isn't that what we all want?
These are just a few reasons, but they should definitely be kept in mind when building your own applications.
___________________________________
Andrew Trice
Principal Architect
Cynergy Systems
http://www.cynergysystems.com




Facebook Application Development
Need to be careful with posts like this. It is reads as if you're saying that inheritance is the be all and end all. It would be good if you would clarify the scenarios in which you might use this approach, rather than composition for example.
For instance, you should not use inheritance to build a complex UI - you should compose the UI of components, which in turn may be compositions of other components. However, the lower level/more granular you get, it will become apparent that the shift from composition moves to inheritance.
All systems should be a careful balance of inheritance and composition, and this applies equally in the RIA space.
I Think that composition is overall more suitable then inheritance.
Code Reuse
Using inheritance, all of your child classes get ALL base code, so you always reuse all of the code, not something I'd call "reusable" in different contexts.
Code Simplicity
I agree, inheritance is definitively simple, but in some cases composition can be simpler.
Code Flexibility
You can't consider inheritance as "flexible" knowing that every change in your base class can break anything down the road. You are talking about encapsulation but basically, inheritance usually breaks encapsulation :)
Maintenance
In the last part, you assume that the inherited code is "simple, flexible, and reusable" thus maintainable, sorry, it is maybe simple, but definitely not flexible or reusable except in very specific cases as Chris said.
Thanks Andrew Trice!
It is usefull for may my job.
I wish you have more..