Posts

Showing posts from December, 2021

Switching from RestTemplate to WebClient

Image
RestTemplate will be deprecated in a future version(> 5.0) and will not have major new features added going forward. Since the REST era, most developers have become used to working with Spring’s traditional RestTemplate from the package spring-boot-starter-web for consuming Rest services. Spring also has a WebClient in its reactive package called s pring-boot-starter-webflux . Now, let’s move on to some example basic methods we can use on the RestTemplate class to communicate with our Rest service. We apply CRUD operations, specify our return object’s class, some parameters, body, header, etc. WebClient exists since Spring 5 and provides an asynchronous way of consuming Rest services, which means it operates in a non-blocking way. WebClient is in the reactive WebFlux library and thus it uses the reactive streams approach. However, to really benefit from this, the entire throughput should be reactive end-to-end.  Now, let’s move on to some example basic methods we can use on t...

Cleaner Code with Static Factory Methods

Image
Static Factory Method is a classic creational design pattern. It is a public static method on the object that returns a new instance of the object.  These type of methods share the same benefits as the traditional factory method design pattern.  This is especially useful for value objects that don’t have a separate interface and implementation class.   Static factory methods example with DTO's. In our web services we use Data Transfer Object as an object that carries data between processes  in order to reduce the number of method calls. So DTO’s carries data between processes. In the case of a REST API, the data is carried between subsystems i.e clients and servers. The implementation of the DTO pattern is typically on the server side where we have a couple of layers involved. The key elements to implement this pattern are simple: the factory method must be public, static,  and return an instance of the class in which it is defined. Static Factory Methods ...

Java Singleton Design Pattern Best Practices with Bill Pugh Singleton Implementation

Image
The implementation of Java Singleton pattern has always been a controversial topic among developers.  Here we will check Bill Pugh Singleton Implementation. But first what is Singleton Pattern? According to good programming practice: Singleton pattern restricts the instantiation of a class and ensures that only one instance of the class exists in the java virtual machine. The singleton class must provide a global access point to get the instance of the class. Singleton pattern is used for logging, drivers objects, caching and thread pool. Singleton design pattern is also used in other design patterns like Abstract Factory, Builder, Prototype, Facade etc. Singleton design pattern is used in core java classes also, for example java.lang.Runtime, java.awt.Desktop. Java Singleton Pattern Implementation To implement a Singleton pattern, we have different approaches but all of them have the following common concepts. Private constructor to restrict instantiation of the class from other c...