Undesired dependency and SRP

posted in Event-driven engine for project Hangman
Published July 28, 2021
Advertisement

I'm trying to extend the hangman mini-game adding the remaining number of attempts in a picture instead of just text.

It seems a very simple task, just instead of calling printLine for the remaining attempts, we call printImage to show the corresponding picture. Just a new interface entry and an AWT canvas method.

public void printImage(String fileName);


But, there are more details to review…

  • If the parameter is just the name of the picture, the canvas method will need to know how to read the file.
  • What if we change the storage details? Canvas method should be updated.
  • If we use an atlas picture, we will need extra parameters. So, again, the canvas method will need to understand all the details.

It seems that this is not the right way to implement it. It breaks the Single Responsibility Principle (https://en.wikipedia.org/wiki/Single-responsibility_principle)

So let's manage the storage issues in other block and just pass “data” to the canvas method.
Again it sounds simple, but… what type of data?

Native AWT functions accept an Image as input, but that is an abstract class. We can use BufferedImage to load an image and send it to the canvas method, ok by now…

boolean java.awt.Graphics.drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer)

java.awt.Image --> The abstract class Image is the superclass of all classes that represent graphical images. The image must be obtained in a platform-specific manner.
java.awt.image.BufferedImage --> The BufferedImage subclass describes an Image with an accessible buffer of image data.

About who is in charge to load it, we can create another interface/class to load the file from storage (IStorage / StorageManager) that will provide the image data.
Now we need to connect Storage classes with Output classes. In the main loop we can send the correct picture every update but that means that our main loop is aware of the image data type, in other words, we need to import all the data classes in our main loop class.

import java.awt.Image;

That means that there are references of the image data type in several places: main loop class, canvas output interface, canvas output class, and of course in the classes that load the image…
Multiple references to a specific class of one implementation of the canvas output… I don't like it.

The alternative that comes to my mind is creating our own specific data types, maybe just a wrapped around any standard java class.
That way we manage what can be done with the data. StorageManager will load it in the way we need, and the Canvas methods will use the data as we have defined it.

Any ideas?
Please, comment.

Previous Entry Another mini-game
Next Entry Release 0.1.0
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement