I have 2 code blocks
1
Class Employee
{
Address address;
Employee()
{
address=new Address();
}
}
2
Class Employee
{
Address address;
Employee(Address address)
{
this.address=address;
}
}
Then,how come 2nd block is loosly coupled and 1st is tightly coupled and why 1st will be hard to test and maintain sa compared to second.
The first block is considered tightly coupled because the Employee class creates a new
Addressinstance directly. The second is considered loosely coupled because theAddressinstance is created somewhere outside theEmployeeclass. As RonU commented, one example of using loose coupling is to facilitate the use of mock objects for testing purposes: