How to re -structure code for single responsibility | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 1

How to re -structure code for single responsibility

Hi I am aware that we should have single class for single responsibility. Refer code "dependency break" and it does A , B and C task which can easily done by three class mentioned in same code example. It was easy as there were no data members. Refer another code "dependency break 2". Here also three tasks are there which I want to break into three classes. But I am confused like what and how to do. First is to Fetch data from other server after authentication, then manipulate data and then write it in the excel file. As it has three responsibility, I thought to split it into three classes. But question and challenge is as below: How to Initialize three class objects ? Should I initialize them into constructor of one another ? Where to store (fetched data from data and structure mydata object in code) between first and second class ? In other words, Should I store data into class fetching it from database or should I store data into class who performs operations? Feel free 2 ask in case of que

16th Apr 2024, 8:56 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
6 Respostas
+ 1
Got your point, but this is just a sample code. In reality, it may become complex and we must have classes having their own responsibility rather than a single Class doing all the stuff.
17th Apr 2024, 5:06 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
The link seemed to have been copied badly. I found another link with even more design pattern examples. see the updated link. here is the copy-pasted example from the site on how classes can be used as external dependencies. https://sololearn.com/compiler-playground/cmF7g9di5GQp/?ref=app
17th Apr 2024, 12:54 PM
Bob_Li
Bob_Li - avatar
0
why not keep them as methods? creating new classes just to use them as methods feels like overcomplication to me. Also, since you are going to be using shared data, using multiple classes could become unneccesarilly more complicated
17th Apr 2024, 4:44 AM
Bob_Li
Bob_Li - avatar
0
maybe you can create methods that accepts the other classes as parameters if they are static classes or instances of or pointers to those classes if they are dynamic. It depends on what they ultimately do and how they are used. Yes, you might even initialize them locally if you need their functionality. As for where to store the data, again it depends on what data it is. It should be most accessible to the class that is primarilly using it. It's the same situation when we use the standard library or external libraries. We sometimes instantiate them in classes, in functions or in in main(). The data can be encapsulated or it can be publicly shared and accessible. It depends on what we want to do. Or maybe you are trying to model a dependency injection architecture? https://www.bogotobogo.com/DesignPatterns/Dependency_Injection_Design_Pattern_Inversion_Of_Control_IoC.php
17th Apr 2024, 7:32 AM
Bob_Li
Bob_Li - avatar
0
I am trying to understand what is good. Unfortunately, link you have shared is broken for me and cannot access.
17th Apr 2024, 12:07 PM
Ketan Lalcheta
Ketan Lalcheta - avatar