Blog Full With Filter

Caption placed here
30
Jan
2025
1

DECORATOR

The Decorator pattern is a structural design that allows you to improve or change the behavior of objects at runtime. This is done by creating a set of decoration classes used to wrap the components. Each decorator adds a specific functionality or behavior to the component, and you can stack multiple decorators to create various combinations. In this section we implement the model under spring.

Implementing the Decorator Pattern in Spring

Let’s illustrate the Decorator pattern with a fun example about a user with various roles who can create a practice, but by role type you can create different practices with specific rules. We’ll start with a simple explanation.

Overall, the hierarchy of classes that have been implemented therefore becomes the one shown in the following figure.

Step 1: Create the Interface

// Interface
interface IPracticesService {

   PraticeValidDTO validPractice (PracticeDTO praticaDTO);
   
   PracticeDTO updatePractice (PracticeDTO praticaDTO);
   void archivePractice (Long idPractice);
   void cancelPractice (Long idPractice);
   PracticeDTO saveDraft (PracticeDTO practiceDTO);
   void saveStatusPractice (Long idPractice, String statusPractice);
   Boolean deleteDraft (Long id);
   void revocationPractice (RevocationPracticeDTO revocationPracticeDTO);
   List<SearchPracticeResponseDTO> searchPractices (SearchPracticeRequestDTO searchPracticeRequestDTO);
}

Step 2: Implement the Concrete Service

// Concrete Service
@service ("ciPracticeBaseService")
public class CIPracticeBaseService implements IPracticesService {

   @Override
   public PraticeValidDTO validPractice(PracticeDTO praticaDTO) {
      //logic to validate practice
      System.out.println("Call validPractice of ciPracticeBaseService");
      return new PraticeValidDTO();
   }

   @Override
   public PracticeDTO updatePractice(PracticeDTO praticaDTO) {
      // logic to update practice
      System.out.println("Call updatePractice of ciPracticeBaseService");
      return new PracticeDTO();
   }

   @Override
   public void archivePractice(Long idPractice) {
      System.out.println("Call archivePractice of ciPracticeBaseService");
   }
}

Step 3: Develop the Decorator

// Decorator
@Service("ciPracticeBaseDecoratorService")

public class CIPracticeBaseDecoratorService implements IPracticesService {

   protected IPracticesService ciWrapperService;
   public CIPracticeBaseDecoratorService(@Qualifier("ciPracticeBaseService")    IPracticesService ciPracticeService) {
      ciWrapperService = ciPracticeService;
   }

   @Override
   public PraticeValidDTO validPractice(PracticeDTO praticaDTO) {
      return ciWrapperService.validPractice(praticaDTO);
   }

   @Override
   public PracticeDTO updatePractice(PracticeDTO praticaDTO) {
      return ciWrapperService.updatePractice(praticaDTO);
   }
}

Step 4: Create Concrete Decorators for user for every role

// Concrete Decorators
@Service("ciCompanyDecoratorService")
public class CICompanyDecoratorService extends CIPracticeBaseDecoratorService {

   public CICompanyDecoratorService(@Qualifier("ciPracticeBaseDecoratorService") IPracticesService ciPracticeService) {
      super(ciPracticeService);
   }

   @Override
   public PraticeValidDTO validPractice(PracticeDTO praticaDTO) {
      return super.validPractice(praticaDTO);
   }

   @Override
   public PracticeDTO updatePractice(PracticeDTO praticaDTO) {
      System.out.println("Call updatePractice of ciCompanyDecoratorService"); 
      return new PracticeDTO();
   }

   @Override
   public void archivePractice(Long idPractice) {
      super.archivePractice(idPractice);
   }
.
.
}

@Service("ciIntermDecoratorService")
public class CIIntermDecoratorService extends CIPracticeBaseDecoratorService {

   public CIIntermDecoratorService(@Qualifier("ciPracticeBaseDecoratorService")   IPracticesService ciPracticeService) {
      super(ciPracticeService);
   }

   @Override
   public PraticeValidDTO validPractice(PracticeDTO praticaDTO) {
      return super.validPractice(praticaDTO);
   }

   @Override
   public PracticeDTO updatePractice(PracticeDTO praticaDTO) {
      System.out.println("Call updatePractice of ciIntermDecoratorService"); 
      return new PracticeDTO();
   }

   @Override
   public void archivePractice(Long idPractice) {
      System.out.println("Call archivePractice of ciIntermDecoratorService"); 
   }
}

