Let's say we have following Address class:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Address { | |
String number | |
String firstLine | |
String secondLine | |
String postCode | |
String City | |
String State | |
} |
And we are using it in another class like Person:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person { | |
String firstName | |
String lastName | |
Address address | |
} |
And at the end, A client code that is going to use it in this way:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
println person.getAddress().getFirstLine() | |
println person.getAddress().getSecondLine() |
After:
Now Lets' change Person to add new methods to return first and second line of address:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person { | |
String firstName | |
String lastName | |
private Address address | |
String getAddressFirstLine() { | |
address.firstLine | |
} | |
String getAddressSecondLine() { | |
address.secondLine | |
} | |
} |
So client can utilize new methods in this way:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
println person.getAddressFirstLine() | |
println person.getAddressSecondLine() |
What are the pros & cons for each solution (before and after) ?