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!).
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,
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, 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.
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!)
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:
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.
Very-very interesting!
buy viagra
cheap viagra online
Bye
buy xanax buy xanax with prescription - xanax side effects recreational
cheapest viagra online viagra canadian pharmacy online - buy 150 mg viagra
viagra online without prescription safe dosage viagra - viagra dosage after prostate surgery
cheap viagra buy viagra online new zealand - order viagra in us
generic viagra viagra for women sale - buy generic viagra in us
viagra online without prescription can i buy viagra at walmart - viagra 65
buy tramadol online what is tramadol high like - tramadol online cod
[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]
soma pharmacy what does soma pills look like - soma drug definition
buy soma online no prescription soma benzodiazepine drug test - soma 250 muscle relaxer
buy soma does soma drug test - soma medication effects
generic cialis order real cialis - buy cheap genuine cialis
cialis online secure tabs online cialis 20 mg - cialis 80
[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
cialis online buy cialis for cheap - cialis side reviews
generic xanax does xanax show up on a drug test how long - generic xanax vs brand xanax
xanax online xanax bars vs alprazolam - xanax maoi drug
[url=http://buymetronidazole] http://buymetronidazole.info [/url]
[url=http://buymetronidazole] http://buymetronidazole.info [/url]
[url=http://buymetronidazole] http://buymetronidazole.info [/url]
generic xanax xanax online consultation - xanax drug experience
cialis online 36 hour cialis online - cialis manufacturer coupon
buy cialis online cialis 5mg. price in usa - buy cialis overnight
20000 :) Buy Azithromycin - cheap azithromycin online http://www.zithromaxhowtobuy.net/#generic-azithromycin, [url=http://www.zithromaxhowtobuy.net/#generic-azithromycin]Order Zithromax[/url]
20000 :) Neurontin 300mg - gabapentin without prescription http://www.neurontinonlinecheap.net/#Buy-Neurontin, [url=http://www.neurontinonlinecheap.net/#Generic-Gabapentin]Neurontin Online[/url]
20000 :) order celebrex online - buy celebrex celecoxib 200mg http://www.celebrexpharmsite.net/, [url=http://www.celebrexpharmsite.net/]order celebrex [/url]
buy tramadol online tramadol 50 mg od - tramadol hcl paracetamol tablets
03 Imitrex Online - generic imitrex for sale http://www.cheapimitrexbuy.net/#imitrex-online, [url=http://www.cheapimitrexbuy.net/#cheap-imitrex]Imitrex No Prescription[/url]
03 Purchase Sumatriptan - imitrex online no prescription http://www.cheapimitrexbuy.net/#imitrex-online, [url=http://www.cheapimitrexbuy.net/#imitrex-price]Imitrex No Prescription[/url]
4, cheap finasteride 5mg - cheap generic propecia http://www.propeciasaleonline.net/, [url=http://www.propeciasaleonline.net/]generic propecia no prescription [/url]
4, finasteride online - cheap propecia http://www.propeciasaleonline.net/#propecia-sale, [url=http://www.propeciasaleonline.net/#cheap-propecia-5mg]propecia sale[/url]
1, lasix without prescription - lasix no prescription http://www.lasixordernow.net/, [url=http://www.lasixordernow.net/]lasix drug [/url]
buy klonopin online 1 mg klonopin erowid - lortab klonopin high
http://landvoicelearning.com/#23561 get off tramadol addiction - tramadol addiction 30 pills a day
buy tramadol saturday delivery buy tramadol online in usa - tramadol hcl 50 mg nausea
buy klonopin online klonopin get you high - get prescribed klonopin online
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]
buy klonopin online klonopin .5 mg side effects - 2mg of klonopin to get high
Hi, buy nexium - order nexium http://www.wolverinefoodandspirits.com/, [url=http://www.wolverinefoodandspirits.com/]esomeprazole online[/url]
12, [url=http://www.lunestasleepaid.net/]lunesta mg [/url] - lunesta eszopiclone - lunesta cost http://www.lunestasleepaid.net/.
4, [url=http://www.sheddesignhub.com/] Imitrex Cost[/url] - Order Imitrex - buy generic imitrex online http://www.sheddesignhub.com/ .
12, [url=http://www.installandenjoy.com/]Zyban Pharmacy[/url] - Wellbutrin Price - buy zyban http://www.installandenjoy.com/
Post a Comment
<< Home