Skip to content

Instantly share code, notes, and snippets.

@rymawby
Last active February 4, 2016 11:48
Show Gist options
  • Save rymawby/48680e999058593650b4 to your computer and use it in GitHub Desktop.
Save rymawby/48680e999058593650b4 to your computer and use it in GitHub Desktop.
Padawan to Jedi: Interface segregation principle

Use the Interface Segregation Principle to tidy the following:

interface ICrew
{
  function communicate(){}
  function useLightSaber(){}
  function flyShip(ship:Ship)
}
class Jedi extends Person implements ICrew
{
  function communicate()
  {
    print("Yo WASSUP?")
  }
  
  function useLightSaber()
  {
    if isEquippedWith(Weapon.LIGHTSABER)
    {
      fushwoon()
      wern()
    }
  }
  
  function flyShip(ship:Ship)
  {
    ship.addPilot(this)
  }
}
class R2D2 extends Robot implements ICrew
{
  function communicate()
  {
    print("Beep di beep squallucks beep")
  }
  
  function useLightSaber()
  {
    // not possible as I don't haz good sword arms
  }
  
  function flyShip(ship:Ship)
  {
    ship.addPilot(this)
  }
}
@cwright017
Copy link

interface ICrew extends ITalk, IJedi, IPilot
{
}
interface IJedi
{
  function useLightSaber(){}
{
interface ITalker
{
   function communicate(){}
}
interface IPilot
{
  function flyShip(ship:Ship)
}
class Jedi extends Person implements ICrew
{
  function communicate()
  {
    print("Yo WASSUP?")
  }

  function useLightSaber()
  {
    if isEquippedWith(Weapon.LIGHTSABER)
    {
      fushwoon()
      wern()
    }
  }

  function flyShip(ship:Ship)
  {
    ship.addPilot(this)
  }
}
class R2D2 extends Robot implements ITalker, IPilot
{
  function communicate()
  {
    print("Beep di beep squallucks beep")
  }

  function flyShip(ship:Ship)
  {
    ship.addPilot(this)
  }
}

@cwright017
Copy link

Obviously you could make other interfaces such as talkablePilot, talkableJedi ... but this is one of the reasons why using a decorator is better as you don't have to think up every combination under the sun for your interfaces

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