Skip to content

Instantly share code, notes, and snippets.

@rymawby
Created December 10, 2015 12:02
Show Gist options
  • Save rymawby/6130c5cd5fabd6d8634b to your computer and use it in GitHub Desktop.
Save rymawby/6130c5cd5fabd6d8634b to your computer and use it in GitHub Desktop.
Padawan to Jedi: Visitor Pattern

The Millenium Falcon has a crew of Han Solo, Chewbacca and Luke Sky Walker. Write the classes (included the Visitor class referenced below) you need to make the crew say (trace) their catchphrases (e.g. Chewy says "Gwaaraghhghll").

Class MilleniumFalcon
{
	protected var han:Pilot
	protected var chewbacca:Wookie;
	protected var luke:Gunner;
	
	function accept(visitor:IMilleniumFalconVisitor) : Void
	{
		visitor.visit(this)
	}
	
	function getCrew() : Array
	{
		return [han, chewbacca, luke]
	}
		
}
@cwright017
Copy link

Ok without further ado...

Class MilleniumFalcon
{
    private var crew:Array;

    function accept(visitor:IMilleniumFalconVisitor) : Void
    {
        visitor.visit(this)
    }

    function getCrew() : Array
    {
        return crew;
    }

    function addCrew(member : Crew) : Boolean
    {
        for each crewMember in crew
        {
            if (crewMember === member)
                return false;
        }

        crew.push(member)
        return true;
    }
}

Tried to remove the need for hardcoded crew members - would suck if one died and the whole ship would have to be recompiled before a replacement could be found - I dislike my approach though - I think I should have used decorators? Now because Wookie implements crew - any Wookie can join the crew, but they might not be properly trained. Instead maybe I could have made a decorator that could add skills to the wookie / other class upon completion of training?

Class Pilot implements Crew
{
    private var name:String

    function Pilot(name:String)
    {
        this.name = name;
    }

    function speak() : Void
    {
        print "I'm a Pilot - ZOOM!";
    }
}
Class Wookie implements Crew
{
    private var name:String

    function Wookie(name:String)
    {
        this.name = name;
    }

    function speak() : Void 
    {
        print "random weird noise!";
    }
}
Class Gunner implements Crew
{
    private var name:String

    function Gunner(name:String)
    {
        this.name = name;
    }

    function Speak() : Void
    {
        print "I'm a Gunner - pew pew!";
    }
}
Public Interface IMilleniumFalconVisitor
{
    function visit(crew:Crew) : Void
}
Class IMilleniumFalconVisitor
{
    function visit(crew:Crew) : Void
    {
        crew.speak();
    }
}

@rymawby
Copy link
Author

rymawby commented Dec 29, 2015

Agree with adding the addCrew function although I would separate out the return boolean to another method - I've never liked the returning of booleans if it is not explicit what it is actually for - it also violates the SRP as technically that method does both "adding crew members" and "does crew member exist".

You are almost there with the answer but there is a mistake with regards the type you are passing into the visitor - you have put type Crew whereas you pass in an object of type MilleniumFalcon.

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