I'm new to Design Patterns, I know the purpose of single responsibility principle, but not 100% sure how it can avoid lots of tiny changes. Below is my example:
//very crude implementation
public class Journal
{
private readonly List<string> entries = new List<string>();
private static int count = 0;
public void AddEntry(string text)
{
entries.Add($"{++count}: {text}");
}
public void RemoveEntry(int index)
{
entries.RemoveAt(index);
count--;
}
public void SaveToDisk(string filename)
{
File.WriteAllText(filename, ToString());
}
}
I know the SaveToDisk
method should not be included in the class, it should be a dedicated class, like PersistenceManager
to handle the file saving.
But why can't I keep the SaveToDisk()
method in Journal
class? if there is any new requirements such as Save the Journal to cloud, then I just add a new method SaveToCloud()
, and any dependent client classes can use SaveToCloud()
, the only modification I need to make is adding SaveToCloud()
in Journal
class, which is totally fine?
Aucun commentaire:
Enregistrer un commentaire