Posts

Spring boot with Thymeleaf example

Image
This blog shows how to use Thymeleaf as a template engine with Spring boot. Technologies Used Java 8 Spring framework  1.5.9 Spring boot 1.5.9 Thymeleaf 2.1.6 Tomcat embed 8.5.23 Logback 1.1.1.1 Junit 4.12 Bootstrap 4 Maven 3.5.0 Implementation This project uses Maven to build the project.  It shows a very simple example of a login page with corresponding Spring boot controller. The goal of this project is to have a login page rendered using Thymeleaf template. The template it self uses the bootstrap css for styling. High level steps as described bellow with sample code Define the pom.xml file Write Spring boot app class Write Spring boot controller class Write  Thymeleaf Template for the controller class Write the CSS referred by Template file pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance...

Installing Couchdb on GCP Compute Engine

Image
This article covers the steps that needed to install Apache Couchdb on Google Cloud Platform. There are various way of how to achieve this, that is having a Compute Engine (VM) installed with Couchdb. One option is to use the the Marketplace in GCP which offers quick option to deploy on Cloud Platform. Login to GCP and click on the  Marketplace Icon and then search for CouchDB. Although we would still need to perform some additional steps like Creating Firewall Rule etc so that we can access the instance after getting VM with CouchDB installed from Marketplace. Rather than using the Marketplace, we would do everything manually on the VM Instance. Following are the steps in details that i had to take to lunch an instance of CouchDB, do configuration update and create Firewall Rules to allow access to the instance. High level steps as covered bellow are Create an Instance of Google Cloud Engine Instance Install Apache CouchDB in the VM Configure the Database Instan...

Testing private method in Java

Private method in java is not accessible outside the Class which defines it. Since they cannot be accessed we cannot call them from any Test Class directly to test them. Following are some of the options to test the private method 1) Change the visibility of the private method to package or protected. 2) Create a public method in the Class and call the private method from the public method Above two solutions requires us to change the original Class. Some times we cannot change the original Class but still would like to have a Test Class for the same method. The other option is to use Reflection api to invoke the private method. For example lets try to test a private method multiple on Class CreditcardMask.java Class targetClass = (Class ) Class.forName(CreditcardMask.class.getName()); // get the method multiply Method testMethod = targetClass.getDeclaredMethod("multiply", Integer.class, Integer.class); Assert.assertNotNull("testMethod is Null", tes...

Proxy Design Pattern In java

Image
Type : Structural Design Pattern Summary Proxy Design pattern lets a Proxy Object rather then the real object to interact with Client. By doing so the proxy can control access to the real object or delay the creation of real object unless it is is really needed. Description In Proxy Design pattern the Client is served with a Proxy Object rather than the real object. Benefit of doing so is that, we have greater control in when to create the real object. It would help us in providing in lazy loading the real object, providing extra security checking etc.  Example For example lets consider a service called UserDetails Service which the Client calls to get UserDetails Object. Now say  UserDetails is a complex object, and we need to real the database to populate it. Also we would like to have a security authorization check by which we will not show the details at all or show only few attributes from UserDetails Object. Returning a Proxy rather than orig...

Flyweight Pattern in Java

Image
Type : Structural Design Pattern Summary Flyweight design pattern deals with sharing some of the internal details between different object instances, so that we can save memory by basically using less duplication of the attributes across its instances. Details In Flyweight design pattern the Object attributes are categorized into its extrinsic and intrinsic attributes.  Extrinsic  attributes  are the attributes that can be considered as sharable across other instances.  Intrinsic  attributes  are the attributes that are unique for each object, like the primary key, unique fields etc. In Flyweight design we can improve the memory footprint by reusing the extrinsic properties in multiple instances of the object. Flyweight can use Cache to store the different Extrinsic states. Factory method can be used to create the instances of the Extrinsic object and stored in cache. Example For instance say we are designing a Vehicle in OOP. ...

Adapter Design Pattern in Java

Image
Type : Structural Design Pattern Summary Adapter design pattern is a Structural design pattern. It is used to provides compatible interface for and Class so that can be used by Client. Adapter pattern is used to provide compatibility for Classes which already exists and cannot be modified. Details Need for adapter design pattern is very obvious. It is also very common in real world as well as in Software Engineering. For instance if we have any electrical appliances which runs on 110 Volt and then we travel to a country where the electricity supply is 220 V. We cannot change or modify the electric connection to provide us 110 V. Rather we can use a adapter to convert the 220 V from outlet to 110V. Adapter pattern is similar to Bridge pattern because both provide mechanism to connect incompatible Classes. Advantages Incompatible Object or Class can be used using an Adapter for it. Disadvantages Increases number of Classes Adapter Class needs to be modif...

Abstract Factory Design Pattern in Java

Image
Type:  Creational Design Pattern Summary Provide an indirect way to create object that abstracts the creation of families of related objects without directly specifying their concrete Classes. The Factory owns the responsibility create objects. Client is not aware of how to create the object. Details It is similar to Factory method design pattern. Only difference is that rather than returning instance of Subclasses, Abstract factory method can create object of Subclasses having different inheritance hierarchy. Generally Abstract Factory will return different Factories, which in turn can be used to Objects Abstract Factory is used when we want to create objects by Clients and control which instance should be returned. It is used in DAO (Data Access Object). It can be used to design Platform dependent software solution, by providing platform specific implementations.   Example We will extend the example of Sales Man and Differe...