Skip to content

Instantly share code, notes, and snippets.

@johnbender
Forked from anonymous/interview q
Created March 28, 2011 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnbender/891527 to your computer and use it in GitHub Desktop.
Save johnbender/891527 to your computer and use it in GitHub Desktop.
// THE BAD
public class Lamp
{
private int bulbAge;
private String bulbType;
private int shadeAge;
public Lamp(int bulbAge, String bulbType, int shadeAge)
{
this.bulbAge = bulbAge;
this.bulbType = bulbType;
this.shadeAge = shadeAge;
}
public boolean needsMaintainance()
{
if(bulbType.equals("INCANDESCENT"))
return bulbAge > 2;
else if(bulbType.equals("HALOGEN"))
return bulbAge > 5;
return false;
}
}
// THE BETTER
public class Lamp
{
private Bulb bulb;
private Shade shade;
public Lamp(Bulb bulb, Shade shade)
{
this.bulb = bulb;
this.shade = shade;
}
public boolean needsMaintainence()
{
return bulb.needsMaintainence();
}
}
public abstract class Bulb
{
private int age;
public Bulb(int age)
{
this.age = age;
}
public boolean needsMaintainence()
{
return age > getMaxAge();
}
public abstract int getMaxAge();
}
public class IncandescentBulb extends Bulb
{
public IncandescentBulb(int age)
{
super(age);
}
@Override
public int getMaxAge()
{
return 2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment