I am writing code to create a simple game, but I am not sure that this part is tightly coupled or not(high coupling):
There is an interface called GameEngine in which it controls the flow of the game. And there is a class called GameEngineImpl that implements the GameEngine interface. In other classes like A, B, and C, they use GameEngine like this:
public class A {
private GameEngine model;
public A(GameEngine model, ...) {
this.model = model;
}
public void draw() {
model.tick();
}
}
The other two classes also use GameEngine in a similar way as the class A.
When I converted it into UML class diagram, the classes, A, B, and C's dependencies were pointing towards the GameEngine interface and GameEngineImpl class was pointing the GameEngine interface.
Would this be a tightly coupled code?
Would this be a tightly coupled code?
No. Inserting the GameEngine
interface between class A
and GameEngineImpl
is exactly the right approach to alleviate tight coupling from A
to GameEngineImpl
.
What you don't want in your UML diagram is the classes A
, B
, and C
's dependencies pointing towards the GameEngineImpl
. You have solved this correctly through the use of an interface. Tight coupling does not include the interface.
Also, you will notice in your diagram the arrows pointing against each other.
A --> GameEngine <-- GameEngineImpl
This is a good thing. It is known as Inversion of Control.