Step 5: Using the Decorators

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

   @Autowired
   private PracticeFactory factory;

   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }

   @Override
   public void run(String... args) throws Exception {
      IPracticesService serviceFactory = factory.getService(EnumUser.COMPANY);
      System.out.println("User to call function company");
      serviceFactory.validPractice(new PracticeDTO()); 
      serviceFactory.updatePractice(new PracticeDTO());
      serviceFactory.saveDraft(new PracticeDTO());
      serviceFactory.saveStatusPractice(Long.getLong("123456"), "ACTIVE");
      System.out.println("");
      System.out.println("User to call function intermediary");
      serviceFactory = factory.getService(EnumUser.INTERMEDIARY);
      serviceFactory.validPractice(new PracticeDTO()); 
      serviceFactory.updatePractice(new PracticeDTO());
      serviceFactory.saveDraft(new PracticeDTO());
      serviceFactory.saveStatusPractice(Long.getLong("123456"), "NO ACTIVE");
      System.out.println(""); 
      System.out.println("User to call function regional");
      serviceFactory = factory.getService(EnumUser.REGIONAL);
      serviceFactory.validPractice(new PracticeDTO()); 
      serviceFactory.updatePractice(new PracticeDTO());
      serviceFactory.saveDraft(new PracticeDTO());
      serviceFactory.saveStatusPractice(Long.getLong("123456"), "ENDED");
      System.out.println("");
   }
}

In this example, we have a Practice interface, a CIPracticeBaseService concrete service, and decorator: CICompanyDecoratorService. These decorator extend for user with different role other decoretors for CICompanyDecoratorService, CIIntermDecoratorService and CIRegDecoratorService class.

Conclusion

The Decorator model is a versatile tool for building flexible and extensible systems. It allows you to dynamically improve the behavior of objects, making your code more maintainable and adaptable to different needs. Using this model you can create various combinations with complex functionality without the need for a convoluted class hierarchy. Here you can found code example.

29
Jan
2025
0

STRATEGY

With the strategic model we define multiple algorithms and let the client application pass the algorithm to use as a parameter. Depending on the different interface implementations, the objects execute the method in different ways. For our case we have various reports that give different results and different searches depending on the type and we will try to implement different strategies for the search and the report in csv format based on the type we pass. First of all we will create the interface for the strategy pattern, in our case to create the report we create IReportServiceStrategy.java

Implementing the Strategy Pattern in Spring

Let’s illustrate the Strategy pattern with the following figure.

Step 1: Create the Interface

public interface IReportServiceStrategy {
   ByteArrayOutputStream exportExcel (ReportFilterDTO filter);
   List<ReportDTO> searcher (ReportFilterDTO filter);
}

Step 2: Create the Strategy Class

@Service("companyReportServiceStrategy")
public class CompanyReportServiceStrategy implements IReportServiceStrategy {

   @Override
   public ByteArrayOutputStream exportExcel(ReportFilterDTO filter) {
      System.out.println("Call exportExcel of CompanyReportServiceStrategy");
      return null;
   }

   @Override
   public List<ReportDTO> searcher(ReportFilterDTO filter) {
      System.out.println("Call searcher of CompanyReportServiceStrategy");
      return null;
   }
}

@Service("regionalReportServiceStrategy")
public class RegionalReportServiceStrategy implements IReportServiceStrategy {

   @Override
   public ByteArrayOutputStream exportExcel(ReportFilterDTO filter) {
      System.out.println("Call exportExcel of RegionalReportServiceStrategy");
      return null;
   }

   @Override
   public List<ReportDTO> searcher(ReportFilterDTO filter) {
      System.out.println("Call searcher of RegionalReportServiceStrategy");
      return null;
   }
}

Step 3: Create the Service

@Service
public class ReportService {

private ReportFilterDTO reportFilter; 

public void addFilter(ReportFilterDTO reportFilter){
   this.reportFilter = reportFilter;
}

public List<ReportDTO> searcher(IReportServiceStrategy reportServiceStrategy){
   return reportServiceStrategy.searcher(this.reportFilter);
}

public ByteArrayOutputStream exportExcel(IReportServiceStrategy  reportServiceStrategy){
    return reportServiceStrategy.exportExcel(this.reportFilter);
   }
}

Step 4: Using the Strategy

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

   @Autowired
   private ReportService reportService; 

   @Autowired
   @Qualifier("companyReportServiceStrategy")
   private IReportServiceStrategy companyReportServiceStrategy;

   @Autowired
   @Qualifier("regionalReportServiceStrategy")
   private IReportServiceStrategy regionalReportServiceStrategy;

   @Autowired
   @Qualifier("stateReportServiceStrategy")
   private IReportServiceStrategy stateReportServiceStrategy;

   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }

   @Override
   public void run(String... args) throws Exception {
      //Strategy
      ReportFilterDTO reportFilterDTO = new ReportFilterDTO();
      reportFilterDTO.setState("State");
      reportFilterDTO.setCompanyType("Company");
      reportFilterDTO.setStatus("Status");
      reportService.addFilter(reportFilterDTO);
      System.out.println("Report call function search");
      reportService.searcher(companyReportServiceStrategy);
      reportService.searcher(regionalReportServiceStrategy);
      reportService.searcher(stateReportServiceStrategy);
      System.out.println("");
   }
}

