Skip to content

Instantly share code, notes, and snippets.

@spencerdcarlson
Last active October 6, 2015 17:58
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 spencerdcarlson/17f345920ee90a1fa597 to your computer and use it in GitHub Desktop.
Save spencerdcarlson/17f345920ee90a1fa597 to your computer and use it in GitHub Desktop.
POJO example
public class Car {
private int year;
private String make;
private String model;
private double fuel;
private Boolean drivable;
public Car()
{
super();
this.year = 1935;
this.make = "Ford";
this.model = "Coupe";
this.fuel = 1.0;
this.drivable = true;
}
public void setDrivable(Boolean drivable) {
this.drivable = drivable;
}
public Boolean isDrivable() {
return drivable;
}
public void setMake(String make) {
this.make = make;
}
public String getMake() {
return make;
}
public void setModel(String model) {
this.model = model;
}
public String getModel() {
return model;
}
public void setYear(int year) {
this.year = year;
}
public int getYear() {
return year;
}
@Override
public String toString(){
return "Car@" + hashCode() + " [drivable="+isDrivable()+", make="+getMake()+", model="+getModel()+", year="+getYear()+"]";
}
/**
* POJOs are a simple data structure that can be passed around easily.
* I would argue that this method as well as the printStatus method are business logic and should be handled by another class
* Create CarManagment class or Interface or that has operations that can be performed on cars
**/
void driveCar(int distance) {
double usedFuel = (fuel / 20) * distance;
fuel = fuel - usedFuel;
if (fuel <= 0) {
drivable = false;
}
}
private void printStatus() {
if (isDrivable()) {
System.out.println("You ran out out of fuel in your " + getYear() + " " + getMake() + " " + getModel() + " and had to walk 2 days to find civilization.");
} else {
System.out.println("You drove your " + getYear() + " " + getMake() + " " + getModel() + " and you have " + fuel + " fuel left.");
}
}
}
public class CarTest {
public static void main(String[] args)
{
Car car = new Car();
System.out.println("New car: " + car);
}
}
@spencerdcarlson
Copy link
Author

The key to a POJO is that it follows a convention of setters and getters that some frameworks will utilize when passing around your object. A POJO is just a standard for how to represent data as an Object. Another benefit is that it leverages encapsulation. The pojo has complete control over how and when the data is manipulated. When all of your fields (make, model, year, etc) are declared as private it forces anyone wanting to access those fields to use the public getters and setters. Say for instance that you didn't want to allow other calling classes to modify the drivable field, simply modify the body of the setDrivable method and remove the logic, as long as you leave the method signature the same, you won't break any calling classes, but you will stop them from modifying the data. This might have downstream affects for the client, but at least they won't get a run time error. If drivable field were left to 'default' access level, than any class in the same package can modify it without using the getter or setter methods. Likewise if the access level is 'public' any calling class would be able to modify it and 'protected' would allow classes that extend it to modify it at their will.

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