Suppose of this question the following:
- I'm in full control of this project
- I'm writing a media player
Obviously, a media player will allow a user to adjust the volume, so I might have a class that looks like this:
public class Audio {
private int level;
// Constructor and other attributes left out
public int getCurrentVolume()
public void turnUp(int amount)
public void turnDown(int amount)
}
My media player will also allow you to take screenshots of current video, so I might have a class that looks like this:
public class Video {
private String screenshotsDirectory;
// Constructor and other attributes left out
public String getCurrentScreenshotDirectory()
public updateScreenshotDirectory(String newScrenshotPath)
}
Problem:
At some point, you'll write and read the data from a file, the problem is, you have have to create a stream for each type.
FileWriter volumeWriter = new FileWriter("settings.txt");
volumeWriter.writer(audioObj.getCurrentVolume());
FileWriter videoWriter = new FileWriter("settings.txt")
videoWriter.writer(videoObj.getCurrentScreenshotDirectory());
It would be nice to pass the FileWriter
object an abstract type, which means I could make an abstract class or interface called Settings
, but as far as I can see the settings don't share common behavior. Sure, the settings can change, but in different ways, for example, the screenshot path is a String
while volume is an int
.
Question:
What is the clean OOP way to structure classes that are on the same type (configuration/user settings), but can change and behave in different ways?
I can turn up the volume and change the screenshot path, but I cannot "turn up" the screenshot path or update the volume with a String (representing the path).