javasolid-principlesloose-couplingcohesion

Is this considered low coupling & high cohesion? Any chance to improve?


I'm trying to get hold of the SOLID principles by Robert C. Martin. Currently I'm looking into low coupling & high cohesion. I've created some code which represents my current understanding of this subject. Could you guys tell me if on the right track? Any chance to improve the current design?

Main app which creates two addresses and assigns these to the employee:

public class App {

    public static void main(String[] args) {
        Address homeAddress = new HomeAddress("This is my Home Address");
        Address workAddress = new WorkAddress("This is my Work Address");        
        Employee employee = new Employee(homeAddress, workAddress);

        employee.getAddresses();
    }

}

Employee class:

public class Employee {

    private Address homeAddress;
    private Address workAddress;

    Employee(Address homeAddress, Address workAddress) {
        this.homeAddress = homeAddress;
        this.workAddress = workAddress;
    }

    public void getAddresses() {
        System.out.println("homeAddress: " + homeAddress.getAddress());
        System.out.println("workAddress: " + workAddress.getAddress());
    }

}

Address interface:

public interface Address {

    String getAddress();

}

Specific Address implementation 1(HomeAddress):

public class HomeAddress implements Address {

    String specificAddress;

    public HomeAddress(String specificAddress) {
        this.specificAddress = specificAddress;
        System.out.println("In HomeAddress Constructor");
    }

    public String getAddress() {
        return specificAddress;
    }
}

Specific Address implementation 2(WorkAddress):

public class WorkAddress implements Address {

    String specificAddress;

    public WorkAddress(String specificAddress) {
        this.specificAddress = specificAddress;
        System.out.println("In WorkAddress Constructor");
    }

    public String getAddress() {
        return this.specificAddress;
    }
}

Any help/feedback would be greatly appreciated! Thanks in advance.

Marc.


Solution

  • It's a smallish example, but it could be improved in terms of coupling/cohesion.

    The objects are cohesive. Why? In the Employee object both the constructor and the getAddresses() (which should be called printAddresses() by the way) refer to both instance variables (which means they are concerned with the same thing). Same for the Address objects.

    On the coupling part I think you could do better. As it stands now, the Employee objects "knows about" (i.e. is coupled to) the internal representation of the Address object. That is because you "export" the data (the String) from the Address object instead of printing it right there where the data is.

    This makes your objects more coupled, and will cause any change (for example introducing Street and City and things like that) in the Address objects to leak up to the Employee. So it has real downsides.

    The solution is to define a print() method in the Address and do the System.out.println() there. This is in line with other concepts, such as the Law of Demeter, Tell Don't Ask.