Code Refactoring 4

Code Refactoring 4I wanted to cover in this video a bunch of questions I’ve received lately. Everything after this code refactoring tutorial will be more complex.

In this video, we’ll cover converting simple types into class objects. We’ll also look at how to move fields and methods into other classes and a bunch of tricks to get the most out of that. I also show a few of the many ways Eclipse can help you write code quicker. More tips on Eclipse are planned later in the tutorial. All of the code follows the video, to help you learn.

If you like videos like this, it helps to tell Google

Code from the Video

// Demonstrate how to extract a class, and move methods and fields
// Classes with too many responsibilities must me split
// 1. Look for a set of data that goes together
// 2. Create a new class with a name that describes that data
// 3. Move all the fields and methods 
// 4. Decide how to provide access to the new class

public class Customer {
	
	private String firstName = "";
	private String lastName = "";
	
	private Address address = null;
	
	private Birthday birthDay = null;
	
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	
	public Customer(String firstName, String lastName, String street,
			String city, String state, int postalCode) {
		super();
		this.firstName = firstName;
		this.lastName = lastName;
		this.address = new Address(street, city, state, postalCode);
	}
	
	public Customer(String firstName, String lastName, Address address,
			Birthday birthDay) {
		super();
		this.firstName = firstName;
		this.lastName = lastName;
		this.address = address;
		this.birthDay = birthDay;
	}
	public static void main(String[] args){
		
		Customer sallySmith = new Customer("Sally", "Smith", "123 Main St", "Perry", "Iowa", 50220);
		
		// The positive of accessing fields through accessor methods is that
		// subclasses can override the way fields are accessed and the fields
		// can be protected. The negative is that the code is hard to read
		
		System.out.println("Customer Name: " + sallySmith.getFirstName() + " " + sallySmith.getLastName());
		System.out.println("Address: " + sallySmith.address.getStreet() + " " + sallySmith.address.getCity() +
				" " + sallySmith.address.getState() + " " + sallySmith.address.getPostalCode());
		
		Address markJonesAddress = new Address("123 Main St", "Perry", "Iowa", 50220);
		
		Birthday markJonesBirthday = new Birthday(12,21,1974);
		
		Customer markJones = new Customer("Mark", "Jones", markJonesAddress, markJonesBirthday);
		
		// I can call for the birthday directly because I have toString() in Birthday
		
		System.out.println(markJones.birthDay);
		
		// Versus this, or the call to accessor methods
		
		System.out.println(markJones.birthDay.getBirthDate());
		
		// I can do the same with Address
		
		System.out.println(markJones.address);
		
	}

}

class Address{
	
	private String street = "";
	private String city = "";
	private String state = "";
	private int postalCode = 0;
	
	public String getStreet() {
		return street;
	}
	public void setStreet(String street) {
		this.street = street;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public int getPostalCode() {
		return postalCode;
	}
	public void setPostalCode(int postalCode) {
		this.postalCode = postalCode;
	}
	
	public Address(String street, String city, String state, int postalCode) {
		super();
		this.street = street;
		this.city = city;
		this.state = state;
		this.postalCode = postalCode;
	}
	
	public String toString(){
		
		return getStreet() + " " + getCity() + " " + getState() + " " + getPostalCode();
		
	}
	
}

// Early in development many fields are represented as primitives
// or Strings. Later in development custom objects make more sense

class Birthday{

	private int day;
	private int month;
	private int year;
	
	public int getDay() {
		return day;
	}
	public void setDay(int day) {
		this.day = day;
	}
	public int getMonth() {
		return month;
	}
	public void setMonth(int month) {
		this.month = month;
	}
	public int getYear() {
		return year;
	}
	public void setYear(int year) {
		this.year = year;
	}
	
	public Birthday(int day, int month, int year) {
		super();
		this.day = day;
		this.month = month;
		this.year = year;
	}
	
	public String getBirthDate(){
		
		return getDay() + " / " + getMonth() + " / " + getYear();
		
	}
	
	public String toString(){
		
		return getDay() + " / " + getMonth() + " / " + getYear();
		
	}
	
}

5 Responses to “Code Refactoring 4”

  1. Cris tan says:

    In the constructor, super() referred to the parent class but there is no used for inheritance

  2. Cris tan says:

    First of all thank you for making the tutorial video. Are you sill going to do the j2ee video? I hope you will cover spring, hibernate & others . Thanks again.

Leave a Reply

Your email address will not be published.

Google+