Saturday, June 18, 2005

Pr#2 Program to an Interface, not to an Implementation

We are back at the Linxemble Studios, discussing "who should fight using what"! You see two abstractions {Warrior, Weapon}. That's great! Having figured out abstractions is the key towards a successful design (Let's not discuss what's "successful" right here).


Let's look at this design:


As a usual design process, we have declared two interfaces- Warrior and Weapon. The idea is, each Warrior uses it's special Weapon. For instance, a Cavalier uses a Sword and an Archer uses a Bow. Both the Cavalier and the Archer implement the Warrior interface, while the Sword and the Bow implement the Weapon interface. To elaborate, both Cavalier and Archer implement their own set of methods (attack, getWeapon..etc.). Similarly, Both Sword and Bow implement their own "trigger" methods. Say, for the Sword the trigger means "one swipe of the word" and for the Bow it means "shooting the arrow".

Looks easy! We start implementing the specific classes like this:

Let the Cavalier contain an instance of Sword and the Archer an instance of Bow.
For now, let's just look at the Cavalier class. It contains the Sword.



So, things are implemented to some extent (in fact to quite a bit extent!), when another designer comes up with this fantastic idea of training! Here's what he has to say:

"A Cavalier normally fights with a Sword. But he can be trained to be a rifle-horseman! Let's not touch the exesting Cavalier class, as it's instances still will be in use. Let's have another class TrainedCavalier, that extends Cavalier. We need to make it contain Rifle instead of a Sword. This looks quite easy as the Rifle also is an implementer of the class Weapon and as this is an extension, we will have to override only a few methods (like attack, setWeapon) of the Cavalier, reusing the rest of the code! Isn't it?"
So, the obvious solution that comes to mind is extend the Cavalier (as it encapsulates the rest of the features, we just need to change the Weapon!).

But Arghhhhh! The TrainedCavalier class uses an instance of Sword. This is inflexible!

To be precise, the Cavalier instantiates its Weapon as:

Sword swd ;
...
public .... setWeapon()
{
...
swd = new Sword();
...
}
Now the TrainedCavalier can NOT do this, while overriding the setWeapon method:

public .... setWeapon()
{
...
/* This is not possible, as "swd" is of type Sword,
* declared in the base class - Cavalier.
*/
swd = new Rifle();
...
}
You can go ahead and suggest you solutions to Linxemble Studios. But let's think of what could have been a better start. Had the designer of the Cavalier class used this instead:

Weapon wp;

...
public .... setWeapon()
{
...
wp = new Sword();
...
}
the TrainedCavalier could override it as:

public .... setWeapon()
{
...
/*
* This is valid, as "wp" is of type Weapon. Remember IS-A rule?
*/
wp = new Rifle();
...
}

So, the principle under the consideration "Program to an Interface, not to an Implementation"'s violated by the designer of Cavalier class, as he used the specific implementer (i.e, the Sword) of the Weapon abstraction. Now if one wants to refactor the Cavalier class (replacing the references to Sword with Weapon), he has to do so many changes in all the methods which call the methods on the Sword contained by the Cavalier. Current design of the Cavalier class done not make use of the Weapon abstraction well! Why, after all, did we force all the {Bow, Rifle, Sword} implement the interface Weapon?

Well, there is another bad thing going on, which requires us to peek into the actual code to understand it. Even after refactoring the Cavalier class, we'll see some repetition in the code across the Cavalier and Archer classes!

Well, for now, I'd say "You may as well use an Abstract Class!".

---- Discussion to be continued...

13 Comments:

Blogger Sujeet Banerjee said...

Well, there's an error in the first UML diagram shown - the Warrior should be an "Interface", instead of the "Class" shown.

Sunday, 17 July, 2005  
Blogger rksistu said...

HI ,
Increase your revenue 100% of your blog by converting into free website.
Convert your blog "yourname.blogspot.com" to www.yourname.com completelyfree.
Become proud owner of the (.com) own site .
we provide you free website+ free web hosting + list of your choice of
scripts like(blog scripts,CMS scripts, forums scripts and many scripts)
all the above services are absolutely free.
You can also start earning money from your blog by referring your friends.
Please visit www.hyperwebenable.com for more info.
regards
www.hyperwebenable.com

Thursday, 19 June, 2008  
Anonymous Anonymous said...

This comment has been removed by a blog administrator.

Saturday, 23 January, 2010  
Anonymous Anonymous said...

This comment has been removed by a blog administrator.

Friday, 19 February, 2010  
Anonymous Anonymous said...

You have to express more your opinion to attract more readers, because just a video or plain text without any personal approach is not that valuable. But it is just form my point of view

Saturday, 20 February, 2010  
Anonymous Anonymous said...

I didn't understand the concluding part of your article, could you please explain it more?

Saturday, 13 March, 2010  
Anonymous mコミュ said...

mコミュで簡単にハメ友を見つけませんか!!登録している娘は相手は誰でも良いんです、条件はSEXが好きの一点だけです。さあ連絡を取って手っ取り早くハメ友をGETしよう

Monday, 25 October, 2010  
Anonymous 神待ち said...

訳アリで家出した少女が神様の救いを待つ神待ち掲示板です!家に帰りたくない、帰れない少女達に愛の手を差し伸べてあげませんか

Wednesday, 27 October, 2010  
Anonymous グリー said...

やっぱり新しい出逢いはグリーやモバゲーよりスタービーチ!フィーリングの合う異性を探し、すぐに遊びにいこう

Friday, 29 October, 2010  
Anonymous SM度チェッカー said...

SMって最初は抵抗有る方が多いと思いますが、新しい自分を見付ける入門にまず、SM度チェッカーで自分に合うSMプレーを探しませんか?ここから新しい可能性が広がりますよ

Sunday, 14 November, 2010  
Anonymous M男 said...

M男の理想・願望を叶えます!!M男だから求める娯楽を当掲示板で楽しんでいきませんか?Sな女性から沢山の喜びを与えてもらえる快楽コミュで楽しんで下さい

Monday, 13 December, 2010  
Anonymous Anonymous said...

Hi, Merely returned here to remind you regarding Mobile Monopoly. This is a superb system and WILL make you money, especially seeing as you own a website. Take a quick look at the video, the system is about using Mobile Advertising which is brand new and an unexposed market where you can make thousands by doing hardly any work. I assure you that after you watch their short video it will change you and it will start making you think. http://mobile-mastermind.com

Friday, 31 December, 2010  
Anonymous Anonymous said...

xngrgfnnc [url=http://ricostrong.net]Rico The Destroyer[/url]

Thursday, 06 January, 2011  

Post a Comment

<< Home