Cleaner Code with Static Factory Methods




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 Implementation


Go to source code

Comments

Popular posts from this blog

Debug Java Stream in Intellij Idea

Creating Efficient Docker Images with Spring Boot 2.3

CRUD Goes Even Easier With JPABuddy