Conclusion

Strategy pattern is useful when we have multiple algorithms for a specific task and we want our application to be flexible to choose any of the algorithms at runtime for a specific task. Here you can found code example.

28
Jan
2025
0

FACTORY

Factory design pattern is used when we have a superclass with multiple subclasses and based on the input, we need to return one of the subclasses. This pattern removes the responsibility of instantiating a class from the client program to the factory class. The case addressed was for the creation of a practice that could be created differently depending on the user role. The approach was to create various classes depending on the user role and then instantiate the class based on the role in the factory.

Implementing the Factory Pattern in Spring

Let’s illustrate the Factory pattern with a fun example we’ll start with a simple explanation.

Overall, the hierarchy of classes that have been implemented therefore becomes the one shown in the following figure.

Step 1: Create the Class

@Component
public class PracticeFactory {

@Autowired
@Qualifier("ciCompanyDecoratorService")
private IPracticesService ciCompanyDecoratorService;

@Autowired
@Qualifier("ciIntermDecoratorService")
private IPracticesService ciIntermDecoratorService;

@Autowired
@Qualifier("ciRegDecoratorService")
private IPracticesService ciRegDecoratorService;

public PracticeFactory(IPracticesService ciCompanyDecoratorService,
                       IPracticesService ciIntermDecoratorService, 
                       IPracticesService ciRegDecoratorService) {

   this.ciCompanyDecoratorService = ciCompanyDecoratorService;
   this.ciIntermDecoratorService = ciIntermDecoratorService;
   this.ciRegDecoratorService = ciRegDecoratorService; 
}

public IPracticesService getService(EnumUser user) {

   IPracticesService iPracticesService = null;
   switch (user) {
      case COMPANY: {
         iPracticesService = ciCompanyDecoratorService;
         break;
      }
      case INTERMEDIARY: {
         iPracticesService = ciIntermDecoratorService;
         break;
      }
      case REGIONAL: {
         iPracticesService = ciRegDecoratorService;
         break;
      }
      default:
         throw new IllegalArgumentException("Unexpected value: " + user.getCode());
      }
   return iPracticesService;
}

Step 2: Using the Factory

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

@Autowired
private PracticeFactory factory;

public static void main(String[] args) {
   SpringApplication.run(DemoApplication.class, args);
}

@Override
public void run(String... args) throws Exception {
   IPracticesService serviceFactory = factory.getService(EnumUser.COMPANY);
   System.out.println("User to call function company");
   serviceFactory.validPractice(new PracticeDTO()); 
   serviceFactory.updatePractice(new PracticeDTO());
   serviceFactory.saveDraft(new PracticeDTO());
   serviceFactory.saveStatusPractice(Long.getLong("123456"), "ACTIVE");
   System.out.println("");
   System.out.println("User to call function intermediary");
   serviceFactory = factory.getService(EnumUser.INTERMEDIARY);
   serviceFactory.validPractice(new PracticeDTO()); 
   serviceFactory.updatePractice(new PracticeDTO());
   serviceFactory.saveDraft(new PracticeDTO());
   serviceFactory.saveStatusPractice(Long.getLong("123456"), "NO ACTIVE");
   System.out.println(""); 
   System.out.println("User to call function regional");
   serviceFactory = factory.getService(EnumUser.REGIONAL);
   serviceFactory.validPractice(new PracticeDTO()); 
   serviceFactory.updatePractice(new PracticeDTO());
   serviceFactory.saveDraft(new PracticeDTO());
   serviceFactory.saveStatusPractice(Long.getLong("123456"), "ENDED");
   System.out.println("");
}

In this example, we have a PracticeFactory class, that for every role of user return a specific instance of service for different role.

Conclusion

The Factory pattern removes instantiation of the actual implementation classes from client code. The Factory pattern makes our code more robust, less coupled, and easier to extend. For example, we can easily change the implementation of the PC class because the client program is unaware of it. Here you can found code example.

27
Jan
2025
0

Design Pattern in spring

I found a lot of documentation on patterns for various languages ​​but also how to use it on different frameworks, but apart from a few examples to explain what it consists of I never had the impression that they were used outside of an academic context. After all, over the years I have never seen the application of a pattern in real cases unless it was used internally in some framework. Since I consider design patterns an excellent approach for solving various cases, I would like to put some real cases that I had to face with the application of the relevant pattern under Spring (which, alas, although I found various articles, the impression they gave me it was as I said only for academic spirit and not for use in real cases). I hope that these examples with the pattern approach can support those who may find themselves in the same situations and be an inspiration to apply them in their contexts too.

Remember: Only Pattern gives you the strength and endurance that you need

26
Jan
2025
0

Preface

How these pages came about – I’m not a writer, and certainly not a super IT expert, but I’d at least like to write some guidelines with examples that might help others in the IT world. Based on my experience, which dates back to when I finished my degree in Computer Engineering in 2002, up until today, working with various companies from small to large on projects across different sectors, the Italian IT landscape I’ve encountered consists of people who definitely know how to tackle problems and find solutions. However, to be honest, every time I faced a problem or needed to find a solution, I never found anything published on Italian websites. In fact, it was always on foreign websites. A few times, I even found answers or potential solutions from some Italians who wrote in English, but always on international platforms. And sadly, of the many senior people I’ve met, very few were truly senior. I was lucky to learn and grow from those few, while most were only senior by age. Unfortunately, like in many other sectors, there are plenty of “half-baked” professionals who studied something entirely different from IT but, due to a lack of job opportunities in their original field and the high demand in the IT industry, ended up taking courses or were brought on board as interns. There’s a misconception that after three to six months of training in a programming language and earning a certification, you’re ready to go, and that simply being on a project for a few years will automatically make you “senior.” The problem is that on these projects, there’s hardly anyone to teach you, or if there are, they’re very few and often doing something else.

As I mentioned, this won’t only be about technical parts with examples, but also what I think are the technicalities that are often mangled with the creation of “IT English” terms that don’t really exist. In my opinion, when we communicate with foreigners, using these “buzzwords” doesn’t make a good impression. In this translation, I have maintained the core meaning of the original text while adapting phrases to sound natural in English, including slang or idiomatic expressions where applicable. Let me know if you’d like any adjustments!

Version Control Software

What are we talking about?
The programs that every developer, whether working in a team or independently, must use: SVN, Git, Mercurial. These tools, rightly so, have their own commands for managing the code versioning process. Specifically, I’m referring to Git, which is the most commonly used and the one I always end up using on projects. But regardless of its use, I’m always faced with these “Itanglish” terms that I don’t know whether to laugh or cry about.
These are the terms that have now become commonplace in all the projects I’ve been on:

Pull – used to fetch the contents from the repository, has become Pullare. Unfortunately, if you search this term online, it translates to: “Verb used to describe the action of smoking, especially when it comes to joints. From this comes the noun pullo, which refers to a drag from a cigarette, vape, etc.” It goes without saying that pullo is also used (a dialectal term, which in some areas of Italy refers to a young chicken or chick), and ironically, every time I hear it, I add “with potatoes,” which gets a laugh from the team, but for me, it’s disheartening.

Merge – used to merge changes into the dev branch, has become Mergiare, and as a result, there’s also Mergiato.

Commit – used to add changes, has become Committare, and thus, we get Committato.

Push – used to send the commit to the repositories, has become Pushare or even Pusho, which, just hearing it, makes me think of something else.

So, what’s the problem, you might ask? Well, the English terms are there, and we should just use those because, by constantly twisting them, they end up becoming the common usage even though they’re wrong. In fact, many times I have to stop and explain because I feel compelled to use them, but I can’t get the message across that the correct terms need to be used. It’s true that the Italian language is constantly evolving, and new terms and words can emerge, but we are “professionals” in IT, not experts in onomastics (a word I didn’t even know until now, meaning “The creation of new words, neologisms”).
Using the right terms helps convey the kind of professional you are, but, as I said, unfortunately, improvisation reigns.

The Interface, the Unknown

I bet that if I ask a developer what an interface is, they would be able to answer me or at least go online to look up the meaning, what it’s for, and when to use it. But what I don’t understand is why, in all the projects I’ve worked on, for 100 classes, I end up with 100 interfaces. I’ve even come across duplicate interfaces where the only difference was the name of the class and the interface, but the methods were the same. I’ve asked several times about the reason behind this, and here are some of the responses I’ve received:

  • “To follow a classification system for dividing classes and interfaces, the code is cleaner and we divide the responsibilities” (Apparently, eating spicy food at night makes you have bad dreams).

  • “We’ve always done it this way” (You’ve always done it like Tafazzi—self-destructive behavior ).

  • “There are various schools of thought…” (Studying philosophy is harmful; I have friends who are philosophers “no offense,” and they’re in bad shape mentally. Sure, my engineer friends have their quirks too, but on average, they’re more tolerable).