1

I am developing a small JAVA game and I have this problem about cohesion and coupling in OOP concept.

I have a class named Locations as below

public class Location {
private int positionX;
private int positionY;

public Location(int positionX, int positionY){
    this.positionX=positionX;
    this.positionY=positionY;
}

public void updateLocation(int positionX, int positionY){
    this.positionX=positionX;
    this.positionY=positionY;
}

public String getLocation(){
    return (positionX+","+positionY);        
}

public int getLocationX(){
    return this.positionX;        
}

public int getLocationY(){
    return positionY;        
}

public void setLocationX(int positionX){
    this.positionX=positionX;        
}

public void setLocationY(int positionY){
    this.positionY=positionY;        
}
}

And I have an interface named locatable as bellow

public interface Locatable {
public Location getPosition();
public void setPosition(Location position);

}

And a abstract class implementing the above interface as bellow

public abstract class GameObject implements Locatable{
protected String name;
protected Location position;

public GameObject(String name, Location position){
    this.name=name;
    this.position=position;

}

//------------- getter & setters for positions----------

@Override
public Location getPosition(){
    return position;
}
@Override
public void setPosition(Location position){
    this.position=position;
}
//------------------------------------------------------
//------------------------------------------------------


//------------- getter & setters for name ---------------
public String getName(){
    return name;
}
public void setName(String name){
    this.name=name;
}
//------------------------------------------------------
//------------------------------------------------------

}

And a class that extends the above class

public class Lotus extends GameObject{
private int no_of_Petals=100;

public Lotus(String name,Location position){
    super(name,position);

}


//-----getters & setters for no of petals --------
public void setNoOfPetals(int no_of_Petals){
    this.no_of_Petals=no_of_Petals;
}

public int getNoOfPetals(){
    return no_of_Petals;
}
//------------------------------------------------
//------------------------------------------------

}

So I am confused about whether the using Location class all over my code is not a good coding practice.Like is it tightly coupling ? or is it okay ? Please let me clarify this question as I am still learning about OOP. if you are going to down vote this question please provide a comment about where I am wrong before doing so..

bGuom
  • 38
  • 1
  • 5
  • 4
    Do you have any reason to believe that you shouldn't do this? – Jonathon Reinhart Oct 21 '18 at 12:28
  • https://stackoverflow.com/questions/2832017/what-is-the-difference-between-loose-coupling-and-tight-coupling-in-the-object-o – rustyx Oct 21 '18 at 12:34
  • @JonathonReinhart I am new to this subject and I doubt whether it will cause tight coupling.. Please correct me if I am wrong.. Thanks for the reply – bGuom Oct 21 '18 at 12:45
  • 1
    You shouldn't seek to decouple every class from every other; your code will become a generic mess. You should seek to decouple at logical boundaries where it makes sense -- where you can reasonably expect to swap out implementation for another. – Jonathon Reinhart Oct 21 '18 at 14:00

2 Answers2

3

Yes. Location is tight coupled in your code. However, I believe tight coupling a generic class which would not change much, is still ok if that is how you want it.

Still if you want to loose couple, you can create an interface that Location class implements and use the interface everywhere in your code instead of the class name.

You can then create objects of the implementation and pass it to the methods where interface names are formal arguments

Mohan
  • 334
  • 2
  • 12
1

What you are asking for is called composition and here you can read more about composition over inheritance.

And few comments to your code:

In Lotus constructor you should use:

super(name, loc);

rather than:

this.name = name;
this.position = loc;

As this is already implemented in base class.

When you define getters for attributes in your class there is no reason to use this keyword, for example:

public int getLocationX() {
    return this.positionX;        
}

Should be replaced with:

public int getPositionX() {
    return positionX;        
}

In your setters good practise is to name the methods and their parameters same as class attribute:

public void setPositionX (int positionX){
    this.positionX = positionX;        
}

With that you can see what for this keyword can be used. With this you always refer to class attribute.

I propose to buy yourself a books about clean code and basics of java.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mershel
  • 542
  • 1
  • 9
  • 17
  • Thanks for your valuable answer. I learned lot from this.If you can recommend me good book to read that will be a big help – bGuom Oct 21 '18 at 12:58