Saturday, June 11, 2005

Pr#1 Encapsulate What Varies

Looks like, this comes from the commonsense! After all, that's what the OO-paradigm (OOPS!) is all about! Then why look at it as a Design Principle?

Hmm, well, what OOP tells us is to think in terms of objects (did I not say 'Abstractions'?). So, if you are designing a game similar to the 'Age of Conquerors' in Linux (say, for instance, you are Linxemble Studios!), the first thing you might do is to identify all the objects. To name a few, Warrior{Cavalier, Knight, Archer, RifleMan etc..}, Weapon{Sword, Bow, Rifle}, Building{TownCenter, Barrack, Academy}, Resource{Gold, Stone, Wood, Berry, Sheep, Boar} that will interact. The next would be to see how they will interact. Then, how would you define and update the state of each of the object in the play. So on.

But wait! May I ask, how are you going to implement the algorithm of maintaining the state of any object in the play? You don't know yet! Right? But the design has to proceed, so you need to know exactly how would you maintain the state (think it as a collection of data members, for now).

Ok! Let me be specific. Every Warrior has to have some life! (Yeah, they do ofcourse! I mean, he can fight till he is not dead). Let's have a simple abstraction for the life, say an Integer (** private Integer life **) with a starting value of 10 (MAX_LIFE). Once the value reaches 0, the Warrior dies. Looks simple, huh? You'd ask, who governs the reduction of the life's value? Well, that's what we don't know yet precisely! But we need to device some way to handle it. OR else, can we proceed with the rest of the design (which eventually seems to boil down to similar decisions)? OO-paradigm does not talk about this (well, it's not supposed to!). Using OOP specs, you think, it's the life of a Warrior, so the algorithm to maintain it somehow needs to be encapsulated within the Warrior. And you are stuck because unless you know the algorithm precisely, you can't finish designing the Warrior itself!

Well, the Design Principle "Encapsulate what varies" has the answer! And this is where it differs from the OOP. To understand how, let's try to immitate a novice designer (like us!) who does not know any thing about the Principle. For now he thinks very simple (KISS?!) . He comes up with the following strategy (remember this word, we'll come back to this!).
Well, if the Warrior sits idle, unattacked, his life is no way gonna reduce! But under attack (could be from a wild animal or another Warrior), the reduction of life-value is a function of 'Armour Strength' of the attacked and the 'Attack Power' of the attacker.
So, he atleast for now, proceeds with the design. So it could be a method in Warrior, calculateReduction, that takes the attacker as an argument.

Now comes the real fight, where our Warrior is surrounded by a group of Warriors! The poor designer has to look back into the Warrior-method to accept a Collection (which has its own ramifications). Not a big deal! Some things are meant to change anyways (or we'll lose our jobs!). Later in the evening, the game planner (who does not know any programming) says, dude, I have a fantastic idea - there would be Monk(s) who could heal our Warrior under attack!!

You see the twist in the story, where the Warrior surrounded by attacks, is continually healed by a pack of Monks. And yet another change in the algorithm! You want more? Ok,
let a Warrior A be under attack from a Warrior B who's on a higher ground! Both the Warriors are of same calibre and started with same life, but, I want A to die first (in fact the only Warrior to die, in this situation!) as B is having the advantage of higher land.

And you run wild! This might change not only the warrior but many other!

So, is there any end to this? I don't know! But the principle seems to help us out. It says,
Well, you have discovered that the algorithm to calculate the life-reduction will always vary. Why not encapsulate it!

Well, that's what we try to do! No, not the OOP-way! The principle says, encapsulate the algorithm as an abstraction all together and let the objects under play have access to it. You assume some AbstractLifeCalculaionAlgothithm (which could be implemented later) and go ahead with the rest of the abstractions (Warrior, Nature etc..) . You see, you also have some scope for later optimizations, as now the algorithm is not within the individual objects under play, rather it's a centrally encapsulated entity (LifeCalculaionAlgothithm), which the objects under play have access to. This AbstractLifeCalculaionAlgothithm can change for ever, without forcing the other abstractions to change!

Well, some of you already familiar with Design Patterns, might say "Oh! it's just the Strategy Pattern!". I'd say "I do not care! (BANG!) ". My basis of study is Design Principles, not Designs Patterns (atleast here).

And to be true to you, this Design Principle does not actually differ from the encapsulation in OOP. But to project 'what this principle has so special about it which is not talked in any OOP book', I took the example we discussed.

Now let me talk about Abstractions. All what you saw in grey are examples of abstractions. Abstractions are the heros of our next Design Principle.

44 Comments:

Blogger Sunil Jigyasu said...

Sujeet, so glad to read this :) This is exactly how thinking takes a bumpy ride and one comes to identify the right abstractions in the system. Keep writing.

