Posts

Showing posts from September, 2022

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.