Lets say we have an Webserver-Framework and a Database-Framework, and now i want to configure that Webserver and the Database, then it could look like the following
class Start
{
public static void main(String[] args)
{
new Start().start();
}
public void start()
{
var dbb= new DatabaseBuilder();
dbb.port(...);
dbb.adress(...);
dbb.user(...);
dbb.pw(...);
dbb.xyz();
var myDb = dbb.build();
var wb=new WebserverBuilder();
wb.port(...);
wb.address(...);
wb.securityPolicy(...);
wb.sslCert(...);
wb.xyz(....);
...
wb.router().path(...).do(...);
wb.router().path(...).do(...);
wb.router().path(...).do(...);
...
myWebserver=wb.build();
myWebserver.maybeSomeMoreConfiguration();
myWebserver.start();
}
}
So now i can refactor that start-method
in the example into more methods here in the Start-Class.
But then i want to move this new created methods into own classes.
So my Question:
- Is that a good idea doing this like so? Or are there better/other solutions getting object oriented code
- How to call this classes where i put in the methods (one method into one class)?
- This classes will then hold only one method each, and are more or less stateless. Is that really the right way?
- Are there any Design-Patterns out there, where this problem is described and solved? I didnt found one, but exists there problem-similar patterns?
Start
(or any other verb) you might not be using object-oriented thinking. What noun does this class represent to you?Builder
start with that file.Application
. In that case it might contain other classes that represent your database, your web server, etc. which could be member variables. And it could have astart
orrun
method, which in turn could call the methods of the database and web server objects to initialize them. The point is to get away from treating classes like methods.