Concepts

Interfaces are Wonderful Things (part 3)

One way to extend the idea we looked at in part 2, is by looking at a Strategy pattern to decide which implementation to use based on information passed in. We would have a separate class which would take House (in the previous example) and decide based on that which Points implementation to use. This is useful when the decision needs to be made at Runtime.

For example, let’s say Professor Snape has found out about Neville Longbottom’s altercation with a Boggart. (To cut a long story short, Neville caused the creature to transform into Professor Snape dressed in his grandmother’s clothing, and Snape wasn’t best pleased at being the laughing stock of the school). In a fit of rage, Snape took 50 points off Neville, but couldn’t remember what house he was in, so couldn’t choose the correct Points implementation to use. He’d prefer for the application to handle that side of things, to be honest.

We could have a Wizard class, with a getHouse() method, and then decide which strategy to use based on the result of calling that method.

public interface Points {
    public void addPoints(int points);
}

public class GryffindorPoints implements Points {
    private int housePoints;

    @Override
    public void addPoints(int points) {
        this.housePoints += points;
    }
}

public class SlytherinPoints implements Points {
    private int housePoints;

    @Override
    public void addPoints(int points) {
        this.housePoints += 2 * points;
    }
}
//continue for all houses...

public class HouseResolver {
    public Points getPointsStrategyUsing(String house) {
        if (house.equals("Gryffindor")) {
            return new GryffindorPoints();
        }
        if (house.equals("Slytherin")) {
            return new SlytherinPoints();
        }
        //continue for all houses...
	}
}

public class Wizard {
	private String name;
	private String house;

	public Wizard(String name) {
		this.name = name;
	}

	public String getHouse() {
		return this.house;
	}
	//all other Wizard related code
}

public class SnapesSchedule {
    public static void main (String[] args) {
		Wizard neville = new Wizard("Neville Longbottom");
		HouseResolver resolver = new HouseResolver();

		Points pointsStrategy = resolver.getStrategyUsing(neville.getHouse());
		pointsStrategy.addPoints(-50);
    }
}

Here, the application will go in and check which house Neville belongs to (Gryffindor), and tell the House Resolver, which will then go and return the correct implementation (GryffindorPoints). Snape doesn’t have to worry about the details when docking points (which is probably why he does it so much).

<<<< Interfaces: Part 2

Advertisements

One thought on “Interfaces are Wonderful Things (part 3)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s