Saturday, 11 June, 2005  
Anonymous Anonymous said...

Very-very interesting!
buy viagra
cheap viagra online
Bye

Sunday, 10 December, 2006  
Anonymous Anonymous said...

buy xanax buy xanax with prescription - xanax side effects recreational

Sunday, 18 November, 2012  
Anonymous Anonymous said...

cheapest viagra online viagra canadian pharmacy online - buy 150 mg viagra

Wednesday, 23 January, 2013  
Anonymous Anonymous said...

viagra online without prescription safe dosage viagra - viagra dosage after prostate surgery

Thursday, 24 January, 2013  
Anonymous Anonymous said...

cheap viagra buy viagra online new zealand - order viagra in us

Friday, 25 January, 2013  
Anonymous Anonymous said...

generic viagra viagra for women sale - buy generic viagra in us

Friday, 25 January, 2013  
Anonymous Anonymous said...

viagra online without prescription can i buy viagra at walmart - viagra 65

Saturday, 26 January, 2013  
Anonymous Anonymous said...

buy tramadol online what is tramadol high like - tramadol online cod

Friday, 01 February, 2013  
Anonymous Anonymous said...


[url=http://mcbc.edu/xanax/xanax_metoprolol_675.html] Xanax Metoprolol [/url]
Reports of the benefits of Xanax pressure the concern of using it strictly as needed and also tapering wrong its use slowly to some extent than stopping the medication abruptly. Building up a stubbornness to the pharmaceutical can affect dosage calculations and, oft over tempo, DISPIRITING patients hand down essential to increase the amount infatuated in status to capture the benefits they did initially, although this is not the suit on all patients.

[url=http://mcbc.edu/xanax/xanax_addiction_withdrawal_538.html] Xanax Addiction Withdrawal [/url]

[url=http://mcbc.edu/xanax/a_substance_controlled_427.html] Is Xanax A Controlled Substance [/url]

Saturday, 02 February, 2013  
Anonymous Anonymous said...

soma pharmacy what does soma pills look like - soma drug definition

Monday, 04 February, 2013  
Anonymous Anonymous said...

buy soma online no prescription soma benzodiazepine drug test - soma 250 muscle relaxer

Wednesday, 06 February, 2013  
Anonymous Anonymous said...

buy soma does soma drug test - soma medication effects

Thursday, 07 February, 2013  
Anonymous Anonymous said...

generic cialis order real cialis - buy cheap genuine cialis

Saturday, 09 February, 2013  
Anonymous Anonymous said...

cialis online secure tabs online cialis 20 mg - cialis 80

Sunday, 10 February, 2013  
Anonymous Anonymous said...

[url=http://www.savoyswing.org/clomid/]clomid[/url]
An antibacterial is an agent that inhibits bacterial vegetation or kills bacteria. The arrange is over again employed synonymously with the dub antibiotic; today, however, with increased erudition of the causative

Tuesday, 12 February, 2013  
Anonymous Anonymous said...

cialis online buy cialis for cheap - cialis side reviews

Thursday, 14 February, 2013  
Anonymous Anonymous said...

generic xanax does xanax show up on a drug test how long - generic xanax vs brand xanax

Thursday, 21 February, 2013  
Anonymous Anonymous said...

xanax online xanax bars vs alprazolam - xanax maoi drug

Saturday, 23 February, 2013  
Anonymous Anonymous said...

[url=http://buymetronidazole] http://buymetronidazole.info [/url]

Saturday, 23 February, 2013  
Anonymous Anonymous said...

[url=http://buymetronidazole] http://buymetronidazole.info [/url]

Saturday, 23 February, 2013  
Anonymous Anonymous said...

[url=http://buymetronidazole] http://buymetronidazole.info [/url]

Saturday, 23 February, 2013  
Anonymous Anonymous said...

generic xanax xanax online consultation - xanax drug experience

Friday, 01 March, 2013  
Anonymous Anonymous said...

cialis online 36 hour cialis online - cialis manufacturer coupon

Saturday, 02 March, 2013  
Anonymous Anonymous said...

buy cialis online cialis 5mg. price in usa - buy cialis overnight

Sunday, 03 March, 2013  
Anonymous Anonymous said...

20000 :) Buy Azithromycin - cheap azithromycin online http://www.zithromaxhowtobuy.net/#generic-azithromycin, [url=http://www.zithromaxhowtobuy.net/#generic-azithromycin]Order Zithromax[/url]

Tuesday, 05 March, 2013  
Anonymous Anonymous said...

20000 :) Neurontin 300mg - gabapentin without prescription http://www.neurontinonlinecheap.net/#Buy-Neurontin, [url=http://www.neurontinonlinecheap.net/#Generic-Gabapentin]Neurontin Online[/url]

Tuesday, 05 March, 2013  
Anonymous Anonymous said...

20000 :) order celebrex online - buy celebrex celecoxib 200mg http://www.celebrexpharmsite.net/, [url=http://www.celebrexpharmsite.net/]order celebrex [/url]

Tuesday, 05 March, 2013  
Anonymous Anonymous said...

buy tramadol online tramadol 50 mg od - tramadol hcl paracetamol tablets

Thursday, 07 March, 2013  
Anonymous Anonymous said...

03 Imitrex Online - generic imitrex for sale http://www.cheapimitrexbuy.net/#imitrex-online, [url=http://www.cheapimitrexbuy.net/#cheap-imitrex]Imitrex No Prescription[/url]

Friday, 08 March, 2013  
Anonymous Anonymous said...

03 Purchase Sumatriptan - imitrex online no prescription http://www.cheapimitrexbuy.net/#imitrex-online, [url=http://www.cheapimitrexbuy.net/#imitrex-price]Imitrex No Prescription[/url]

Friday, 08 March, 2013  
Anonymous Anonymous said...

4, cheap finasteride 5mg - cheap generic propecia http://www.propeciasaleonline.net/, [url=http://www.propeciasaleonline.net/]generic propecia no prescription [/url]

Friday, 08 March, 2013  
Anonymous Anonymous said...

4, finasteride online - cheap propecia http://www.propeciasaleonline.net/#propecia-sale, [url=http://www.propeciasaleonline.net/#cheap-propecia-5mg]propecia sale[/url]

Friday, 08 March, 2013  
Anonymous Anonymous said...

1, lasix without prescription - lasix no prescription http://www.lasixordernow.net/, [url=http://www.lasixordernow.net/]lasix drug [/url]

Friday, 08 March, 2013  
Anonymous Anonymous said...

buy klonopin online 1 mg klonopin erowid - lortab klonopin high

Saturday, 09 March, 2013  
Anonymous Anonymous said...

http://landvoicelearning.com/#23561 get off tramadol addiction - tramadol addiction 30 pills a day

Saturday, 09 March, 2013  
Anonymous Anonymous said...

buy tramadol saturday delivery buy tramadol online in usa - tramadol hcl 50 mg nausea

Saturday, 09 March, 2013  
Anonymous Anonymous said...

buy klonopin online klonopin get you high - get prescribed klonopin online

Sunday, 10 March, 2013  
Anonymous Anonymous said...

all, order diazepam online - diazepam for sale no prescription http://www.diazepamhowtopurchase.com/#diazepam-for-sale-no-prescription , [url=http://www.diazepamhowtopurchase.com/#purchase-valium-online ]purchase valium online [/url]

Sunday, 10 March, 2013  
Anonymous Anonymous said...

buy klonopin online klonopin .5 mg side effects - 2mg of klonopin to get high

Tuesday, 12 March, 2013  
Anonymous Anonymous said...

Hi, buy nexium - order nexium http://www.wolverinefoodandspirits.com/, [url=http://www.wolverinefoodandspirits.com/]esomeprazole online[/url]

Thursday, 21 March, 2013  
Anonymous Anonymous said...

12, [url=http://www.lunestasleepaid.net/]lunesta mg [/url] - lunesta eszopiclone - lunesta cost http://www.lunestasleepaid.net/.

Friday, 22 March, 2013  
Anonymous Anonymous said...

4, [url=http://www.sheddesignhub.com/] Imitrex Cost[/url] - Order Imitrex - buy generic imitrex online http://www.sheddesignhub.com/ .

Wednesday, 27 March, 2013  
Anonymous Anonymous said...

12, [url=http://www.installandenjoy.com/]Zyban Pharmacy[/url] - Wellbutrin Price - buy zyban http://www.installandenjoy.com/

Thursday, 28 March, 2013  

Post a Comment

<< Home