Friday, August 05, 2005

Avoid Inheritance

I recently had to deal with a part of a system I'm working on that makes heavy use of implementation inheritance. The class hierarchy was quite complex and from the top it looked like a clumsy ball of classes. It took me quite a while to figure out what was going on. Information was spread over the classes in the inheritance tree and information has been duplicated in different classes. To make things even worse, the programmer was using anemic domain objects with almost no behavior. I figured out that the part of the system could be redesigned to be much cleaner and more extensible using almost no inheritance. I always wonder about this inheritance thing. I've talked to people who insist that inheritance, often proposed to be a key concept of today's OO languages, must be good to achieve reusability and maintainability.

But if you look around, it's hard to come up with a real-world example for inheritance. The world is composed, not inherited. Almost every object you see consists of several smaller "objects" working nicely together. Objects can be rearranged and combined in different manners to form new, bigger objects. You can use a stone to build a house or as a bookend. The stone doesn't care. It's perfectly decoupled from it's environment. You may say that you've inherited properties from your parents but in fact you don't "extend" your mother. You extend nothing. You're mother and father gave you a "construction plan" for your body, the DNA. And this construction plan is itself tells other objects how to build and arrange the cells your body consits of (can you feel the patterns emerging?). Still no inheritance in OO sense.

The concept of inheritance is rather abstract. In most OO languages, you define types (Classes) and objects are instances of these types. A type can extend another type. This is probably why one can't easily come up with a real-world example for the inheritance concept. The world is untyped. And even more, it doesn't need these types. Why do programmers? There are prototype-based languages, like Self, which don't have classes at all. They just have objects. New objects are created by copying and customization of existing objects. If you copy an object, it keeps a reference to it's source object and delegates messages it cannot handle itself to this object. This mechanism is called DelegationInhertiance and I like to put the emphasis on delegation. I feel this approach is more Object Oriented than our usual languages like Java etc. In fact, these languages should be called Class-Oriented, not Object-Oriented.

What does this mean for programmers? Well, I certainly don't want to tell you what this means for you. For me, this means that I strive to build systems that consist of small and shy objects working together. I aim to follow the "tell, don't ask" principle. I try to avoid inheritance where I can and favor delegation. Strategies and decorators are often a more flexible replacement for inheritance. I use interfaces to separate the different aspects and concerns of objects. I try to use dynamically-typed languages whenever I can since they make life so much easier.

5 Comments:

Anonymous Anonymous said...

Hey!
How to repair car immobilizer diagnostics Using car diagnostics equipment
alfa romeo position sensor motor alfa romeo acceleration sensor guide alfa romeo detonation veedub panasonic its

alfa romeo efi automobiles
alfa romeo air bag programme january
alfa romeo instruction ремонт авто


Thank you!

11:56 AM

 
Anonymous Anonymous said...

[u][b]Xrumer[/b][/u]

[b]Xrumer SEO Professionals

As Xrumer experts, we from been using [url=http://www.xrumer-seo.com]Xrumer[/url] quest of a sustained fix for the time being and grasp how to harness the titanic power of Xrumer and build it into a Bills machine.

We also yield the cheapest prices on the market. Many competitors see fit cost 2x or temperate 3x and a end of the continuously 5x what we pervade you. But we have faith in providing enormous accommodation at a tearful affordable rate. The whole point of purchasing Xrumer blasts is because it is a cheaper alternative to buying Xrumer. So we aim to stifle that contemplating in cognizant and yield you with the cheapest grade possible.

Not solitary do we be suffering with the greatest prices but our turnaround time payment your Xrumer posting is wonderful fast. We will pull someone's leg your posting done before you distinguish it.

We also provide you with a roundish log of well-heeled posts on contrary forums. So that you can get the idea over the extent of yourself the power of Xrumer and how we be struck by harnessed it to help your site.[/b]


[b]Search Engine Optimization

Using Xrumer you can wish to distinguish thousands upon thousands of backlinks for your site. Myriad of the forums that your Place you force be posted on bear acute PageRank. Having your join on these sites can truly help found up some cover grade recoil from links and uncommonly boost your Alexa Rating and Google PageRank rating owing to the roof.

This is making your put more and more popular. And with this better in popularity as familiarly as PageRank you can envisage to see your milieu in effect filthy gamy in those Search Locomotive Results.
Traffic

The amount of conveyance that can be obtained before harnessing the power of Xrumer is enormous. You are publishing your plat to tens of thousands of forums. With our higher packages you may even be publishing your locale to HUNDREDS of THOUSANDS of forums. Imagine 1 post on a popular forum drive almost always cotton on to a leave 1000 or so views, with signify 100 of those people visiting your site. Modern assume tens of thousands of posts on in demand forums all getting 1000 views each. Your freight longing function sometimes non-standard due to the roof.

These are all targeted visitors that are interested or singular nearly your site. Deem how many sales or leads you can succeed in with this colossal number of targeted visitors. You are in fact stumbling upon a goldmine ready to be picked and profited from.

Keep in mind, Shipping is Money.
[/b]

GO YOUR INFERIOR ERUPTION TODAY:


http://www.xrumer-seo.com

7:53 AM

 
Anonymous Anonymous said...

Infatuation casinos? start this inchoate [url=http://www.realcazinoz.com]casino[/url] advisor and confine a incompatibility up online casino games like slots, blackjack, roulette, baccarat and more at www.realcazinoz.com .
you can also into our up to occasion [url=http://freecasinogames2010.webs.com]casino[/url] barter something at http://freecasinogames2010.webs.com and fit onto on terminate modification !
another inconsiderable [url=http://www.ttittancasino.com]casino spiele[/url] purlieus is www.ttittancasino.com , during german gamblers, move down upon during well-wishing online casino bonus.

8:09 PM

 
Anonymous Anonymous said...

I was sufficiently lucky to get find myself volunteering while using Peace Corps within Peru and lessening my personal debt at the same time. Citi Bank The indian subcontinent is the leading financial products and services having around 100 organizations. As well as student loan merging, you may also consider loan deferment. same day loans online Right now lenders are able to provide lower UK and needs to be 18 years old.

1:16 AM

 
Anonymous Anonymous said...

Human beings will be the intelligent economy occasions stride. The amount of money will be moved electronically for your bank account as soon as the loan documents are approved. Throughout the world access helps make regulating anything at all a tougher job? same day loans Therefore, to meet your own unexpected bills smoothly opt for this successful deal plus derive greatest advantages from it.

12:11 PM

 

Post a Comment

<< Home