Refactoring a java class

9

I have a Java class that contains 1756 lines (obviously it's not all code, there are blank lines, lots of comments and some commented code in case you need it in the future)

I'm implementing the MVC structure and this class belongs to the control.

This class is responsible for:

  • manage clients (communicate with clients, save who is connected, send information to them, know who left, etc.)
  • fetch data from a webservice and a database (synchronously)
  • Process this data and send this information to clients
  • Save important data information in the system database

Class structure ServidorService :

//variaveis
 + 8 variaveis 
 + de 12 HashMap //quase todos os dados estão relacionados key->value ex: clientex->outputx

//classes

Problem

The class is getting really big even for me, but thinking about the future of someone needing to change some function I think it will be difficult for her to maintain her code.

What is the best way to refactor it?

Top questions:

  • I want to create a new class,

  • I have tried to create another classes to support the main (ServerService) but I was having problems accessing the variables, what better way to make the other class have access to the variables? through gets?

asked by anonymous 22.10.2014 / 12:14

3 answers

4

From the responsibilities you mentioned, I suggested the following (class names are just suggestions, you can by your taste):

1 - Create classes with the implementation of specific methods according to the responsibilities

GestaoClientes.java : With the implementation of methods for handling customer management (Responsibilities 1 and 3);

AcessoBD.java : With the implementation of all methods of access and interaction with the Database (Responsibilities 1b and 4);

AcessoWebServices.java : With the implementation of all methods that interact with WebServices (Responsibility 1a).

Note: Responsibility 1a is "fetching data from a webservice" and responsibility 1b is "to a database" / p>

2 - Keep server-related and specific methods in the ServidorService class and variables (those that should be visible or modified by all methods).

If all variables remain in the ServidorService class then I believe that the methods in the classes I have suggested may be static. And you're just invoking the methods when you need them.

I noticed that you also have an internal class and I still did not understand the idea of it there, but I do not know what to suggest yet about it.

In this way I believe that you have already greatly reduced the number of lines and facilitate the maintenance of the code, since the other technician would know where to look in a specific way according to the objective.

    
22.10.2014 / 13:49
3

The MVC structure indicates you use Model where you will create class for your entities within it we have the attributes of the class and the get's and set's plus the toString() only that, if you are using database you can create classes calls DAO (Data Access Object) each entity has its DAO , the Controller is for the business rule or each entity has its Controler, and the View is for the information display.

You will separate everything in an example package:

br.nomeProjeto.model
br.nomeProjeto.controler
br.nomeProjeto.view
br.nomeProjeto.dao

Good practices say today that a very commented code is not very elaborate if necessary to create interfaces and implement their methods. I hope I have helped.

    
22.10.2014 / 12:37
2

I think it would be interesting for you to abstract methods that are not directly linked to the function of your class, such as utilitarian methods, but answering your question:

  • If your methods are related to what your class proposes I do not see why creating a new class, of course each has a different view of the subject;
  • It is always recommended to access attributes of other classes using getters and setters, low coupling, and high cohesion.
  • 22.10.2014 / 13:13