This week I have decided to take the blog in a different direction. Instead of blogging about the weeks’ progress and what’s to come, I am going to start focusing much more on being informative and helpful to readers who may have had similar troubles. Today, I am going to be talking about interfaces in Java, and how I used them in developing my Rafter Maker app for Android. Now, why would I want to use an interface? Well, an interface will allow you to pass objects in your code generically. For instance, I can create any number of objects which implement the Rafter interface and then pass them all throughout the code defined generically as Rafter. This makes it possible to retrieve the same data from any type of rafter through a generic variable, while allowing the code in each rafter object to be unique. I created some examples to illustrate my point.
public interface Rafter{ getAngle(); getLength(); getName(); }
Above, is a simplified example of the Rafter interface in my app. The original has more methods, but that’s the only difference.
public class ShedRafter implements Rafter{ private double angle, length; public ShedRafter(double rise, double run){ // calculate angle and length from rise and run } @Override getAngle(){ return angle } @Override getLength(){ return length } @Override getName(){ return "Shed Rafter"; }
Above is a simplified example of a shed rafter class from my app. Notice it implements the Rafter interface and overrides each empty method declaration in the interface. Any class that implements an interface must override all the methods within that interface otherwise you will get an error that says something like “must implement the inherited abstract methods” meaning the abstract (undefined) methods in the interface.
public class GableRafter implements Rafter{ private double angle, length; public GableRafter(string pitch, double span){ // calculate angle and length from pitch and span <pre> @Override getAngle(){ return angle } @Override getLength(){ return length } @Override getName(){ return "Gable Rafter"; } }
Above here is a simplified example of a gable rafter class from my app which differs from the shed rafter class, but notice that both classes implement the Rafter interface allowing the creation of a Rafter object that can be a ShedRafter or a GableRafter .
double span = 144, rise = 60, run = 144; String pitch = "5/12" Rafter rafter = new ShedRafter(rise, run); rafter = new GableRafter(span, pitch)
The classes take different arguments with different names and the algorithms for calculating a rafter from a rise and run versus a pitch and span are also different; though I don’t show them for the purpose of simplicity. This approach allowed me to perform completely different calculations in each rafter object and on different variables, while still adhering to the interface definition by overriding getAngle(), getLength() and getName(). This way both classes were Rafters and Rafters are generic. While in contrast each individual rafter class implementing Rafter’s is unique.
This made it easy to create any rafter on the fly and pass it to my output class, which is responsible for displaying the angle length and name of the rafter. Now there is no need to check or know what type of rafter you have, because it does not matter, all the differences between rafters have been compartmentalized within the individual definitions. For instance, the getName() method is easily modified to return the proper names for each rafter, and likewise I can easily change the way I calculate angle and length in the constructor. The behavior of methods I might add to the interface later on will also be easily modified and will require minimal updating to the code overall.
double span = 144, rise = 60, run = 144; String pitch = "5/12" Rafter rafter = new ShedRafter(rise, run); System.out.println("Print " + rafter.getName() + " Data\n"); System.out.println("The angle is = " + rafter.getAngle()); System.out.println("The length is = " + rafter.getLength() + "\n"); rafter = new GableRafter(span, pitch) System.out.println("Print " + rafter.getName() + " Data\n"); System.out.println("The angle is = " + rafter.getAngle()); System.out.println("The length is = " + rafter.getLength() + "\n");
Above you can see it is easy now to define a single Rafter variable and use it to get the values from any rafter class which implements the Rafter interface.
Well, thats it for this blog.
Till next time.
From the blog jasonhintlian » cs-wsu by jasonhintlian and used with permission of the author. All other rights reserved by the author.