I'm not a much experienced developer, but I'll try to explore my ideas on this based on the understanding of the concepts.
Single Responsibility: I believe, it is the answer of the question "Your class is responsible for what?" when you are going to answer this question, you should tell only one responsibility. In your case, the answer is: "My class is responsible to control the baz"(Your implementation should do that of-course).
Since your answer specifies only one responsibility you implemented it properly.
But I think your code doesn't meet the D
of SOLID
. i.e. Dependency Injection. You could have injected foo
and bar
via constructor or by other means.
Update: Still your class is fine because the responsibility of your class is to control the baz
.
foo
and bar
are the components of baz
and you are controlling their action via your bazCntroller
.
I can say you violated Single Responsibility
when you add methods that is doing other job than the controlling of your baz. Ex:
public void LogBazExecution() {}
public int GetBazExecutionCount() {}
As you can see, it is not the responsibility of the baz controller
to keep track of how many times the baz action fired.
The reason behind the principle is ease of maintenance
. When each class is designed to have only one responsibility it is easy for you to find the location of the failure in your system and easy to extend it whenever required without introducing much new bugs.