Preface – This post is part of the Object Oriented Thinking series.
“Many Bothans died to bring us this information.”
“The enemy battlestation is heavily shielded and carries a firepower greater than half the starfleet. Its defenses are designed around a direct large-scale assault. A small, one-man fighter should be able to penetrate the outer defense. The Empire doesn’t consider a small, one-man fighter to be any threat…”
“The target area is a small thermal exhaust port only two meters wide. Only a precise hit with a proton torpedo will start a chain reaction which should destroy the station.”
“May the Force be with you…”
**ALL PILOTS, MAN YOUR SHIPS!!**
User lukeSkywalker = [SELECT Id FROM User WHERE Title = 'Jedi']; // Create Luke's X-Wing using the default constructorXWing lukesXWing = new XWing();// Use dot notation to manually set the attributes of the X-WinglukesXWing.pilot = lukeSkywalker; lukesXWing.protonTorpedoes = 4; lukesXWing.deathStarsDestroyed = 0; lukesXWing.powerRemaining = 1.00; lukesXWing.shield = new DeflectorShield(); lukesXWing.droid = new AstromechDroid('R2-D2');// Create a team of X-Wings to help with the battle! List<XWing> redSquadron = new List<XWing>(); List<User> pilots = [SELECT Id FROM User WHERE Title = 'Pilot']; for (User pilot : pilots) { // Use our custom constructor to quickly initiate new X-WingsXWing x = new XWing('Red ' + pilot.Id, pilot);redSquadron.add(x); System.debug(x.Name + ' standing by!'); }
**ENTERING HYPERSPACE**
// Use an X-Wing method and store its return valueBoolean isSuccess = lukesXWing.initiateHyperdrive();if (isSuccess != true) { System.debug('I have a bad feeling about this...'); } for (XWing x : redSquadron) { // You don't always have to use the return value!x.initiateHyperdrive();}
**FIGHTERS ABOVE YOU, COMING IN!!**
**RED SIX IS UNDER HEAVY FIRE!!**
// Use dot notation to traverse relationshipsredSquadron[5].shield.decreaseShields(0.50); redSquadron[5].shield.decreaseShields(0.30);System.debug('BAROOM!!'); redSquadron.remove(5);
**LOCK S-FOILS INTO ATTACK POSITION!!**
“Luke, trust your feelings.”
lukesXWing.shootLasers(2); lukesXWing.shootLasers(4); lukesXWing.shootLasers(10);System.debug('ZREEEET!');
**THIS IS RED LEADER, WE’RE BEGINNING OUR ATTACK RUN!!**
“Several fighters have broken from the main group.”
“Come with me.”
**I CAN’T SHAKE HIM – I’M HIT BUT NOT BAD!!**
lukesXWing.shield.decreaseShields(0.40);
**R2, SEE WHAT YOU CAN DO ABOUT IT!!**
lukesXWing.repair(); System.debug(lukesXWing.shield.capacity); // 1.00, baby!
**WE’RE ALMOST IN RANGE OF THE EXHAUST PORT!!**
**ALMOST THERE!!**
**ALMOST THERE!!**
**IT’S NO GOOD, WE CAN’T MANEUVER!!**
**THIS IS IT!!**
redSquadron.remove(0); redSquadron.remove(3); redSquadron.remove(2);redSquadron.clear();
**RED SQUADRON HAS BEEN WIPED OUT!!**
**LUKE YOU’RE OUR ONLY HOPE!!**
“I’m on their leader.”
“Use the Force, Luke.”
“The Force is strong with this one.”
**LUKE YOUR TARGETING COMPUTER IS OFF!!**
**WHAT’S WRONG!!**
**NOTHING, I’M ALRIGHT!!**
“I have you now…”
“What?!”
**YEAHOO! YOU’RE ALL CLEAR KID!!**
**NOW LET’S BLOW THIS THING AND GO HOME!!**
lukesXWing.fireProtonTorpedo();
// Don't forget the test class!System.assertEquals(1, lukesXWing.deathStarsDestroyed);
Too good and pretty clear explination thank you so much
You were unlimited buddy.
Awsome tutorial.
Awesome Man…!
This is the best explanation of using OOP concepts ever. You win the Internet.
YAY!!!!
Amazing narrative and a superb fun way towards OOP concepts. Thanks David for putting up the time and effort for this. I just wanna point out a little thing though. I couldn’t find out repair() method in XWing class. So maybe R2 could not increase the shield capacity, Luke couldn’t blow up the Death Star resulting the mission and test class ending up as a total failure. :)
LOL!!!! Bravo!!!
I love it, hahaha
Zreeeeeet! :)
Love the analogy you are using. Keeps it light and funny to make sense of it all ;-) Can’t wait for the next post, looks like it’s gonna be a “lightsaber”!