Skip to content

Instantly share code, notes, and snippets.

@rymawby
Created September 17, 2015 20:53
Show Gist options
  • Save rymawby/c20047953811151bbbf6 to your computer and use it in GitHub Desktop.
Save rymawby/c20047953811151bbbf6 to your computer and use it in GitHub Desktop.
Padawan to Jedi: Improve with Decorators
Class Gun
{	
	shootSound = "Bang"
	function shoot()
	{
		makeNoise(shootSound)
	}
}

Class PewPewGun extends Gun
{
	shootSound = "Pew Pew"	
}

Class PewPewLaserGun extend PewPewGun
{
	shootSound = super.shootSound + " LASERS"
}
@cwright017
Copy link

Class Gun
{   

    function shoot(shootSound="Bang Bang" as Sound)
    {
        makeNoise(shootSound)
    }
}

Class PewPewGun 
{
    shootSound = "Pew Pew" 
    _baseGun

    function PewPewGun(gun as Gun)
    {
        _baseGun = gun
    }

    function shoot()
    {
        _baseGun.shoot(shootSound)
    } 
}

Class Lasers 
{
    _baseGun

    function Lasers(gun as Gun)
    {
        _baseGun = gun
    }

    function shoot()
    {
        _baseGun.shoot(shootSound) + "LASERS!"
    } 
}

? So here you could then do:

myCrappyGun = gun()
myCrappyGun.shoot() //Bang

myPewPew = PewPewGun(myCrappyGun)
myPewPew.shoot() //Pew Pew

myLaser = Lasers(myPewPew)
myLaser.shoot() //Pew Pew LASERS!

but still be able to call them without their new features so

myPewPew.shoot() //Pew Pew

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment