In our daily work, we always write the same code and do it again and again, but how can we create the reusable components for our project and make our code more clearly?


In this article we just explain it at the concept side, so please refer the below 3 points that I have concluded;


1. Think more before you write the logic

Before we write the logic code after we understand the requirement, we must think more about the requirement firstly. Make sure that the part requirement clearly. Then put the part requirement into the whole architecture, and consider that what will affect the whole architecture based on the CR(change requirement) or the NR(new request). If the CR or NR has relationship with the original logic, we must be carefull to handle it. But if without relationship, we must design the logic high cohesion and low coupling. After you know the detail of them, then try to design it and write the logic.


2. Conclude the common or the similar logic

It will have most common and similar logic in one architecture, so in the phase of design, we need to find out of them. It's the best feature in OO concept. For example, the date format handler and some other utilize fucntions, we can conclude them then try to create the reusable components. In my first project after I joined the work, we need to handle the JSON and XML arrays without the key, just the value send to us from the 3th part company, they didn't provide us the solution. We send much email to the other team and company to search the common method to handle the response. But all of them us the java array to handle the response in each function. Finally, I think that all the work we do are same, just mapping the field by using the index number of the array. So I find that it's just the mapping feature, and suddenly I think that the JPA(Java Persistence Annotation) is the standard for the POJO fields mapping with the data table in database. So I use this case to design my framework like JPA to handle the array response. Finally, we use the common util and the annotation, other team member shouldn't care more about the convert, they just care more abou the logic for our project.


3. Create the reusable components at the right layer

High cohesion, and low coupling is the OO design rules. And we need to put it at the right layer, then we can gain the most best from our design. I concluded 3 ways to put the components at the right layer.

a. If your components will be used in each layer, just create a single components, then you can use in each layer.

b. If your components will be used in the superior layer, and the not in the each layer. Create it as a single or create it with the superior layer.

c. If your components will be used in the same layer only, just create it at this layer and separate with logic code.

I think the 3 rules should be like the Maven POM denpendency.


If you create the components correct with right logic, feature and layer, I think that it can make your code profit.


* If you have any doubt, please let me know and we can discuss more about this item.