Control sample HARD GRASS?

I have a question about applying the GRASP controller pattern while keeping it SOLID, more precisely while keeping one responsibility.

Wikipedia The controller template definition says:

The Controller pattern assigns responsibility for system events to a non-UI class that represents a common system or use case. A Controller object is a non-user interface object that is responsible for receiving or handling a system event.

And about SOLID, the Single Responsibility Principle :

In object-oriented programming, the Single Responsibility Principle states that each class should have a single responsibility and that responsibility should be fully encapsulated by the class. All of his services must be closely aligned with this responsibility.

Go to the sample 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 is a good controller implementation while maintaining a single responsibility on it? Am I just looking at 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 the only one responsible here? Am I doing this or am I getting it right? Thanks in advance.

EDIT: What happens if I bazController

used this method by linking both classes?

public int bazAction(){
    return this.foo.fooAction1() + this.bar.barAction1();
}

      

+3


source to share


2 answers


I am not a very experienced developer, but I will try to explore my ideas about this based on understanding the concepts.

Single Responsibility: I believe this is the answer to the question, "Is your class responsible for what?" when you are going to answer this question, you only have to say one responsibility. In your case, the answer is "My class is responsible for controlling baz" (your implementation has to do this because of the course).

Since your answer only defines one responsibility, you have implemented it correctly.

But I think your code doesn't match D

SOLID

. those. dependency injection. You could enter foo

it bar

through the constructor or other means.

Update: However, your class is fine because your class's responsibility is control the baz

.



foo

and bar

are components baz

, and you control them with bazCntroller

.

I can tell you broke Single Responsibility

when you add methods that do different work than managing your baz. Example:

public void LogBazExecution() {}
public int GetBazExecutionCount() {}

      

As you can see, it's not worth baz controller

keeping track of how many times the baz action has fired.

The reason for the principle is ease of maintenance

. If there is only one responsibility per class, it is easy for you to locate the fault in your system and easily extend it as needed without introducing new bugs.

+2


source


It really depends on the context of the foo and bar classes and how closely the business-to-business aligns with the controller's business context.

In your example, you have methods that work with foo

and methods that work with methods bar

, but zero that work with foo

as well as bar

. To me this means that foo

u bar

probably don't have much in common.

This is how I would modify your example: (assuming foo

u bar

have nothing in common):



public class fooController
{
    private foo fooInstance;

    public fooController() {
        fooInstance = new foo
    }

    public void fooAction1(int arg){
        this.foo.foo3(arg);
    }

    public void fooAction2(){
        this.foo.foo1();
    }
}

public class barController 
{

    private bar barInstance;

    public bazController(){
        this.barInstance = new bar();
    }

    public void barAction1(int arg){
         this.bar.bar3(arg);
    }

    public void barAction2(){
         this.bar.bar1();
    }
}

      

EDIT
An example of where you would have a controller that delegates multiple instances of business logic might look something like this:

public class UserSettingsController
{
    private UserAddressLogic addressLogic;
    private UserPersonalInfoLogic personalInfoLogic;

    public UserSettingsController() {
        addressLogic = new UserAddressLogic();
        personalInfoLogic = new UserPersonalInfoLogic();
    }

    public User GetUser() {
        User user = new User();
        user.Address = addressLogic.GetUserAddress();
        user.PersonalInfo = personalInfoLogic.GetPersonalInfo();

        return user;
    }
}

      

+1


source







All Articles