Posts

Consider a builder when faced with many constructor

Image
Static factories and constructors share a limitation: they do not scale well to large numbers of optional parameters. Traditionally, programmers have used the telescoping constructor pattern, in which you provide a constructor with only the required parameters, another with a single optional parameter, a third with two optional parameters, and so on, culminating in a constructor with all the optional parameter. We can use alternative when you’re faced with many optional parameters in a onstructor is the JavaBeans pattern, in which you call a parameterless constructor to create the object and then call setter methods to set each required parameter and each optional parameter of interest. This pattern has none of the disadvantages of the telescoping constructor pattern. Unfortunately, the JavaBeans pattern has serious disadvantages of its own. Because construction is split across multiple calls, a JavaBean may be in an inconsistent state partway through its construction. Luckily, there i...

Monitoring Spring Boot Application with Prometheus and Grafana

Image
 Every application that is deployed on production needs some kind of monitoring to see how the application is performing. This will give you some insights on whether the application is performing as aspected or if you would need to take some action in order to obtain the desired level of performance. In the modern world, this data is called Application Performance Metrics (APM).   Let’s try to set up a basic Springboot App monitoring with a Grafana Dashboard and Prometheus.

Consider static factory methods instead of constructors

Image
The traditional way for a class to allow a client to obtain an instance is to provide a public constructor.  There is another technique that should be a part of every programmer’s toolkit. A class can provide a public static factory method, which is simply a static method that returns an instance of the class. A class can provide its clients with static factory methods instead of, or in addition to, public constructors. One advantage of static factory methods is that, unlike constructors, they have names. A second advantage of static factory methods is that, unlike constructors, they are not required to create a new object each time they’re invoked. A third advantage of static factory methods is that, unlike constructors, they can return an object of any sub-type of their return type. A fourth advantage of static factories is that the class of the returned object can vary from call to call as a function of the input parameters. A fifth advantage of static factories is that the clas...

Replace Conditional Logic with Strategy Pattern

Image
The basic principle of object-oriented design patterns is that they should be open for extension and closed for modification. (OCP, Open Closed Principle) It should be possible to extend or change the functionality of a module without modifying the code. When we have if-else statment in method and want to modify by adding new functionality the OCP priciple will be violated. OCP: (Open-Closed Principle). Software entities (classes, modules, functions, etc.) should be open for extension but closed for change. To solve the problem we can use design pattern.  

Data Auditing in Spring Data JPA

Image
  Many systems require auditing and tracking the change in the persistence layer. It is really helpful to investigate problems related to the data. Data auditing typically answers these questions: when was it created, who was create the record, when was it created, who modified the record last and when was it modified last.   Spring provides four annotations in the spring-data-commons module for auditing features.  @CreatedByrepresents the principal that created the entity containing the field. @CreatedDate represents the date the entity containing the field was created. @LastModifiedBy represents the principal that recently modified the entity containing the field. @LastModifiedDaterepresents the date the entity containing the field was recently modified. Besides the four mentioned annotations, Spring also provides @EnableJpaAuditing that aims enable auditing feature. In order to explain how the auditing feature works in Spring Data JPA, we should be known abou...

Custom Spring Initializer

Image
Typically, REST developers use Spring Initializr to create the initial setup.  This comes as a web based UI mainly which provides the ability to create a new Spring Boot project using dependencies available and download the project created as a zip. So we don’t have to create it from scratch. All the basic structure of the project is already there in this downloaded zip. Spring Initializer comes as IDE plugins.  Why do we need to create our own Spring Boot Server? In order for a large team to work on microservices architecture, it must constantly build Spring Boot projects / libraries. Having a custom spring initiator enables developers to generate projects / libraries where they don't have to worry about package name, project name conventions, etc. You can configure the dependency list to suit your needs by overriding or using the default dependency list. The project version, build tool type, Java version, project descriptions etc are definable, so there is no problem rememb...

Validate model with Strategy Pattern to keep Open–Closed Principle

Image
  Defensive Programming with Strategy Pattern Defensive programming is an approach to programming that attempts to ensure that software still functions under adverse or unforeseen circumstances.  Open–Closed Principle Classes, functions, and modules should be closed for modification but open for extension. That means closed for modification and open for extension. Adding new functionality should not modify existing source code. A component should be extendable to make it behave in new ways. Why Strategy pattern? The Strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable.