Joseph Haugh
University of New Mexico




Back to Ducks, but with interfaces now
public interface Duck {
public void quack();
public void fly();
}
public class MallardDuck implements Duck {
public void quack() {
System.out.println("Quack");
}
public void fly() {
System.out.println("I'm flying!");
}
}
public interface Turkey {
public void gobble();
public void fly();
}
public class WildTurkey implements Turkey {
public void gobble() {
System.out.println("Gobble gobble");
}
public void fly() {
System.out.println("I'm flying a short amount!");
}
}
public class TurkeyAdapter implements Duck {
private Turkey turkey;
public TurkeyAdapter(Turkey turkey) {
this.turkey = turkey;
}
public void quack() {
turkey.gobble();
}
public void fly() {
for(int i = 0; i < 5; i++) {
turkey.fly();
}
}
}




remove() on an EnumerationIterator
public class EnumerationIterator implements Iterator {
private Enumeration en;
public EnumerationIterator(Enumeration en) {
this.en = en;
}
public boolean hasNext() {
return en.hasMoreElements();
}
public Object next() {
return en.nextElement();
}
public void remove() {
throw new UnsupportedOperationException();
}
}


HomeTheaterFacade, which exposes a few simple methods such as watchMovie()watchMovie() method
public class HomeTheaterFacade {
private Amplifier amp;
private Tuner tuner;
private DvdPlayer dvd;
private CdPlayer cd;
private Projector projector;
private TheaterLights lights;
private Screen screen;
private PopcornPopper popper;
public HomeTheaterFacade(Amplifier amp, Tuner tuner,
DvdPlayer dvd, CdPlayer cd,
Projector projector,
TheaterLights lights, Screen screen,
PopcornPopper popper) {
amp = amp;
tuner = tuner;
dvd = dvd;
cd = cd;
projector = projector;
lights = lights;
screen = screen;
popper = popper;
}
}
public void watchMovie(String movie) {
System.out.println("Get ready to watch a movie");
popper.on();
popper.pop();
lights.dim(10);
screen.down();
projector.on();
projector.wideScreenMode();
amp.on();
amp.setDvd(dvd);
amp.setSurroundSound();
amp.setVolume(5);
dvd.on();
dvd.play(movie);
}
public void endMovie() {
System.out.println("Shutting movie theater down");
popper.off();
lights.on();
screen.up();
projector.off();
amp.off();
dvd.stop();
dvd.eject();
dvd.off();
}
Without the Principle:
public float getTemp() {
Thermometer t = station.getThermometer();
return t.getTemperature();
}We get the thermometer object from the station and call the method ourselves.
With the Principle:
public float getTemp() {
return station.getTemperature();
}We add a method to the Station class that makes a request to the thermometer for us, reducing the number of classes we’re dependent on.
public class Car {
private Engine engine;
public Car() {
// initialize engine, etc.
}
public void start(Key key) {
Doors doors = new Doors();
boolean authorized = key.turns();
if(authorized) {
engine.start();
updateDashboardDisplay();
doors.lock();
}
}
public void updateDashboardDisplay() {
// update display
}
}
Does this violate the Principle of Least Knowledge?
public House {
private WeatherStation station;
// other methods and constructor
public float getTemp() {
return station.getThermometer().getTemperature();
}
}
Does this violate the Principle of Least Knowledge?
public House {
private WeatherStation station;
// other methods and constructor
public float getTemp() {
Thermometer therm = station.getThermometer();
return getTempHelper(therm);
}
public float getTempHelper(Thermometer therm) {
return therm.getTemperature();
}
}
HomeTheaterFacadeHomeTheaterFacade manages all the subsystem components for the client, keeping the client simple and flexible