Home  >  

Why use base classes?

Author photo
| | Comments (2)
AddThis Social Bookmark Button

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

Read more from Andrew Trice. Andrew Trice's Atom feed

Comments

2 Comments

Chris Brind said:

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.

Adnan Doric said:

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.

Leave a comment


Type the characters you see in the picture above.

Tag Cloud

Poll: Sci-Fi Movies

What's Your Favorite Sci-Fi Movie of All Time?

Vote | View Poll Results | Read Related Blog Entry

Latest Features

  •     Welcome back to the series. This time we are goings to build a really exciting component that will be used to simply display information about the user. Well, you might say why to we need such a component, is there... Continue Reading
  •    Welcome back to our exciting Facebook ActionScript series. In this article we will discuss one of most important (and most exciting) features of the FB platform, it's the publishing of news. We all know when we log in to facebook,... Continue Reading
  • This article provides 10 tips and best practices (in no particular order) for maximizing the benefits that Dojo can bring to your next project. For a more thorough introduction to Dojo, see the article Dojo: The JavaScript Toolkit with... Continue Reading
  •     The notifications are one of the most interesting (and important) parts of the facebook area. In order to completely understand the Flash side of it, we need to understand the basics of the facebook notification, what it is and how... Continue Reading

Development Series

Get an overview of the tools and technologies that work together to allow developers to build Rich Internet Applications (RIAs) quickly and easily.

facebook icon Facebook Application Development

Anatomy of an Enterprise Flex RIA

Recommended for You

@InsideRIA on Twitter

Archives

  • Or, visit our complete archive.  

About This Site

Welcome to the premiere community site for all things RIA sponsored by O'Reilly Media and Adobe Systems Incorporated.