Posts

Showing posts from January, 2022

Docker for Java development

Image
The promise to use Docker during development is to provide a consistent test environment on your development machines and the different environments you use (such as QA and manufacturing). The last thing you want to do is slow your development cycle. Even if you or your team are not involved in using Docker on development computers as part of this process, there are several use cases for modifying and debugging code running in the container. For example, a developer can use Docker to mimic the production environment to reproduce errors or other conditions. In addition, the ability to debug remotely on a docked host can enable practical troubleshooting of a running environment such as QA. Let us take as an example a simple application that emulates the console's password change utility. The program asks for the login and the current password. After verifying the credentials, it prompts you to enter your new password twice. If the passwords do not match, it asks for a new password ag...

Proxy design pattern in Java

Image
  The Proxy pattern allows us to create an intermediary that acts as an interface to another resource, while also hiding the underlying complexity of the component. Types of proxies Remote proxy: They are responsible for representing the object located remotely. Talking to the real object might involve marshalling and unmarshalling of data and talking to the remote object. All that logic is encapsulated in these proxies and the client application need not worry about them. Virtual proxy: These proxies will provide some default and instant results if the real object is supposed to take some time to produce results. These proxies initiate the operation on real objects and provide a default result to the application. Once the real object is done, these proxies push the actual data to the client where it has provided dummy data earlier. Protection proxy: If an application does not have access to some resource then such proxies will talk to the objects in applications that have access t...

Builder design pattern in Java

Image
The interesting thing about Builder is that you can find different variations of the builder . For example, there is a variation of pattern with director class that is responsible for building objects. And also there is a variation of builder pattern that called chain builder and allows to build new objects step by step. Personally, I believe that the second variation is more popular. Implementation with director. To implement builder pattern with director we have to: declare builder interface create multiple builders create director that can work with different builders in client code instantiate director and concrete implementation of builder to build new objects And this below is another version of builder pattern called chain builder. To implement builder pattern with chain method calls we have to: create inner Builder class inside type that we want to build implement method in type that returns reference to the builder object declare steers in builder that returns the re...

Java Record "shallowly immutable"

Image
  In the documentation of Java Records there's the term "shallowly immutable". What do we mean by shallowly immutable?  Shallowly immutable means, that if a class has fields, these fields are treated as being final. However, their fields (i.e. the fields of the fields) don't need to be final. An example for the "shallowly immutability" could be a record instance having an ArrayList instance as its field. While that record field cannot refer to any other ArrayList list instance, the list's elements can still change (by code holding a reference to that list instance contained in the record). Because the list instance remains mutable even though it's now contained in an immutable record instance – you will only ever get "shallow immutability" from a record. An immutable record This is record is an immutable object. Since it is a record, it is automatically final and has no setters. All is good. A mutable record This time the record is a ...