How are settings structured when they can be configured in diffferent ways?

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).

“Settings” are a classic cross-cutting concern similar to logging, caching etc. The simplest way to handle settings is to treat it as a singleton and have your Audio and Video classes directly interact with the settings class.

The general idea is to decouple the in-memory representation of settings (which could be a simple HashMap) from how it is persisted (file/registry, XML/JSON etc.). Java has a built-in Preferences API that works this way.

I’ll suggest the application of an oft-repeated part of the Unix philosophy: do one thing, and do it well. And yes, that rule is applicable to objects (and functions, and data structures in general, and multi-program systems, and…), not just programs.

So, what’s the one thing? An audio player? A video player? A file reader?

I’d suggest starting off by taking your settings.txt file, and having a single object that reads it, and then gets queried for values. After all, you can justify building a class that just does a good job of reading & writing that file instead of dragging the entrails of it’s parsing code all over the place, right? Parsing your settings file deserves to be listed as “one thing” under the philosophy.

After that, it seems to me that things get a bit more opaque. Do you want your audio & video classes to constantly have the same values after they get instantiated, or not? If their values should change, then do they go looking for changes, or get explicitly told about them? Do you want an interactive control object somewhere in all this? Do you want to chain video & audio classes? All of this needs to be decided, and individual bits picked out and run through the philosophical rule for the sake of producing clean, easily used “atoms” that can be substituted into the “final goal” for the sake of simplifying it.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật