Lecture thumbnail 0:02 / 7:29 The single responsibility principle is just a piece of very good advice on how to build systems, and
it specifies that any particular class should have just a single reason to change.
Now, I know that sounds cryptic, so let me give you an example of something that you might build.
Let’s suppose that you want to make a journal where you write down your most intimate thoughts.
So you make a class called Journal and you have a bunch of entries in the journal.
So we’ll have a private read only list, a string of entries in your journal that’s going to be initialized
so that we don’t get any null reference exceptions.
We’re also going to keep a count of all the elements.
So private static int count equals zero.
Now, given that we have this journal, we might want to add entries to this journal.
So I might have a method called add entry like so.
And here I’m going to add an entry which is just a bunch of text.
And what I’m going to do is I’m going to say entries, dot add.
Let’s say I want to have the number of the entry.
So plus, plus count here and I’m going to have the entry itself.
That would be the text and I would return also the actual position of that particular entry.
By the way, that’s a memento pattern that we’re going to take a look in quite a while.
So in addition to having this Add entry, you might also want to remove the entry using that index.
So we have something like public void remove entry, which takes an index like so.
And all we have to do here is say entries dot remove, add and specify that particular index.
Not a very stable way of removing entries by the way, because once you remove one of the entries,
all the indices that you have to the other elements become invalid, which is something worth correcting
later on.
But anyways, this kind of functionality is relevant to the journal.
You might also want to generate some sort of two string implementation, so let’s do that.
So I’m going to override two string and what I’m going to do is I’m just join all the entries with a
line break.
So I’ll say string dot, join environment dot new line and have the entries here.
So these are my entries and I can start working with them so I can make a journal.
So here down below, what we can do is we can say var J equals new journal.
We can add a bunch of entries.
So I can say J dot add entry.
By the way, I hope I didn’t make it private.
So J dot add entry.
Let’s say I cry today.
Very sad journal.
And then another entry.
Let’s say I ate a bug.
So these are the two entries and we can now write line, write line the actual entries so we can say
write line J that will call to a string obviously, and we can execute this and hopefully everything
goes fine.
We get our output fingers crossed.
Okay.
So build succeeded.
And here are the two outputs.
Now all the methods inside the journal actually relate to the journal.
So we’re adding entries in the journal, we’re removing entries and in addition, we’re kind of printing
them to the command line or whatever.
We’re just outputting them to a string.
Now let’s suppose that you also want to persist.
The journal.
You might want to save it to a file.
So typically the kind of things that people might do is actually start making methods right here.
So you make a method called save, and this save will take maybe the file name.
You could also have different kinds of parameters.
So here you would do something simple like file dot write or text or something to that effect.
So file dot, write all text like so you would write the text to the file name and you would call toString
to get the actual text, of course.
So this is the kind of stuff you might want to start adding.
You would similarly have a method called load, which would actually also take a file name to load from,
and it might actually be a static method that returns a new journal.
So it might be a static journal load kind of implementation.
In addition, you might have another implementation which actually loads from a Uri, so you might have
something like this.
Now the problem here and the reason why we are violating the single responsibility principle is that
we’re adding too much responsibility to the journal class.
You can see that the Journal is now responsible not only for keeping the entries, but also managing
all of this persistence.
So you might have more than one reason to change the journal, not just to change the way that entries
are stored or the functionality for removing them, for example, because what I’ve implemented here
is not particularly efficient.
But in addition, you might also have additional reasons to change this when persistence changes.
So this is something you want to avoid and this is something that the single responsibility kind of
mentality should hopefully get you to.
Not do So The idea here is that instead of doing this, what you would typically do is you’d make a
new class which handles the persistence of things.
And this is where you would, for example, make a method called save to file, where you can save all
sorts of objects, but you can also save journals.
So you would take the journal, you would have the file name, you would maybe have a Boolean flag for
overriding false by default.
And then this is where you would do the actual implementation.
So if somebody wants to overwrite or there is no such a file, there is no file with the name file name,
then you go ahead and you file dot write all text.
You write all text with the file name, file name and once again, journal dot to string.
Well, we call the J, so J dot to string.
And that’s how you would serialize the whole thing.
So now we need two classes for persisting a journal, not just the journal itself, but also this persistent
class.
But the consequence and the kind of side effect of this is that you now have a separation of concerns.
So the Journal is concerned with keeping a bunch of entries and the persistent class is concerned with
saving whatever object that’s being fed, whether it’s a journal or something else to a file or maybe
to some web storage somewhere.
So you would say var P equals new persistence, persistence like so, and then you would specify the
file name.
So var file name equals and then some file name.
So let’s say C temp journal dot txt.
And then of course you can actually say p dot save to file.
You can specify the journal, the file name and maybe if you want to override, you specify true.
And once this is done, we might want to display what actually went on in there so we can say process
dot start.
So we can just look at the file name, file name and see that it actually works.
So if I execute this, you can see that the file has been saved.
Here is journal dot txt and here is its actual textual context.
So everything works just as we expected.
So the whole point of the single responsibility principle is that a typical class is responsible for
one thing and has one reason to change.
Play Stop Play Play Play Play Play