TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
Why Copy-Paste is Bad for Your Code
1. Why Copy – Paste is Bad for Your
Code
A short intro to Design Patterns
Carlos C Tapang
May 26th, 2012
2. How many are developers?
How many have studied OO in college? Studied on your own?
How many are applying OO concepts at work? Hobby?
How many of you would say that OO concepts are hard?
I HAVE QUESTIONS
3. List of Requirements:
• Each type of Animal can have a different number of legs.
• Animal objects must be able to remember and retrieve this
information.
• Each type of animal can have a different type of movement.
• Animal objects must be able to return how long it will take to move
from one place to another given a specified type of terrain.
BUILDING A WILDLIFE GAME :
ANIMAL TAXONOMY
4. First-Cut Design Animal
enum Movement
Movement movement
{ Walking, Flying, Slithering }
class Animal
Movement movement
class Mammal : Animal
int numLegs Mammal
int numLegs
class Reptile : Animal
int numLegs
Reptile
int numLegs
5. Animal
First-Cut Design --------------------------------------
enum Movement
int numLegs
{ Walking, Flying, Slithering } Movement movement
class Animal
int numLegs
Movement movement
class Mammal : Animal
Mammal
class Reptile : Animal
Reptile
6. Animal
First-Cut Design --------------------------------------
enum Movement
int numLegs
{ Walking, Flying, Slithering } Movement movement
enum Terrain
{ Mountains, SandDunes, Sea }
class Animal
int numLegs Mammal
Movement movement decimal Speed(Terrain t)
decimal virtual Speed(Terrain t)
class Mammal : Animal
decimal override Speed(Terrain t)
class Reptile : Animal Reptile
decimal override Speed(Terrain t)
decimal Speed(Terrain t)
7. First-Cut
decimal Speed(Terrain t)
Mammal Bird
switch (movement) switch(movement)
{ {
case Walking: case Walking:
break; break;
case Flying: case Flying:
break; break;
case Slithering: case Slithering:
throw new Exception(); throw new Exception();
} }
8. First-Cut
decimal Speed(Terrain t)
Mammal Bird
switch (movement) switch(movement)
{ {
case Walking:
case Walking:
return WalkingSpeed(numLegs, t);
break; return WalkingSpeed(numLegs, t);
case Flying: break;
if (this is Bat) case Flying:
return BatFlyingSpeed(); return BirdFlyingSpeed();
else throw new Exception(); break;
break; case Slithering:
case Slithering:
throw new Exception();
throw new Exception();
} }
9. Second-Cut Design Animal
class Animal
int numLegs
int numLegs
class AnimalThatWalks : Animal
class AnimalThatFlies : Animal
AnimalThatFlies
AnimalThatWalks
10. Animal
Second-Cut Design int numLegs
enum Terrain
{ Mountains, SandDunes, Sea}
class Animal
int numLegs
decimal Speed(Terrain t) AnimalThatFlies AnimalThatWalks
class AnimalThatWalks : Animal
decimal override Speed(Terrain t)
class AnimalThatFlies : Animal
decimal override Speed(Terrain t)
Bird
class Bird : AnimalThatFlies
decimal override Speed(Terrain t)
Mammal
class Mammal : AnimalThatWalks
class Reptile : AnimalThatWalks
Reptile
11. Animal AnimalMovement
Third-cut design
class AnimalMovement
class Flying : AnimalMovement
class Walking : AnimalMovement Walking
Flying
class Animal
int numLegs
AnimalMovement movement
class Mammal : Animal
class Reptile : Animal Mammal Reptile
12. Animal
Third-cut design decimal Speed(Terrain t)
enum Terrain { Mountains, …}
class AnimalMovement
decimal Speed(Terrain t)
class Flying : AnimalMovement AnimalMovement
decimal Speed(Terrain t) decimal Speed(Terrain t)
class Walking : AnimalMovement
decimal Speed(Terrain t)
class Animal
int numLegs
AnimalMovement movement Walking Flying
decimal Speed(Terrain t)
decimal Speed(…) decimal Speed(…)
class Mammal : Animal
class Reptile : Animal
13. Third-Cut Design
Animal AnimalMovement
class Animal class AnimalMovement
int numLegs; Animal animal;
decimal speedFactor;
AnimalMovement movement; public AnimalMovement(Animal a)
{
public Animal() animal = a;
{ }
movement = new AnimalMovement(this);
} public decimal Speed(Terrain t)
{
public decimal Speed(Terrain t) return CalcSpeed(
{ animal.numLegs,
return movement.Speed(t); animal.speedFactor, t);
} }
14. If you find that you are doing a lot of copy-paste, it maybe time to step
back and review your design.
CONCLUSION
15. If I were to conduct a seminar on Design Patterns, would you attend?
How much is it worth to you? (full day seminar)
Yes, indeed I am conducting a seminar on June 23rd right here in Cebu.
If you need a copy of these slides and the sample, join me at Software
Design Patterns Philippines at LinkedIn.com or email me at
ctapang@hotmail.com
A SEMINAR FOR YOU
Notes de l'éditeur
Self-Intro:Microsoft IT Division “We eat dogfood”Physics Instructor (Physics 101 for non-science majors)