I have a question about applying the GRASP controller pattern while keeping it SOLID, more specifically, while keeping it with a single responsibility.
Wikipedia's Controller Pattern definition says:
The Controller pattern assigns the responsibility of dealing with system events to a non-UI class that represents the overall system or a use case scenario. A Controller object is a non-user interface object responsible for receiving or handling a system event.
And about SOLID's single responsibility principle:
In object-oriented programming, the single responsibility principle states that every class should have a single responsibility, and that responsibility should be entirely encapsulated by the class. All its services should be narrowly aligned with that responsibility.
Let's go to some example code. Let's say I have the following Java classes:
public class foo {
public foo(){}
public int foo1(){/*Some code*/}
public String foo2(){/*Some code*/}
public String foo3(int foo31){/*Some code*/}
}
public class bar {
public bar(){}
public int bar1(){/*Some code*/}
public String bar2(){/*Some code*/}
public String bar3(int bar31){/*Some code*/}
}
What's a good controller implementation while keeping a single responsibility on it? Do I just go through use cases or what? For example:
public class bazController {
private foo fooInstance;
private bar barInstance;
public bazController(){
this.fooInstance = new foo();
this.barInstance = new bar();
}
public void fooAction1(int arg){
this.foo.foo3(arg);
}
public void barAction1(int arg){
this.bar.bar3(arg);
}
public void fooAction2(){
this.foo.foo1();
}
public void barAction2(){
this.bar.bar1();
}
}
Am I keeping one responsibility here? Am I doing this, or understanding this, correctly? Thank you beforehand.
EDIT: What would happen if bazController
had this method, making both classes related?
public int bazAction(){
return this.foo.fooAction1() + this.bar.barAction1();
}