samedi 14 avril 2018

class calling into other class methods is violate (SOLID) Single Responsibility Principles (SRP)

this is small scenario of a project management system that i'm going to build in that system there is software projects and there is manager who can who is responsible for managing those projects

manager can

  • create projects
  • delete those projects
  • edit the details (update)

code is shown below

//class Manager

class Manager {

    private $managerName;


    public function __construct($managerName){
        $this->managerName        = $managerName;
    } 

    // getters
    public function getmanagerName(){/*code*/}

    // setters
    public function setmanagerName(){/*code*/}

    public function createProject(){
        /*code*/
        //passing values to projecct class constructor
        $p1 = new Project($projectName,$budget,$deadline,$projectDescription);
        //save p1 to database 
    }


    public function updateProject(){
    /*code - calling project class setters*/
    }

    public function deleteProject(){   /*code*/  }

}



//class Project

class Project {

    private $projectName;
    private $budget;
    private $deadline;
    private $projectDescription;

    public function __construct($projectName,$budget,$deadline,$projectDescription){
        $this->projectName        = $projectName;
        $this->budget             = $budget;
        $this->deadline           = $deadline;
        $this->projectDescription = $projectDescription;    
    } 

    // getters
    public function getprojectName(){/*code*/}
    public function getbudget(){/*code*/}
    public function getdeadline(){/*code*/}
    public function getprojectDescription(){/*code*/}

    // setters
    public function setprojectName(){/*code*/}
    public function setbudget(){/*code*/}
    public function setdeadline(){/*code*/}
    public function setprojectDescription(){/*code*/}
}


in Manager class

  • to create projects there is method available in manager class - createProject(); in this method createing project object by passing values to project class constructor

  • to update projects there is method available in manager class - updateProject() in this method thing happen is accessing project class setters conditionally

  • to create projects there is method available in manager class - deleteProject() in this method delete project object which was passed argument

in manager class above methods donot access it's insted of that those methods access methods of another class does this class violate the Single Responsibility Principle? because of those methods?

if yes what are the modifications to do achive Single Responsibility Principle in?

Aucun commentaire:

Enregistrer un commentaire