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]
	}
		
}
@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